Beispiel #1
0
 /**
  * @param Request $request
  * @param Params $params
  * @return array
  */
 public function parse(Request $request, Params $params)
 {
     $filters = [];
     if (empty($params->primaryClass)) {
         return $filters;
     }
     $metadata = $this->metadataMiner->mine($params->primaryClass);
     $add = function ($param, $value, $type) use(&$filters) {
         $property = Inflector::camelize($param);
         if (is_string($value)) {
             $value = explode(',', $value);
         }
         $filters[$type][$property] = $value;
     };
     foreach ($request->query as $param => $value) {
         if ($metadata->isField($param)) {
             $add($param, $value, 'field');
         } elseif ($metadata->isRelationship($param)) {
             $add($param, $value, 'association');
             // Doctrine 2 term.
         } elseif (!in_array($param, self::$reserved)) {
             $add($param, $value, 'custom');
         }
     }
     return $filters;
 }
 /**
  * @return EntityResource
  */
 public function create()
 {
     $metadata = $this->metadataMiner->mine($this->entity);
     $resource = $metadata->resourceClass->newInstance($this->entity, $metadata);
     if ($metadata->resourceClass->implementsInterface(self::CONTAINER_AWARE_INTERFACE)) {
         $resource->setContainer($this->serviceContainer);
     }
     return $resource;
 }
Beispiel #3
0
 /**
  * @param Params $params
  * @param RequestAction $action
  * @return boolean
  */
 private function isRelationshipToMany(Params $params, RequestAction $action)
 {
     if (RequestAction::TARGET_RELATIONSHIP == $action->target) {
         $metadata = $this->mm->mine($params->primaryClass);
         return $metadata->isToManyRelationship($params->relationship);
     }
     return FALSE;
 }
Beispiel #4
0
 /**
  * @param Request $request
  * @param Params $params
  * @return string
  */
 public function parseRelationship(Request $request, Params $params)
 {
     $relationship = $this->parseUrlPart($request, self::PRIMARY_RESOURCE_RELATIONSHIP);
     if (!empty($relationship)) {
         $metadata = $this->mm->mine($params->primaryClass);
         if (!$metadata->isRelationship($relationship) || $metadata->isLinkOnlyRelationship($relationship)) {
             throw new RelationshipNotFoundException(self::ERROR_RELATIONSHIP_UNDEFINED);
         }
     }
     return $relationship;
 }
 /**
  * @param Params $params
  * @return string
  */
 private function getRelationType(Params $params)
 {
     $relation = NULL;
     $metadata = $this->metadataMiner->mine($this->params->primaryClass);
     if ($metadata->isToOneRelationship($this->params->relationship)) {
         $relation = $metadata->relationships->toOne[$this->params->relationship];
     } elseif ($metadata->isToManyRelationship($this->params->relationship)) {
         $relation = $metadata->relationships->toMany[$this->params->relationship];
     } else {
         throw new FactoryErrorException(self::ERROR_PARAMS_INCOHERENT);
     }
     return $relation->type;
 }
Beispiel #6
0
 /**
  * @param Request $request
  * @param Params $params
  * @return array
  */
 public function parse(Request $request, Params $params)
 {
     $sorting = [];
     if (!$request->query->has('sort')) {
         return $sorting;
     }
     if (empty($params->primaryClass)) {
         return $sorting;
     }
     $sort = $request->query->get('sort');
     if (!is_array($sort)) {
         $sort = [$params->primaryType => $sort];
     }
     $metadata = $this->metadataMiner->mine($params->primaryClass);
     foreach ($sort as $resource => $sort) {
         if (is_string($sort)) {
             $sort = explode(',', $sort);
         }
         foreach ($sort as $field) {
             if ('-' != substr($field, 0, 1)) {
                 $order = Params::ASCENDING_ORDER;
             } else {
                 $order = Params::DESCENDING_ORDER;
                 $field = substr($field, 1);
             }
             if ($resource == $params->primaryType && $metadata->isField($field)) {
                 $type = 'field';
             } elseif ($metadata->isToOneRelationship($resource)) {
                 $type = 'association';
             } else {
                 $type = 'custom';
             }
             $field = Inflector::camelize($field);
             $camelizeResource = Inflector::camelize($resource);
             $sorting[$type][$camelizeResource][$field] = $order;
         }
     }
     return $sorting;
 }