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

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