/** * @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; }
public function serialize(Document $document) { $json = []; if ($document->resources instanceof SearchResultResourceCollection) { $searchResult = $document->resources->getSearchResult(); foreach (['query', 'query-time', 'facets'] as $property) { $method = 'get' . Inflector::camelize($property, TRUE); $json[$property] = $searchResult->{$method}(); } } return $json; }
/** * Construye una instancia a partir de un objeto con una interfaz similar. * @param string $url * @param Url $root NULL por defecto. */ public static function fromString($url, Url $root = null) { $instance = new static(); $components = static::parseUrl($url); foreach ($components as $component => $value) { $method = 'set' . Inflector::camelize($component); call_user_func(array($instance, $method), $value); } $instance->setOriginal($url); $instance->setRoot($root); return $instance; }
/** * @param Request $request * @param Params $params * @return array */ public function parse(Request $request, Params $params) { $entity = $params->entities->primary->first(); $ids = NULL; // @todo Encapsulate. $method = 'get' . Util\Inflector::camelize($params->relationship); $relation = $entity->{$method}(); if ($relation instanceof Collection) { $relation = $relation->toArray(); } $ids = $this->parseDeleteAction($params, $relation); $entityData = [(string) $entity->getId() => [self::LINKS => [$params->relationship => $ids]]]; return $entityData; }
/** * @return array */ private function indexEntityClassNames() { $indexedClassNames = []; $entityClassNames = $this->em->getConfiguration()->getMetadataDriverImpl()->getAllClassNames(); foreach ($entityClassNames as $name) { // @todo Support subtypes. $resourceType = Util\Inflector::typify($name); $indexedClassNames[$resourceType][] = $name; } //Config class override foreach ($this->getResourcesConfig()->getAll() as $resource) { if (!empty($resource->type) && !empty($resource->class)) { $indexedClassNames[$resource->type] = array($resource->class); } } return $indexedClassNames; }
/** * @param ContainerBuilder $container */ public function process(ContainerBuilder $container) { foreach (self::$services as $service) { $name = $tag = self::SERVICE_PREFIX . $service; if (!$container->hasDefinition($name)) { continue; } $definition = $container->getDefinition($name); $taggedServices = $container->findTaggedServiceIds($tag); foreach ($taggedServices as $id => $tagAttributes) { foreach ($tagAttributes as $attributes) { $definition->addMethodCall('add' . Inflector::camelize($service), [new Reference($id), $attributes[self::RESOURCE_TYPE]]); } } } return $this; }
/** * @param \GoIntegro\Hateoas\JsonApi\ResourceEntityInterface|string $entityClassName * @param ResourceRelationships $relationships * @return array * @todo Publicar o eliminar el parámetro $entityClassName. */ protected function getFields($entityClassName, ResourceRelationships $relationships) { $fields = []; $class = $this->metadataCache->getReflection($entityClassName); foreach ($class->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { if (in_array($method->getShortName(), self::$reservedGetters)) { continue; } if (Reflection::isMethodGetter($method)) { $fields[] = Inflector::hyphenate(substr($method->getShortName(), 3)); } } foreach (ResourceRelationships::$kinds as $kind) { $fields = array_diff($fields, array_keys($relationships->{$kind})); } $fields = array_diff($fields, $relationships->dbOnly); return new ResourceFields($fields); }
public function serialize(Document $document) { $json = []; $pagination = $document->pagination; if (!empty($pagination)) { foreach (self::$relationships as $relationship) { $method = 'get' . Inflector::camelize($relationship); $page = $this->{$method}($document); if (is_null($page)) { continue; } $resource = $document->resources; $relationKey = $this->buildRelationKey($resource, $relationship); $query = $pagination->paginationlessUrl->getQuery(); $url = $pagination->paginationlessUrl->getOriginal(); $url .= empty($query) ? '?' : '&'; $url .= 'page=' . $page . '&size=' . $pagination->size; $json[$relationKey] = ['href' => $url, 'type' => $resource->getMetadata()->type]; } } return $json; }
/** * @param Request $request * @param Params $params * @param array $body * @return array * @todo Sólo sirve para actualizar ahora. */ public function parse(Request $request, Params $params, array $body) { $entity = $params->entities->primary->first(); $ids = NULL; // @todo Encapsulate. $method = 'get' . Util\Inflector::camelize($params->relationship); $relation = $entity->{$method}(); if ($relation instanceof Collection) { $relation = $relation->toArray(); } if (in_array($params->action->name, [RequestAction::ACTION_CREATE, RequestAction::ACTION_UPDATE])) { if (!is_array($body) || !isset($body[$params->relationship])) { throw new ParseException(self::ERROR_EMPTY_BODY); } $ids = $body[$params->relationship]; if (RequestAction::ACTION_CREATE == $params->action->name) { $ids = $this->parseCreateAction($ids, $relation); } } $entityData = [(string) $entity->getId() => [self::LINKS => [$params->relationship => $ids]]]; return $entityData; }
/** * @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; }
/** * Para relaciones que no van en el campo "links" del resource object. * @param \ReflectionClass $class * @param string $name * @return boolean * @see http://jsonapi.org/format/#document-structure-url-templates */ private static function isLinkOnlyRelation(\ReflectionClass $class, $name) { $getterName = 'get' . Inflector::camelize($name); return !$class->hasMethod($getterName) || !Reflection::isMethodGetter($class->getMethod($getterName)); }
/** * @param string $field * @return mixed */ public function callGetter($field) { $camelized = Inflector::camelize($field, TRUE); $getter = 'get' . $camelized; $injector = 'inject' . $camelized; if (is_callable([$this, $injector])) { return $this->{$injector}(); } elseif ($this->metadata->isField($field) || $this->metadata->isRelationship($field)) { if (is_callable([$this->entity, $getter])) { return $this->entity->{$getter}(); } throw new \LogicException(self::ERROR_INACCESSIBLE_MEMBER); } throw new \Exception(sprintf(self::ERROR_UNCALLABLE_GETTER, $getter)); }
/** * @param \ReflectionMethod $method * @return string * @todo ¿Quizás moverlo al Inflector? */ private static function fieldFromInjector(\ReflectionMethod $method) { $name = substr($method->getShortName(), strlen('inject')); return Inflector::hyphenate($name); }
/** * @param Params $params * @param ResourceEntityInterface $entity * @return array * @throws EntityAccessDeniedException * @todo Find by relationship Ids when deleting. * @todo Refactor. */ protected function findRelationshipEntities(Params $params, ResourceEntityInterface $entity) { $method = 'get' . Util\Inflector::camelize($params->relationship); $entities = $entity->{$method}(); if ($entities instanceof Collection) { $entities = $entities->toArray(); } elseif (!is_array($entities)) { $entities = [$entities]; } return empty($params->relationshipIds) ? $this->filterRelationshipEntities($params, $entities) : $this->selectRelationshipEntities($params, $entities); }