コード例 #1
0
ファイル: Normalizer.php プロジェクト: eliberty/api-bundle
 /**
  * @param                   $object
  * @param ResourceInterface $dunglasResource
  * @param Manager           $fractalManager
  * @param Request|null      $request
  * @param bool              $defaultIncludes
  *
  * @return array
  * @throws \Exception
  */
 public function normalize($object, ResourceInterface $dunglasResource, Manager $fractalManager, Request $request = null, $defaultIncludes = true)
 {
     $transformer = $this->transformerHelper->getTransformer($dunglasResource->getShortName());
     if (null !== $request) {
         $fractalManager->parseIncludes($this->getEmbedsWithoutOptions($transformer, $request));
     }
     $resource = new Item($object, $transformer);
     if ($object instanceof Paginator || $object instanceof PersistentCollection) {
         $resource = new Collection($object, $transformer);
         if ($fractalManager->getSerializer() instanceof ArraySerializer) {
             $resource->setPaginator(new DunglasPaginatorAdapter($object, $resource));
         }
     }
     $rootScope = $fractalManager->createData($resource, $dunglasResource->getShortName());
     if ($defaultIncludes === false) {
         $transformer->setDefaultIncludes([]);
     }
     $transformer->setCurrentScope($rootScope)->setEmbed($dunglasResource->getShortName());
     return $rootScope->toArray();
 }
コード例 #2
0
 /**
  * Creates operation.
  *
  * @param ResourceInterface $resource
  * @param bool              $collection
  * @param string|array      $methods
  * @param string|null       $path
  * @param null              $controller
  * @param null              $routeName
  * @param array             $context
  *
  * @return Operation
  */
 private function createOperation(ResourceInterface $resource, $collection, $methods, $path = null, $controller = null, $routeName = null, array $context = [])
 {
     $shortName = $resource->getShortName();
     if (!isset(self::$inflectorCache[$shortName])) {
         self::$inflectorCache[$shortName] = Inflector::pluralize(Inflector::tableize($shortName));
     }
     // Populate path
     if (!$path) {
         $path = '/' . self::$inflectorCache[$shortName];
         if (!$collection) {
             $path .= '/{id}';
         }
     }
     // Guess default method
     if (is_array($methods)) {
         $defaultMethod = $methods[0];
     } else {
         $defaultMethod = $methods;
     }
     // Populate controller
     if (!$controller) {
         $defaultAction = strtolower($defaultMethod);
         if ($collection) {
             $defaultAction = 'c' . $defaultAction;
         }
         $controller = self::DEFAULT_CONTROLLER . ':' . $defaultAction;
         // Populate route name
         if (!$routeName) {
             $routeName = self::$inflectorCache[$shortName] . '_' . $defaultAction;
         }
     }
     $requirements = [];
     if (strpos($path, '{id}')) {
         $requirements['id'] = '\\d+';
     }
     if (strpos($path, '{embed}')) {
         try {
             $embeds = $this->transformerHelper->getAvailableIncludes($shortName);
             $requirements['embed'] = implode('|', $embeds);
         } catch (\Exception $ex) {
             //commande sfroute symfony
         }
     }
     //        $requirements ['"context.getApiVersion() === '".$apiVersion."'"']
     return new Operation(new Route($path, ['_controller' => $controller, '_resource' => $shortName], $requirements, [], '', [], $methods), self::ROUTE_NAME_PREFIX . $resource->getVersion() . '_' . $routeName, $context);
 }
コード例 #3
0
 /**
  * @param $normalizedInput
  * @param $shortName
  * @param string $type
  * @return array
  */
 public function getParametersParser($normalizedInput, $shortName, $type = 'Output')
 {
     $supportedParsers = [];
     $parameters = [];
     $transformerAttributes = [];
     if ($type === 'Output') {
         $normalizedOutput = $this->normalizeClassParameter($this->transformerHelper->getTransformerClass($shortName));
         $transformerAttributes = $this->getParametersParser($normalizedOutput, $shortName, 'Transformer');
     }
     foreach ($this->getParsers($normalizedInput) as $parser) {
         if ($parser->supports($normalizedInput)) {
             $supportedParsers[] = $parser;
             if ($parser instanceof JmsMetadataParser) {
                 $normalizedInput['groups'] = [];
                 $attributes = $parser->parse($normalizedInput);
                 foreach ($attributes as $key => $value) {
                     if ($key === 'id' && !empty($value)) {
                         $parameters['id'] = $value;
                     }
                     if (isset($parameters[$key]) && isset($value['description'])) {
                         $parameters[$key]['description'] = $value['description'];
                     }
                 }
                 if ($type === 'Output') {
                     $this->transformerHelper->getOutputAttr($shortName, $parameters, 'doc', $attributes, $transformerAttributes);
                 }
                 continue;
             }
             $attributes = $parser->parse($normalizedInput);
             $parameters = $this->mergeParameters($parameters, $attributes);
         }
     }
     if ($type === 'Output') {
         foreach ($supportedParsers as $parser) {
             if ($parser instanceof PostParserInterface) {
                 $parameters = $this->mergeParameters($parameters, $parser->postParse($normalizedInput, $parameters));
             }
         }
     }
     return $parameters;
 }
コード例 #4
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;
 }