     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 78 79 80 81 82 83 84 85

    <?php
    namespace Catapult;
    /**
     * Resolve take a set of arguments
     * and figures out what to
     * do with it. In other words
     * if we have an id in the parameters
     * call get/1. Conversly no id, call create/1
     * if no data is provided simply exit.
     */
    class ResolverResource extends GenericResource {

        public static $keys = array(
            "id",
            "content",
            "number"
        );

        /**
         * finds the main key on a Catapult
         * object
         *
         * @param obj: array or object
         */
        public static function Key($obj) {
          foreach (self::$keys as $k) {
            if (BaseUtilities::prop_or_arr($obj, $k)) {
              return $k;
            }
          }

          Throw new \CatapultApiException("This object does not have a valid id, content field");
        }

        /**
         * Main function figure out whether
         * to create or get this object.
         *
         * @param object: Catapult Model Object
         * @param data: EnsuredInput
         */
        public static function Find(&$object, $data)
        {
          $data = Ensure::Input($data);
          $is_str = $data->is_string();

          if ($data->data == null || sizeof($data) == 0)
            return $object;
          /**
           * certain objects cannot
           * be resolved without there parents
           * this is 'only' the case when arity is 1
           * otherwise we follow normal flow
           */
          if ($object->loads->silent && $is_str)
            return $object;
          /**
           * if we have a singleton
           * treat as primary key then
           * invoke create call with
           * in some cases the primary method can be
                       * create in others get
           */
          if ($is_str && !$object->loads->silent)
            if ($object->loads->primary == 'create' || !isset($object->loads->primary))
              return $object->create(array(
                $object->loads->primary => $data
              ));
            else
              return $object->get($data->get());

          $input = Ensure::Input($data);

          $data = $input->get();

          if (in_array("id", array_keys($data)))
            $object->get($data['id']);
          else
            $object->create($data);
        }

    }
    final class Resolver extends ResolverResource {}