vendor/vich/uploader-bundle/src/Metadata/Driver/AttributeReader.php line 116

Open in your IDE?
  1. <?php
  2. namespace Vich\UploaderBundle\Metadata\Driver;
  3. use Vich\UploaderBundle\Mapping\AttributeInterface;
  4. /**
  5.  * @internal
  6.  */
  7. final class AttributeReader
  8. {
  9.     /** @return AttributeInterface[] */
  10.     public function getClassAttributes(\ReflectionClass $class): array
  11.     {
  12.         return $this->convertToAttributeInstances($class->getAttributes());
  13.     }
  14.     public function getClassAttribute(\ReflectionClass $classstring $attributeName): ?AttributeInterface
  15.     {
  16.         return $this->getClassAttributes($class)[$attributeName] ?? null;
  17.     }
  18.     /** @return AttributeInterface[] */
  19.     public function getMethodAttributes(\ReflectionMethod $method): array
  20.     {
  21.         return $this->convertToAttributeInstances($method->getAttributes());
  22.     }
  23.     public function getMethodAttribute(\ReflectionMethod $methodstring $attributeName): ?AttributeInterface
  24.     {
  25.         return $this->getMethodAttributes($method)[$attributeName] ?? null;
  26.     }
  27.     /** @return AttributeInterface[] */
  28.     public function getPropertyAttributes(\ReflectionProperty $property): array
  29.     {
  30.         return $this->convertToAttributeInstances($property->getAttributes());
  31.     }
  32.     public function getPropertyAttribute(\ReflectionProperty $propertystring $attributeName): ?AttributeInterface
  33.     {
  34.         return $this->getPropertyAttributes($property)[$attributeName] ?? null;
  35.     }
  36.     /**
  37.      * @deprecated since 2.9, use getClassAttributes() instead
  38.      *
  39.      * @return AttributeInterface[]
  40.      */
  41.     public function getClassAnnotations(\ReflectionClass $class): array
  42.     {
  43.         trigger_deprecation('vich/uploader-bundle''2.9''Method "%s" is deprecated, use "getClassAttributes()" instead.'__METHOD__);
  44.         return $this->getClassAttributes($class);
  45.     }
  46.     /**
  47.      * @deprecated since 2.9, use getClassAttribute() instead
  48.      */
  49.     public function getClassAnnotation(\ReflectionClass $classstring $annotationName): ?AttributeInterface
  50.     {
  51.         trigger_deprecation('vich/uploader-bundle''2.9''Method "%s" is deprecated, use "getClassAttribute()" instead.'__METHOD__);
  52.         return $this->getClassAttribute($class$annotationName);
  53.     }
  54.     /**
  55.      * @deprecated since 2.9, use getMethodAttributes() instead
  56.      *
  57.      * @return AttributeInterface[]
  58.      */
  59.     public function getMethodAnnotations(\ReflectionMethod $method): array
  60.     {
  61.         trigger_deprecation('vich/uploader-bundle''2.9''Method "%s" is deprecated, use "getMethodAttributes()" instead.'__METHOD__);
  62.         return $this->getMethodAttributes($method);
  63.     }
  64.     /**
  65.      * @deprecated since 2.9, use getMethodAttribute() instead
  66.      */
  67.     public function getMethodAnnotation(\ReflectionMethod $methodstring $annotationName): ?AttributeInterface
  68.     {
  69.         trigger_deprecation('vich/uploader-bundle''2.9''Method "%s" is deprecated, use "getMethodAttribute()" instead.'__METHOD__);
  70.         return $this->getMethodAttribute($method$annotationName);
  71.     }
  72.     /**
  73.      * @deprecated since 2.9, use getPropertyAttributes() instead
  74.      *
  75.      * @return AttributeInterface[]
  76.      */
  77.     public function getPropertyAnnotations(\ReflectionProperty $property): array
  78.     {
  79.         trigger_deprecation('vich/uploader-bundle''2.9''Method "%s" is deprecated, use "getPropertyAttributes()" instead.'__METHOD__);
  80.         return $this->getPropertyAttributes($property);
  81.     }
  82.     /**
  83.      * @deprecated since 2.9, use getPropertyAttribute() instead
  84.      */
  85.     public function getPropertyAnnotation(\ReflectionProperty $propertystring $annotationName): ?AttributeInterface
  86.     {
  87.         trigger_deprecation('vich/uploader-bundle''2.9''Method "%s" is deprecated, use "getPropertyAttribute()" instead.'__METHOD__);
  88.         return $this->getPropertyAttribute($property$annotationName);
  89.     }
  90.     /**
  91.      * @param \ReflectionAttribute[] $attributes
  92.      *
  93.      * @return AttributeInterface[]
  94.      */
  95.     private function convertToAttributeInstances(array $attributes): array
  96.     {
  97.         $instances = [];
  98.         foreach ($attributes as $attribute) {
  99.             $attributeName $attribute->getName();
  100.             $instance $attribute->newInstance();
  101.             if (!$instance instanceof AttributeInterface) {
  102.                 continue;
  103.             }
  104.             $instances[$attributeName] = $instance;
  105.         }
  106.         return $instances;
  107.     }
  108. }