vendor/nelmio/api-doc-bundle/PropertyDescriber/ArrayPropertyDescriber.php line 33

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the NelmioApiDocBundle package.
  4. *
  5. * (c) Nelmio
  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 Nelmio\ApiDocBundle\PropertyDescriber;
  11. use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareInterface;
  12. use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareTrait;
  13. use Nelmio\ApiDocBundle\Exception\UndocumentedArrayItemsException;
  14. use Nelmio\ApiDocBundle\OpenApiPhp\Util;
  15. use OpenApi\Annotations as OA;
  16. class ArrayPropertyDescriber implements PropertyDescriberInterface, ModelRegistryAwareInterface
  17. {
  18. use ModelRegistryAwareTrait;
  19. use NullablePropertyTrait;
  20. /** @var PropertyDescriberInterface[] */
  21. private $propertyDescribers;
  22. public function __construct(iterable $propertyDescribers = [])
  23. {
  24. $this->propertyDescribers = $propertyDescribers;
  25. }
  26. public function describe(array $types, OA\Schema $property, array $groups = null)
  27. {
  28. // BC layer for symfony < 5.3
  29. $type = method_exists($types[0], 'getCollectionValueTypes') ?
  30. ($types[0]->getCollectionValueTypes()[0] ?? null) :
  31. $types[0]->getCollectionValueType();
  32. if (null === $type) {
  33. throw new UndocumentedArrayItemsException();
  34. }
  35. $property->type = 'array';
  36. $this->setNullableProperty($types[0], $property);
  37. $property = Util::getChild($property, OA\Items::class);
  38. foreach ($this->propertyDescribers as $propertyDescriber) {
  39. if ($propertyDescriber instanceof ModelRegistryAwareInterface) {
  40. $propertyDescriber->setModelRegistry($this->modelRegistry);
  41. }
  42. if ($propertyDescriber->supports([$type])) {
  43. try {
  44. $propertyDescriber->describe([$type], $property, $groups);
  45. } catch (UndocumentedArrayItemsException $e) {
  46. if (null !== $e->getClass()) {
  47. throw $e; // This exception is already complete
  48. }
  49. throw new UndocumentedArrayItemsException(null, sprintf('%s[]', $e->getPath()));
  50. }
  51. break;
  52. }
  53. }
  54. }
  55. public function supports(array $types): bool
  56. {
  57. return 1 === count($types) && $types[0]->isCollection();
  58. }
  59. }