vendor/nelmio/api-doc-bundle/src/PropertyDescriber/RequiredPropertyDescriber.php line 24

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 OpenApi\Annotations as OA;
  12. use OpenApi\Generator;
  13. /**
  14. * Mark a property as required if it is not nullable.
  15. */
  16. final class RequiredPropertyDescriber implements PropertyDescriberInterface, PropertyDescriberAwareInterface
  17. {
  18. use PropertyDescriberAwareTrait;
  19. public function describe(array $types, OA\Schema $property, array $groups = null, ?OA\Schema $schema = null, array $context = [])
  20. {
  21. $this->propertyDescriber->describe($types, $property, $groups, $schema, $context);
  22. if (!$property instanceof OA\Property) {
  23. return;
  24. }
  25. if (null === $schema) {
  26. return;
  27. }
  28. if (true === $property->nullable || !Generator::isDefault($property->default)) {
  29. return;
  30. }
  31. $existingRequiredFields = Generator::UNDEFINED !== $schema->required ? $schema->required : [];
  32. $existingRequiredFields[] = $property->property;
  33. $schema->required = array_values(array_unique($existingRequiredFields));
  34. }
  35. public function supports(array $types): bool
  36. {
  37. return true;
  38. }
  39. }