2016-08-04 22:27:10 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2016-12-29 12:09:26 +01:00
|
|
|
* This file is part of the NelmioApiDocBundle package.
|
2016-08-04 22:27:10 +02:00
|
|
|
*
|
2016-12-29 12:09:26 +01:00
|
|
|
* (c) Nelmio
|
2016-08-04 22:27:10 +02:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2016-12-29 12:09:26 +01:00
|
|
|
namespace Nelmio\ApiDocBundle\RouteDescriber;
|
2016-08-04 22:27:10 +02:00
|
|
|
|
|
|
|
use Doctrine\Common\Annotations\Reader;
|
|
|
|
use EXSyst\Component\Swagger\Swagger;
|
|
|
|
use FOS\RestBundle\Controller\Annotations\QueryParam;
|
2016-08-04 22:34:34 +02:00
|
|
|
use FOS\RestBundle\Controller\Annotations\RequestParam;
|
2016-08-04 22:27:10 +02:00
|
|
|
use Symfony\Component\Routing\Route;
|
|
|
|
use Symfony\Component\Validator\Constraint;
|
|
|
|
use Symfony\Component\Validator\Constraints\Regex;
|
|
|
|
|
2016-11-30 15:04:53 +01:00
|
|
|
final class FosRestDescriber implements RouteDescriberInterface
|
2016-08-04 22:27:10 +02:00
|
|
|
{
|
|
|
|
use RouteDescriberTrait;
|
|
|
|
|
|
|
|
private $annotationReader;
|
|
|
|
|
|
|
|
public function __construct(Reader $annotationReader)
|
|
|
|
{
|
|
|
|
$this->annotationReader = $annotationReader;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function describe(Swagger $api, Route $route, \ReflectionMethod $reflectionMethod)
|
|
|
|
{
|
2016-08-04 22:36:20 +02:00
|
|
|
$annotations = $this->annotationReader->getMethodAnnotations($reflectionMethod);
|
2016-08-04 22:27:10 +02:00
|
|
|
$annotations = array_filter($annotations, function ($value) {
|
|
|
|
return $value instanceof RequestParam || $value instanceof QueryParam;
|
|
|
|
});
|
|
|
|
|
|
|
|
foreach ($this->getOperations($api, $route) as $operation) {
|
|
|
|
foreach ($annotations as $annotation) {
|
2018-06-01 16:50:50 +02:00
|
|
|
if ($annotation instanceof QueryParam) {
|
|
|
|
$parameter = $operation->getParameters()->get($annotation->getName(), 'query');
|
|
|
|
$parameter->setAllowEmptyValue($annotation->nullable && $annotation->allowBlank);
|
2018-08-30 00:41:53 +02:00
|
|
|
|
|
|
|
$parameter->setRequired(!$annotation->nullable && $annotation->strict);
|
2018-06-01 16:50:50 +02:00
|
|
|
} else {
|
|
|
|
$body = $operation->getParameters()->get('body', 'body')->getSchema();
|
|
|
|
$body->setType('object');
|
|
|
|
$parameter = $body->getProperties()->get($annotation->getName());
|
2018-08-30 00:41:53 +02:00
|
|
|
|
|
|
|
if (!$annotation->nullable && $annotation->strict) {
|
|
|
|
$requiredParameters = $body->getRequired();
|
|
|
|
$requiredParameters[] = $annotation->getName();
|
|
|
|
|
|
|
|
$body->setRequired(array_values(array_unique($requiredParameters)));
|
|
|
|
}
|
2018-06-01 16:50:50 +02:00
|
|
|
}
|
2016-08-04 22:27:10 +02:00
|
|
|
|
|
|
|
$parameter->setDefault($annotation->getDefault());
|
2019-04-20 13:18:00 +02:00
|
|
|
if (null !== $parameter->getType()) {
|
|
|
|
continue;
|
2017-07-05 14:28:48 +02:00
|
|
|
}
|
2019-04-20 13:18:00 +02:00
|
|
|
|
2016-08-04 22:27:10 +02:00
|
|
|
if (null === $parameter->getDescription()) {
|
|
|
|
$parameter->setDescription($annotation->description);
|
|
|
|
}
|
2019-04-20 13:18:00 +02:00
|
|
|
|
|
|
|
if ($annotation->map) {
|
|
|
|
$parameter->setType('array');
|
|
|
|
$parameter = $parameter->getItems();
|
|
|
|
}
|
|
|
|
|
|
|
|
$parameter->setType('string');
|
2016-08-04 22:27:10 +02:00
|
|
|
|
2018-03-23 19:46:54 +01:00
|
|
|
$pattern = $this->getPattern($annotation->requirements);
|
|
|
|
if (null !== $pattern) {
|
|
|
|
$parameter->setPattern($pattern);
|
|
|
|
}
|
|
|
|
|
|
|
|
$format = $this->getFormat($annotation->requirements);
|
|
|
|
if (null !== $format) {
|
|
|
|
$parameter->setFormat($format);
|
2016-08-04 22:27:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-23 19:46:54 +01:00
|
|
|
private function getPattern($requirements)
|
2016-08-04 22:27:10 +02:00
|
|
|
{
|
2017-08-10 12:16:50 +02:00
|
|
|
if (is_array($requirements) && isset($requirements['rule'])) {
|
2016-08-04 22:27:10 +02:00
|
|
|
return (string) $requirements['rule'];
|
|
|
|
}
|
2018-03-23 19:46:54 +01:00
|
|
|
|
2016-08-04 22:27:10 +02:00
|
|
|
if (is_string($requirements)) {
|
|
|
|
return $requirements;
|
|
|
|
}
|
2018-03-23 19:46:54 +01:00
|
|
|
|
|
|
|
if ($requirements instanceof Regex) {
|
|
|
|
return $requirements->getHtmlPattern();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getFormat($requirements)
|
|
|
|
{
|
|
|
|
if ($requirements instanceof Constraint && !$requirements instanceof Regex) {
|
|
|
|
$reflectionClass = new \ReflectionClass($requirements);
|
|
|
|
|
|
|
|
return $reflectionClass->getShortName();
|
2016-08-04 22:27:10 +02:00
|
|
|
}
|
2018-03-23 19:46:54 +01:00
|
|
|
|
|
|
|
return null;
|
2016-08-04 22:27:10 +02:00
|
|
|
}
|
|
|
|
}
|