ConstructorExtractor.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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\Extractor;
  11. use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
  12. use Symfony\Component\TypeInfo\Type;
  13. /**
  14. * Extracts the constructor argument type using ConstructorArgumentTypeExtractorInterface implementations.
  15. *
  16. * @author Dmitrii Poddubnyi <dpoddubny@gmail.com>
  17. */
  18. final class ConstructorExtractor implements PropertyTypeExtractorInterface
  19. {
  20. /**
  21. * @param iterable<int, ConstructorArgumentTypeExtractorInterface> $extractors
  22. */
  23. public function __construct(
  24. private readonly iterable $extractors = [],
  25. ) {
  26. }
  27. public function getType(string $class, string $property, array $context = []): ?Type
  28. {
  29. foreach ($this->extractors as $extractor) {
  30. if (null !== $value = $extractor->getTypeFromConstructor($class, $property)) {
  31. return $value;
  32. }
  33. }
  34. return null;
  35. }
  36. public function getTypes(string $class, string $property, array $context = []): ?array
  37. {
  38. foreach ($this->extractors as $extractor) {
  39. $value = $extractor->getTypesFromConstructor($class, $property);
  40. if (null !== $value) {
  41. return $value;
  42. }
  43. }
  44. return null;
  45. }
  46. }