Exemplo n.º 1
0
 /**
  * @param Resource $resource
  */
 public function addResource($resource)
 {
     $version = $resource->getVersion();
     $entityClass = $resource->getEntityClass();
     if (isset($this->entityClassIndex[$version][$entityClass])) {
         throw new \InvalidArgumentException(sprintf('A Resource class already exists for "%s".', $entityClass));
     }
     $shortName = $resource->getShortName();
     if (isset($this->shortNameIndex[$version][$shortName])) {
         throw new \InvalidArgumentException(sprintf('A Resource class with the short name "%s" already exists.', $shortName));
     }
     $this->append($resource);
     $this->entityClassIndex[$version][$entityClass] = $resource;
     $this->shortNameIndex[$version][$shortName] = $resource;
     foreach ($resource->getAlias() as $alias) {
         if (!class_exists($alias)) {
             $this->shortNameIndex[$version][$alias] = $resource;
             continue;
         }
         $this->entityClassIndex[$version][$alias] = $resource;
     }
 }
Exemplo n.º 2
0
 public function testShortName()
 {
     $resource = new Resource('Eliberty\\ApiBundle\\Tests\\Fixtures\\DummyEntity');
     $resource->initShortName('Test');
     $this->assertEquals('Test', $resource->getShortName());
 }
Exemplo n.º 3
0
 /**
  * @param $resource
  * @param ApiDoc $annotation
  * @param Resource|Resource $dunglasResource
  * @param Route $route
  * @return ApiDoc
  */
 private function addFilters($resource, ApiDoc $annotation, Resource $dunglasResource, Route $route)
 {
     $data = $annotation->toArray();
     $tags = isset($data['tags']) ? $data['tags'] : [];
     //filter embed
     if ('DELETE' !== $annotation->getMethod()) {
         $availableIncludes = $this->transformerHelper->getAvailableIncludes($resource);
         $defaultIncludes = $this->transformerHelper->getDefaultIncludes($resource);
         if (false === array_key_exists('embed', $tags)) {
             $annotation->addFilter('embed', ['requirement' => '\\t', 'description' => 'Include resources within other resources.', 'available' => is_array($availableIncludes) ? implode(',', $availableIncludes) : $availableIncludes, 'default' => is_array($defaultIncludes) ? implode(',', $defaultIncludes) : $defaultIncludes]);
         } else {
             unset($data['requirements']['embed']);
             $data['tags']['embed'] = "#298A08";
             $path = explode('/', $route->getPath());
             $embed = array_pop($path);
             $singularize = Inflector::singularize($embed);
             if ($embed !== $singularize) {
                 $data['tags']['collection'] = "#0040FF";
             }
             foreach ($data['requirements'] as $key => $value) {
                 $data['requirements'][$key] = array_merge(['name' => $key], $value);
             }
             $annotation = new ApiDoc($data);
             $routeClone = clone $route;
             $annotation->setRoute($routeClone);
             $tags = isset($annotation->toArray()['tags']) ? $annotation->toArray()['tags'] : [];
         }
     }
     if (false !== array_key_exists('collection', $tags)) {
         foreach ($dunglasResource->getFilters() as $filter) {
             foreach ($filter->getDescription($dunglasResource) as $key => $value) {
                 $annotation->addFilter($key, ['type' => isset($value['type']) ? $value['type'] : 'string', 'requirement' => isset($value['requirement']) ? $value['requirement'] : '[a-zA-Z0-9-]+', 'description' => isset($value['description']) ? $value['description'] : $key . ' filter', 'default' => '']);
             }
         }
         //filter perpage
         $annotation->addFilter('perpage', ['requirement' => '\\d+', 'description' => 'How many object return per page.', 'default' => 10]);
         //filter perpage
         $annotation->addFilter('page', ['requirement' => '\\d+', 'description' => 'How many page start to return.', 'default' => 1]);
     }
     return $annotation;
 }