vendor/symfony/property-info/PropertyInfoExtractor.php line 64

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\PropertyInfo;
  11. /**
  12. * Default {@see PropertyInfoExtractorInterface} implementation.
  13. *
  14. * @author Kévin Dunglas <dunglas@gmail.com>
  15. *
  16. * @final
  17. */
  18. class PropertyInfoExtractor implements PropertyInfoExtractorInterface, PropertyInitializableExtractorInterface
  19. {
  20. /**
  21. * @param iterable<mixed, PropertyListExtractorInterface> $listExtractors
  22. * @param iterable<mixed, PropertyTypeExtractorInterface> $typeExtractors
  23. * @param iterable<mixed, PropertyDescriptionExtractorInterface> $descriptionExtractors
  24. * @param iterable<mixed, PropertyAccessExtractorInterface> $accessExtractors
  25. * @param iterable<mixed, PropertyInitializableExtractorInterface> $initializableExtractors
  26. */
  27. public function __construct(
  28. private readonly iterable $listExtractors = [],
  29. private readonly iterable $typeExtractors = [],
  30. private readonly iterable $descriptionExtractors = [],
  31. private readonly iterable $accessExtractors = [],
  32. private readonly iterable $initializableExtractors = [],
  33. ) {
  34. }
  35. public function getProperties(string $class, array $context = []): ?array
  36. {
  37. return $this->extract($this->listExtractors, 'getProperties', [$class, $context]);
  38. }
  39. public function getShortDescription(string $class, string $property, array $context = []): ?string
  40. {
  41. return $this->extract($this->descriptionExtractors, 'getShortDescription', [$class, $property, $context]);
  42. }
  43. public function getLongDescription(string $class, string $property, array $context = []): ?string
  44. {
  45. return $this->extract($this->descriptionExtractors, 'getLongDescription', [$class, $property, $context]);
  46. }
  47. public function getTypes(string $class, string $property, array $context = []): ?array
  48. {
  49. return $this->extract($this->typeExtractors, 'getTypes', [$class, $property, $context]);
  50. }
  51. public function isReadable(string $class, string $property, array $context = []): ?bool
  52. {
  53. return $this->extract($this->accessExtractors, 'isReadable', [$class, $property, $context]);
  54. }
  55. public function isWritable(string $class, string $property, array $context = []): ?bool
  56. {
  57. return $this->extract($this->accessExtractors, 'isWritable', [$class, $property, $context]);
  58. }
  59. public function isInitializable(string $class, string $property, array $context = []): ?bool
  60. {
  61. return $this->extract($this->initializableExtractors, 'isInitializable', [$class, $property, $context]);
  62. }
  63. /**
  64. * Iterates over registered extractors and return the first value found.
  65. *
  66. * @param iterable<mixed, object> $extractors
  67. * @param list<mixed> $arguments
  68. */
  69. private function extract(iterable $extractors, string $method, array $arguments): mixed
  70. {
  71. foreach ($extractors as $extractor) {
  72. if (null !== $value = $extractor->{$method}(...$arguments)) {
  73. return $value;
  74. }
  75. }
  76. return null;
  77. }
  78. }