vendor/willdurand/hateoas/src/Configuration/Metadata/Driver/AnnotationDriver.php line 53

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Hateoas\Configuration\Metadata\Driver;
  4. use Doctrine\Common\Annotations\Reader as AnnotationsReader;
  5. use Hateoas\Configuration\Annotation;
  6. use Hateoas\Configuration\Embedded;
  7. use Hateoas\Configuration\Exclusion;
  8. use Hateoas\Configuration\Metadata\ClassMetadata;
  9. use Hateoas\Configuration\Provider\RelationProviderInterface;
  10. use Hateoas\Configuration\Relation;
  11. use Hateoas\Configuration\RelationProvider;
  12. use Hateoas\Configuration\Route;
  13. use JMS\Serializer\Expression\CompilableExpressionEvaluatorInterface;
  14. use JMS\Serializer\Expression\Expression;
  15. use JMS\Serializer\Type\ParserInterface;
  16. use Metadata\ClassMetadata as JMSClassMetadata;
  17. use Metadata\Driver\DriverInterface;
  18. class AnnotationDriver implements DriverInterface
  19. {
  20.     use CheckExpressionTrait;
  21.     /**
  22.      * @var AnnotationsReader
  23.      */
  24.     private $reader;
  25.     /**
  26.      * @var RelationProviderInterface
  27.      */
  28.     private $relationProvider;
  29.     /**
  30.      * @var ParserInterface
  31.      */
  32.     private $typeParser;
  33.     public function __construct(
  34.         AnnotationsReader $reader,
  35.         CompilableExpressionEvaluatorInterface $expressionLanguage,
  36.         RelationProviderInterface $relationProvider,
  37.         ParserInterface $typeParser
  38.     ) {
  39.         $this->reader $reader;
  40.         $this->relationProvider $relationProvider;
  41.         $this->expressionLanguage $expressionLanguage;
  42.         $this->typeParser $typeParser;
  43.     }
  44.     public function loadMetadataForClass(\ReflectionClass $class): ?JMSClassMetadata
  45.     {
  46.         $annotations $this->reader->getClassAnnotations($class);
  47.         if (=== count($annotations)) {
  48.             return null;
  49.         }
  50.         $classMetadata = new ClassMetadata($class->getName());
  51.         $classMetadata->fileResources[] = $class->getFilename();
  52.         foreach ($annotations as $annotation) {
  53.             if ($annotation instanceof Annotation\Relation) {
  54.                 $classMetadata->addRelation(new Relation(
  55.                     $annotation->name,
  56.                     $this->createHref($annotation->href),
  57.                     $this->createEmbedded($annotation->embedded),
  58.                     $this->checkExpressionArray($annotation->attributes) ?: [],
  59.                     $this->createExclusion($annotation->exclusion)
  60.                 ));
  61.             } elseif ($annotation instanceof Annotation\RelationProvider) {
  62.                 $relations $this->relationProvider->getRelations(new RelationProvider($annotation->name), $class->getName());
  63.                 foreach ($relations as $relation) {
  64.                     $classMetadata->addRelation($relation);
  65.                 }
  66.             }
  67.         }
  68.         if (=== count($classMetadata->getRelations())) {
  69.             return null;
  70.         }
  71.         return $classMetadata;
  72.     }
  73.     private function parseExclusion(Annotation\Exclusion $exclusion): Exclusion
  74.     {
  75.         return new Exclusion(
  76.             $exclusion->groups,
  77.             null !== $exclusion->sinceVersion ? (string) $exclusion->sinceVersion null,
  78.             null !== $exclusion->untilVersion ? (string) $exclusion->untilVersion null,
  79.             null !== $exclusion->maxDepth ? (int) $exclusion->maxDepth null,
  80.             $this->checkExpression($exclusion->excludeIf)
  81.         );
  82.     }
  83.     /**
  84.      * @param mixed $href
  85.      *
  86.      * @return Expression|mixed
  87.      */
  88.     private function createHref($href)
  89.     {
  90.         if ($href instanceof Annotation\Route) {
  91.             return new Route(
  92.                 $this->checkExpression($href->name),
  93.                 is_array($href->parameters) ? $this->checkExpressionArray($href->parameters) : $this->checkExpression($href->parameters),
  94.                 $this->checkExpression($href->absolute),
  95.                 $href->generator
  96.             );
  97.         }
  98.         return $this->checkExpression($href);
  99.     }
  100.     /**
  101.      * @param Annotation\Embedded|mixed $embedded
  102.      *
  103.      * @return Expression|mixed
  104.      */
  105.     private function createEmbedded($embedded)
  106.     {
  107.         if ($embedded instanceof Annotation\Embedded) {
  108.             $embeddedExclusion $embedded->exclusion;
  109.             if (null !== $embeddedExclusion) {
  110.                 $embeddedExclusion $this->parseExclusion($embeddedExclusion);
  111.             }
  112.             return new Embedded(
  113.                 $this->checkExpression($embedded->content),
  114.                 $this->checkExpression($embedded->xmlElementName),
  115.                 $embeddedExclusion,
  116.                 null !== $embedded->type $this->typeParser->parse($embedded->type) : null
  117.             );
  118.         }
  119.         return $this->checkExpression($embedded);
  120.     }
  121.     private function createExclusion(?Annotation\Exclusion $exclusion null): ?Exclusion
  122.     {
  123.         if (null !== $exclusion) {
  124.             $exclusion $this->parseExclusion($exclusion);
  125.         }
  126.         return $exclusion;
  127.     }
  128. }