PropertyInfoExtractor.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. private $listExtractors;
  21. private $typeExtractors;
  22. private $descriptionExtractors;
  23. private $accessExtractors;
  24. private $initializableExtractors;
  25. /**
  26. * @param iterable|PropertyListExtractorInterface[] $listExtractors
  27. * @param iterable|PropertyTypeExtractorInterface[] $typeExtractors
  28. * @param iterable|PropertyDescriptionExtractorInterface[] $descriptionExtractors
  29. * @param iterable|PropertyAccessExtractorInterface[] $accessExtractors
  30. * @param iterable|PropertyInitializableExtractorInterface[] $initializableExtractors
  31. */
  32. public function __construct(iterable $listExtractors = [], iterable $typeExtractors = [], iterable $descriptionExtractors = [], iterable $accessExtractors = [], iterable $initializableExtractors = [])
  33. {
  34. $this->listExtractors = $listExtractors;
  35. $this->typeExtractors = $typeExtractors;
  36. $this->descriptionExtractors = $descriptionExtractors;
  37. $this->accessExtractors = $accessExtractors;
  38. $this->initializableExtractors = $initializableExtractors;
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function getProperties(string $class, array $context = []): ?array
  44. {
  45. return $this->extract($this->listExtractors, 'getProperties', [$class, $context]);
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function getShortDescription(string $class, string $property, array $context = []): ?string
  51. {
  52. return $this->extract($this->descriptionExtractors, 'getShortDescription', [$class, $property, $context]);
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function getLongDescription(string $class, string $property, array $context = []): ?string
  58. {
  59. return $this->extract($this->descriptionExtractors, 'getLongDescription', [$class, $property, $context]);
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function getTypes(string $class, string $property, array $context = []): ?array
  65. {
  66. return $this->extract($this->typeExtractors, 'getTypes', [$class, $property, $context]);
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function isReadable(string $class, string $property, array $context = []): ?bool
  72. {
  73. return $this->extract($this->accessExtractors, 'isReadable', [$class, $property, $context]);
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public function isWritable(string $class, string $property, array $context = []): ?bool
  79. {
  80. return $this->extract($this->accessExtractors, 'isWritable', [$class, $property, $context]);
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function isInitializable(string $class, string $property, array $context = []): ?bool
  86. {
  87. return $this->extract($this->initializableExtractors, 'isInitializable', [$class, $property, $context]);
  88. }
  89. /**
  90. * Iterates over registered extractors and return the first value found.
  91. *
  92. * @return mixed
  93. */
  94. private function extract(iterable $extractors, string $method, array $arguments)
  95. {
  96. foreach ($extractors as $extractor) {
  97. if (null !== $value = $extractor->{$method}(...$arguments)) {
  98. return $value;
  99. }
  100. }
  101. return null;
  102. }
  103. }