     1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

    <?php
    namespace Catapult;
    /**
     * Path resource will build paths
     * for objects and figure out which
     * ids they need in their paths.
     * usage follows:
     *
     * PathResource::Make($object)
     *
     */
    class PathResource extends BaseResource {

        /**
         * constructor
         * should allow basic PathResource
         * which would structure based on key value
         *
         * @param object: Catapult Model
         * @param data: Ensured Data
         */
        public function __construct(&$object, $data) {
          if (!isset($object->hasPath) || !$object->hasPath) {
            $object->path = "";
            foreach ($data as $k => $d) {
              if ($d == "") {
                $object->path .= "/$k";
              } else {
                $object->path .= "$k/$d";
              }
            }
          }
        }

        /**
         * path should be build once
         * the object has been
         * initialized with data
         * to build a path we use
         * each dependancy, if has id in data
         * add after path and also set in
         * object.
         *
         * result:
         * depend1/depend1id/path
         *
         * @param object: Catapult Model Object
         * @param data: EnsureResource input or Array
         */
        public static function Make(&$object, &$data=null) {
          $path = "";
          foreach($object->depends->terms as $dep) {
            if ($dep->plural) {
              $path .= TitleUtility::toPlural($dep->term) . "/";
            } else {
              $path .= $dep->term . "/";
            }
            /** represent a term in singular form **/
            $term = TitleUtility::toSingular($dep->term) . "Id";
            if (is_array($data) && array_key_exists($term, $data)) {
              $depid = $data[$term];
              $path .= $depid . "/";
              $object->{$dep->term} = $depid;

              unset($data[$term]);

              /** only set when we're given data **/
              $object->hasPath = true;
            }
          }

          $object->path = $path . $object->path;
        }
    }