vendor/api-platform/core/src/Metadata/Property/Factory/ExtractorPropertyNameCollectionFactory.php line 33

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the API Platform project.
  4. *
  5. * (c) Kévin Dunglas <dunglas@gmail.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. declare(strict_types=1);
  11. namespace ApiPlatform\Metadata\Property\Factory;
  12. use ApiPlatform\Exception\InvalidArgumentException;
  13. use ApiPlatform\Exception\ResourceClassNotFoundException;
  14. use ApiPlatform\Metadata\Extractor\PropertyExtractorInterface;
  15. use ApiPlatform\Metadata\Property\PropertyNameCollection;
  16. /**
  17. * Creates a property name collection using an extractor.
  18. *
  19. * @author Kévin Dunglas <dunglas@gmail.com>
  20. * @author Baptiste Meyer <baptiste.meyer@gmail.com>
  21. * @author Vincent Chalamon <vincentchalamon@gmail.com>
  22. */
  23. final class ExtractorPropertyNameCollectionFactory implements PropertyNameCollectionFactoryInterface
  24. {
  25. private $extractor;
  26. private $decorated;
  27. public function __construct(PropertyExtractorInterface $extractor, PropertyNameCollectionFactoryInterface $decorated = null)
  28. {
  29. $this->extractor = $extractor;
  30. $this->decorated = $decorated;
  31. }
  32. /**
  33. * {@inheritdoc}
  34. *
  35. * @throws InvalidArgumentException
  36. */
  37. public function create(string $resourceClass, array $options = []): PropertyNameCollection
  38. {
  39. $propertyNames = [];
  40. $propertyNameCollection = null;
  41. if ($this->decorated) {
  42. try {
  43. $propertyNameCollection = $this->decorated->create($resourceClass, $options);
  44. } catch (ResourceClassNotFoundException $resourceClassNotFoundException) {
  45. // Ignore not found exceptions from decorated factory
  46. }
  47. foreach ($propertyNameCollection as $propertyName) {
  48. $propertyNames[$propertyName] = $propertyName;
  49. }
  50. }
  51. if (!class_exists($resourceClass) && !interface_exists($resourceClass)) {
  52. if (null !== $propertyNameCollection) {
  53. return $propertyNameCollection;
  54. }
  55. throw new ResourceClassNotFoundException(sprintf('The resource class "%s" does not exist.', $resourceClass));
  56. }
  57. if ($properties = $this->extractor->getProperties()[$resourceClass] ?? false) {
  58. foreach ($properties as $propertyName => $property) {
  59. $propertyNames[$propertyName] = $propertyName;
  60. }
  61. }
  62. return new PropertyNameCollection(array_values($propertyNames));
  63. }
  64. }
  65. class_alias(ExtractorPropertyNameCollectionFactory::class, \ApiPlatform\Core\Metadata\Property\Factory\ExtractorPropertyNameCollectionFactory::class);