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;
|
|
|
|
use EXSyst\Component\Swagger\Schema;
|
2021-03-12 00:59:35 +01:00
|
|
|
use Symfony\Component\Validator\Constraint;
|
2018-01-25 14:59:48 +01:00
|
|
|
use Symfony\Component\Validator\Constraints as Assert;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
class SymfonyConstraintAnnotationReader
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var Reader
|
|
|
|
*/
|
|
|
|
private $annotationsReader;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Schema
|
|
|
|
*/
|
|
|
|
private $schema;
|
|
|
|
|
|
|
|
public function __construct(Reader $annotationsReader)
|
|
|
|
{
|
|
|
|
$this->annotationsReader = $annotationsReader;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the given property and schema with defined Symfony constraints.
|
2021-03-12 00:59:35 +01:00
|
|
|
*
|
|
|
|
* @param \ReflectionProperty|\ReflectionMethod $reflection
|
2018-01-25 14:59:48 +01:00
|
|
|
*/
|
2020-07-12 14:54:39 +02:00
|
|
|
public function updateProperty($reflection, Schema $property)
|
2018-01-25 14:59:48 +01:00
|
|
|
{
|
2021-03-12 00:59:35 +01:00
|
|
|
foreach ($this->getAnnotations($reflection) as $annotation) {
|
2018-01-25 14:59:48 +01:00
|
|
|
if ($annotation instanceof Assert\NotBlank || $annotation instanceof Assert\NotNull) {
|
2020-09-20 20:38:26 +02:00
|
|
|
// To support symfony/validator < 4.3
|
2020-09-28 22:45:24 +03:00
|
|
|
if ($annotation instanceof Assert\NotBlank && \property_exists($annotation, 'allowNull') && $annotation->allowNull) {
|
2020-08-31 23:22:42 +03:00
|
|
|
// The field is optional
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-08-30 00:32:11 +02:00
|
|
|
$existingRequiredFields = $this->schema->getRequired() ?? [];
|
2018-09-11 13:42:50 +03:00
|
|
|
$existingRequiredFields[] = $propertyName;
|
2018-08-30 00:32:11 +02:00
|
|
|
|
|
|
|
$this->schema->setRequired(array_values(array_unique($existingRequiredFields)));
|
|
|
|
} elseif ($annotation instanceof Assert\Length) {
|
2018-01-25 14:59:48 +01:00
|
|
|
$property->setMinLength($annotation->min);
|
|
|
|
$property->setMaxLength($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) {
|
2018-01-25 14:59:48 +01:00
|
|
|
$property->setMinItems($annotation->min);
|
|
|
|
$property->setMaxItems($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-03-13 16:59:56 +03:00
|
|
|
$property->setEnum(array_values($values));
|
2018-12-19 15:41:27 +01:00
|
|
|
} elseif ($annotation instanceof Assert\Range) {
|
|
|
|
$property->setMinimum($annotation->min);
|
|
|
|
$property->setMaximum($annotation->max);
|
|
|
|
} elseif ($annotation instanceof Assert\LessThan) {
|
|
|
|
$property->setExclusiveMaximum($annotation->value);
|
|
|
|
} elseif ($annotation instanceof Assert\LessThanOrEqual) {
|
|
|
|
$property->setMaximum($annotation->value);
|
2018-01-25 14:59:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setSchema($schema)
|
|
|
|
{
|
|
|
|
$this->schema = $schema;
|
|
|
|
}
|
|
|
|
|
2018-09-11 13:42:50 +03:00
|
|
|
/**
|
|
|
|
* Get assigned property name for property schema.
|
|
|
|
*/
|
|
|
|
private function getSchemaPropertyName(Schema $property)
|
|
|
|
{
|
|
|
|
if (null === $this->schema) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->schema->getProperties() as $name => $schemaProperty) {
|
|
|
|
if ($schemaProperty === $property) {
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-01-25 14:59:48 +01:00
|
|
|
/**
|
|
|
|
* Append the pattern from the constraint to the existing pattern.
|
|
|
|
*/
|
2018-01-26 17:09:38 +01:00
|
|
|
private function appendPattern(Schema $property, $newPattern)
|
2018-01-25 14:59:48 +01:00
|
|
|
{
|
2018-01-26 17:09:38 +01:00
|
|
|
if (null === $newPattern) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-25 14:59:48 +01:00
|
|
|
if (null !== $property->getPattern()) {
|
|
|
|
$property->setPattern(sprintf('%s, %s', $property->getPattern(), $newPattern));
|
|
|
|
} else {
|
|
|
|
$property->setPattern($newPattern);
|
|
|
|
}
|
|
|
|
}
|
2021-03-12 00:59:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \ReflectionProperty|\ReflectionMethod $reflection
|
|
|
|
*/
|
|
|
|
private function getAnnotations($reflection): \Traversable
|
|
|
|
{
|
|
|
|
if (\PHP_VERSION_ID >= 80000) {
|
|
|
|
foreach ($reflection->getAttributes(Constraint::class, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) {
|
|
|
|
yield $attribute->newInstance();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($reflection instanceof \ReflectionProperty) {
|
|
|
|
yield from $this->annotationsReader->getPropertyAnnotations($reflection);
|
|
|
|
} elseif ($reflection instanceof \ReflectionMethod) {
|
|
|
|
yield from $this->annotationsReader->getMethodAnnotations($reflection);
|
|
|
|
}
|
|
|
|
}
|
2018-01-25 14:59:48 +01:00
|
|
|
}
|