vendor/vich/uploader-bundle/src/Metadata/MetadataReader.php line 42

Open in your IDE?
  1. <?php
  2. namespace Vich\UploaderBundle\Metadata;
  3. use Metadata\AdvancedMetadataFactoryInterface;
  4. use Vich\UploaderBundle\Exception\MappingNotFoundException;
  5. /**
  6.  * MetadataReader.
  7.  *
  8.  * Exposes a simple interface to read objects metadata.
  9.  *
  10.  * @author Kévin Gomez <contact@kevingomez.fr>
  11.  */
  12. class MetadataReader
  13. {
  14.     /**
  15.      * @var AdvancedMetadataFactoryInterface
  16.      */
  17.     protected $reader;
  18.     /**
  19.      * Constructs a new instance of the MetadataReader.
  20.      *
  21.      * @param AdvancedMetadataFactoryInterface $reader The "low-level" metadata reader
  22.      */
  23.     public function __construct(AdvancedMetadataFactoryInterface $reader)
  24.     {
  25.         $this->reader $reader;
  26.     }
  27.     /**
  28.      * Tells if the given class is uploadable.
  29.      *
  30.      * @param string      $class   The class name to test (FQCN)
  31.      * @param string|null $mapping If given, also checks that the object has the given mapping
  32.      *
  33.      * @throws MappingNotFoundException
  34.      */
  35.     public function isUploadable(string $class, ?string $mapping null): bool
  36.     {
  37.         $metadata $this->reader->getMetadataForClass($class);
  38.         if (null === $metadata) {
  39.             return false;
  40.         }
  41.         if (null === $mapping) {
  42.             return true;
  43.         }
  44.         foreach ($this->getUploadableFields($class) as $fieldMetadata) {
  45.             if ($fieldMetadata['mapping'] === $mapping) {
  46.                 return true;
  47.             }
  48.         }
  49.         return false;
  50.     }
  51.     /**
  52.      * Search for all uploadable classes.
  53.      *
  54.      * @return array|null A list of uploadable class names
  55.      *
  56.      * @throws \RuntimeException
  57.      */
  58.     public function getUploadableClasses(): ?array
  59.     {
  60.         return $this->reader->getAllClassNames();
  61.     }
  62.     /**
  63.      * Attempts to read the uploadable fields.
  64.      *
  65.      * @param string      $class   The class name to test (FQCN)
  66.      * @param string|null $mapping If given, also checks that the object has the given mapping
  67.      *
  68.      * @return array A list of uploadable fields
  69.      *
  70.      * @throws MappingNotFoundException
  71.      */
  72.     public function getUploadableFields(string $class, ?string $mapping null): array
  73.     {
  74.         if (null === $metadata $this->reader->getMetadataForClass($class)) {
  75.             throw MappingNotFoundException::createNotFoundForClass($mapping ?? ''$class);
  76.         }
  77.         $uploadableFields = [];
  78.         /** @var ClassMetadata $classMetadata */
  79.         foreach ($metadata->classMetadata as $classMetadata) {
  80.             $uploadableFields \array_merge($uploadableFields$classMetadata->fields);
  81.         }
  82.         if (null !== $mapping) {
  83.             $uploadableFields \array_filter($uploadableFields, static function (array $fieldMetadata) use ($mapping): bool {
  84.                 return $fieldMetadata['mapping'] === $mapping;
  85.             });
  86.         }
  87.         return $uploadableFields;
  88.     }
  89.     /**
  90.      * Attempts to read the mapping of a specified property.
  91.      *
  92.      * @param string $class The class name to test (FQCN)
  93.      * @param string $field The field
  94.      *
  95.      * @return mixed The field mapping
  96.      *
  97.      * @throws MappingNotFoundException
  98.      */
  99.     public function getUploadableField(string $classstring $field)
  100.     {
  101.         $fieldsMetadata $this->getUploadableFields($class);
  102.         return $fieldsMetadata[$field] ?? null;
  103.     }
  104. }