2018-01-25 14:59:48 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the NelmioApiDocBundle package.
|
|
|
|
*
|
|
|
|
* (c) Nelmio
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Nelmio\ApiDocBundle\ModelDescriber\Annotations;
|
|
|
|
|
|
|
|
use Doctrine\Common\Annotations\Reader;
|
2020-05-28 13:19:11 +02:00
|
|
|
use OpenApi\Annotations as OA;
|
2018-01-25 14:59:48 +01:00
|
|
|
use Symfony\Component\Validator\Constraints as Assert;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
class SymfonyConstraintAnnotationReader
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var Reader
|
|
|
|
*/
|
|
|
|
private $annotationsReader;
|
|
|
|
|
|
|
|
/**
|
2020-05-28 13:19:11 +02:00
|
|
|
* @var OA\Schema
|
2018-01-25 14:59:48 +01:00
|
|
|
*/
|
|
|
|
private $schema;
|
|
|
|
|
|
|
|
public function __construct(Reader $annotationsReader)
|
|
|
|
{
|
|
|
|
$this->annotationsReader = $annotationsReader;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the given property and schema with defined Symfony constraints.
|
|
|
|
*/
|
2020-07-12 15:04:20 +02:00
|
|
|
public function updateProperty($reflection, OA\Property $property): void
|
2018-01-25 14:59:48 +01:00
|
|
|
{
|
2020-07-12 14:54:39 +02:00
|
|
|
if ($reflection instanceof \ReflectionProperty) {
|
|
|
|
$annotations = $this->annotationsReader->getPropertyAnnotations($reflection);
|
|
|
|
} else {
|
|
|
|
$annotations = $this->annotationsReader->getMethodAnnotations($reflection);
|
|
|
|
}
|
2018-01-25 14:59:48 +01:00
|
|
|
|
|
|
|
foreach ($annotations as $annotation) {
|
|
|
|
if ($annotation instanceof Assert\NotBlank || $annotation instanceof Assert\NotNull) {
|
2018-08-30 00:32:11 +02:00
|
|
|
// The field is required
|
|
|
|
if (null === $this->schema) {
|
|
|
|
continue;
|
2018-01-25 14:59:48 +01:00
|
|
|
}
|
|
|
|
|
2018-09-11 13:42:50 +03:00
|
|
|
$propertyName = $this->getSchemaPropertyName($property);
|
|
|
|
if (null === $propertyName) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-05-28 13:19:11 +02:00
|
|
|
$existingRequiredFields = OA\UNDEFINED !== $this->schema->required ? $this->schema->required : [];
|
2018-09-11 13:42:50 +03:00
|
|
|
$existingRequiredFields[] = $propertyName;
|
2018-08-30 00:32:11 +02:00
|
|
|
|
2020-05-28 13:19:11 +02:00
|
|
|
$this->schema->required = array_values(array_unique($existingRequiredFields));
|
2018-08-30 00:32:11 +02:00
|
|
|
} elseif ($annotation instanceof Assert\Length) {
|
2020-07-06 19:50:34 +02:00
|
|
|
$property->minLength = (int) $annotation->min;
|
|
|
|
$property->maxLength = (int) $annotation->max;
|
2018-08-30 00:32:11 +02:00
|
|
|
} elseif ($annotation instanceof Assert\Regex) {
|
2018-01-25 14:59:48 +01:00
|
|
|
$this->appendPattern($property, $annotation->getHtmlPattern());
|
2018-08-30 00:32:11 +02:00
|
|
|
} elseif ($annotation instanceof Assert\Count) {
|
2020-07-06 19:50:34 +02:00
|
|
|
$property->minItems = (int) $annotation->min;
|
|
|
|
$property->maxItems = (int) $annotation->max;
|
2018-08-30 00:32:11 +02:00
|
|
|
} elseif ($annotation instanceof Assert\Choice) {
|
2020-07-12 14:54:39 +02:00
|
|
|
$values = $annotation->callback ? call_user_func(is_array($annotation->callback) ? $annotation->callback : [$reflection->class, $annotation->callback]) : $annotation->choices;
|
2020-05-28 13:19:11 +02:00
|
|
|
$property->enum = array_values($values);
|
2018-12-19 15:41:27 +01:00
|
|
|
} elseif ($annotation instanceof Assert\Range) {
|
2020-07-06 19:50:34 +02:00
|
|
|
$property->minimum = (int) $annotation->min;
|
|
|
|
$property->maximum = (int) $annotation->max;
|
2018-12-19 15:41:27 +01:00
|
|
|
} elseif ($annotation instanceof Assert\LessThan) {
|
2020-07-06 19:50:34 +02:00
|
|
|
$property->exclusiveMaximum = true;
|
|
|
|
$property->maximum = (int) $annotation->value;
|
2018-12-19 15:41:27 +01:00
|
|
|
} elseif ($annotation instanceof Assert\LessThanOrEqual) {
|
2020-07-06 19:50:34 +02:00
|
|
|
$property->maximum = (int) $annotation->value;
|
2018-01-25 14:59:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 13:19:11 +02:00
|
|
|
public function setSchema($schema): void
|
2018-01-25 14:59:48 +01:00
|
|
|
{
|
|
|
|
$this->schema = $schema;
|
|
|
|
}
|
|
|
|
|
2018-09-11 13:42:50 +03:00
|
|
|
/**
|
|
|
|
* Get assigned property name for property schema.
|
|
|
|
*/
|
2020-05-28 13:19:11 +02:00
|
|
|
private function getSchemaPropertyName(OA\Schema $property): ?string
|
2018-09-11 13:42:50 +03:00
|
|
|
{
|
|
|
|
if (null === $this->schema) {
|
|
|
|
return null;
|
|
|
|
}
|
2020-05-28 13:19:11 +02:00
|
|
|
foreach ($this->schema->properties as $schemaProperty) {
|
2018-09-11 13:42:50 +03:00
|
|
|
if ($schemaProperty === $property) {
|
2020-05-28 13:19:11 +02:00
|
|
|
return OA\UNDEFINED !== $schemaProperty->property ? $schemaProperty->property : null;
|
2018-09-11 13:42:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-01-25 14:59:48 +01:00
|
|
|
/**
|
|
|
|
* Append the pattern from the constraint to the existing pattern.
|
|
|
|
*/
|
2020-05-28 13:19:11 +02:00
|
|
|
private function appendPattern(OA\Schema $property, $newPattern): void
|
2018-01-25 14:59:48 +01:00
|
|
|
{
|
2018-01-26 17:09:38 +01:00
|
|
|
if (null === $newPattern) {
|
|
|
|
return;
|
|
|
|
}
|
2020-05-28 13:19:11 +02:00
|
|
|
if (OA\UNDEFINED !== $property->pattern) {
|
|
|
|
$property->pattern = sprintf('%s, %s', $property->pattern, $newPattern);
|
2018-01-25 14:59:48 +01:00
|
|
|
} else {
|
2020-05-28 13:19:11 +02:00
|
|
|
$property->pattern = $newPattern;
|
2018-01-25 14:59:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|