Пример #1
0
 /**
  * Serialize a collection.
  *
  * @param string $resourceKey
  * @param array  $data
  *
  * @return array
  */
 public function collection($resourceKey, array $data)
 {
     $dataResponse = [];
     $resourceKey = strtolower($resourceKey);
     $pluralize = Inflector::pluralize($resourceKey);
     $singularize = Inflector::singularize($resourceKey);
     if (!$this->scope->hasParent()) {
         $dataResponse[$pluralize] = [$singularize => $data];
     } else {
         $dataResponse = [$singularize => $data];
     }
     return $dataResponse;
 }
Пример #2
0
 public function addRootRelations(Root $root, ClassMetadataInterface $classMetadata)
 {
     $prefix = $root->getPrefix();
     $relations = array();
     $self_args = $this->router->match($root->getPrefix());
     $relations[] = new Hateoas\Relation('self', new Hateoas\Route($self_args['_route'], array(), false));
     foreach ($root->getEntityNames() as $rel => $entityName) {
         /** @var ClassMetadata $metadata */
         $metadata = $this->entityManager->getMetadataFactory()->getMetadataFor($entityName);
         $routeName = $this->routeResolver->resolveRouteName($metadata->getName(), 'cgetAction');
         if ($routeName) {
             $arguments = array();
             foreach (self::$COLLECTION_ARGUMENTS as $argument) {
                 $arguments[$argument] = '{' . $argument . '}';
             }
             $relations[] = new Hateoas\Relation(Inflector::pluralize($rel), new Hateoas\Route($routeName, $arguments, false));
         }
         $routeName = $this->routeResolver->resolveRouteName($metadata->getName(), 'getAction');
         if ($routeName) {
             $relations[] = new Hateoas\Relation($rel, new Hateoas\Route($routeName, array('id' => "{id}"), false));
         }
         $routeName = $self_args['_route'] . '_schema';
         if ($this->router->getRouteCollection()->get($routeName)) {
             $relations[] = new Hateoas\Relation('schema:' . $rel, new Hateoas\Route($routeName, array('rel' => $rel), false));
         }
     }
     $user = $root->getCurrentUser();
     if ($user instanceof User) {
         $routeName = $this->routeResolver->resolveRouteName(get_class($user), 'getAction');
         if ($routeName) {
             $relations[] = new Hateoas\Relation('currentUser', new Hateoas\Route($routeName, array('id' => $user->getId()), false));
         }
     }
     return $relations;
 }
Пример #3
0
 /**
  * @see DoctrineInflector::pluralize
  */
 public static function pluralize($word)
 {
     return DoctrineInflector::pluralize($word);
 }