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

  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.  * @internal
  13.  */
  14. final class MetadataReader
  15. {
  16.     /**
  17.      * Constructs a new instance of the MetadataReader.
  18.      *
  19.      * @param AdvancedMetadataFactoryInterface $reader The "low-level" metadata reader
  20.      */
  21.     public function __construct(private readonly AdvancedMetadataFactoryInterface $reader)
  22.     {
  23.     }
  24.     /**
  25.      * Tells if the given class is uploadable.
  26.      *
  27.      * @param string      $class   The class name to test (FQCN)
  28.      * @param string|null $mapping If given, also checks that the object has the given mapping
  29.      *
  30.      * @throws MappingNotFoundException
  31.      */
  32.     public function isUploadable(string $class, ?string $mapping null): bool
  33.     {
  34.         $metadata $this->reader->getMetadataForClass($class);
  35.         if (null === $metadata) {
  36.             return false;
  37.         }
  38.         if (null === $mapping) {
  39.             return true;
  40.         }
  41.         foreach ($this->getUploadableFields($class) as $fieldMetadata) {
  42.             if ($fieldMetadata['mapping'] === $mapping) {
  43.                 return true;
  44.             }
  45.         }
  46.         return false;
  47.     }
  48.     /**
  49.      * Search for all uploadable classes.
  50.      *
  51.      * @return array|null A list of uploadable class names
  52.      *
  53.      * @throws \RuntimeException
  54.      */
  55.     public function getUploadableClasses(): ?array
  56.     {
  57.         return $this->reader->getAllClassNames();
  58.     }
  59.     /**
  60.      * Attempts to read the uploadable fields.
  61.      *
  62.      * @param string      $class   The class name to test (FQCN)
  63.      * @param string|null $mapping If given, also checks that the object has the given mapping
  64.      *
  65.      * @return array A list of uploadable fields
  66.      *
  67.      * @throws MappingNotFoundException
  68.      */
  69.     public function getUploadableFields(string $class, ?string $mapping null): array
  70.     {
  71.         if (null === $metadata $this->reader->getMetadataForClass($class)) {
  72.             throw MappingNotFoundException::createNotFoundForClass($mapping ?? ''$class);
  73.         }
  74.         $uploadableFields = [];
  75.         /** @var ClassMetadata $classMetadata */
  76.         foreach ($metadata->classMetadata as $classMetadata) {
  77.             $uploadableFields \array_merge($uploadableFields$classMetadata->fields);
  78.         }
  79.         if (null !== $mapping) {
  80.             $uploadableFields \array_filter($uploadableFields, static fn (array $fieldMetadata): bool => $fieldMetadata['mapping'] === $mapping);
  81.         }
  82.         return $uploadableFields;
  83.     }
  84.     /**
  85.      * Attempts to read the mapping of a specified property.
  86.      *
  87.      * @param string $class The class name to test (FQCN)
  88.      * @param string $field The field
  89.      *
  90.      * @return mixed The field mapping
  91.      *
  92.      * @throws MappingNotFoundException
  93.      */
  94.     public function getUploadableField(string $classstring $field)
  95.     {
  96.         $fieldsMetadata $this->getUploadableFields($class);
  97.         return $fieldsMetadata[$field] ?? null;
  98.     }
  99. }