NelmioApiDocBundle/RouteDescriber/FosRestDescriber.php

89 lines
3.2 KiB
PHP
Raw Normal View History

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) {
$in = $annotation instanceof QueryParam ? 'query' : 'formData';
$parameter = $operation->getParameters()->get($annotation->getName(), $in);
2016-08-04 22:27:10 +02:00
$parameter->setRequired(!$annotation->nullable && $annotation->strict);
2016-08-04 22:27:10 +02:00
$parameter->setAllowEmptyValue($annotation->nullable && $annotation->allowBlank);
$parameter->setDefault($annotation->getDefault());
if (null === $parameter->getType()) {
$parameter->setType($annotation->map ? 'array' : 'string');
}
2016-08-04 22:27:10 +02:00
if (null === $parameter->getDescription()) {
$parameter->setDescription($annotation->description);
}
$normalizedRequirements = $this->normalizeRequirements($annotation->requirements);
if (null !== $normalizedRequirements) {
if ($normalizedRequirements instanceof Constraint) {
if ($normalizedRequirements instanceof Regex) {
$format = $normalizedRequirements->getHtmlPattern();
} else {
$reflectionClass = new \ReflectionClass($normalizedRequirements);
$format = $reflectionClass->getShortName();
}
$parameter->setFormat($format);
} else {
$parameter->setPattern($normalizedRequirements);
}
2016-08-04 22:27:10 +02:00
}
}
}
}
private function normalizeRequirements($requirements)
{
// if pattern
if (is_array($requirements) && isset($requirements['rule'])) {
2016-08-04 22:27:10 +02:00
return (string) $requirements['rule'];
}
if (is_string($requirements)) {
return $requirements;
}
// if custom constraint
if ($requirements instanceof Constraint) {
return $requirements;
2016-08-04 22:27:10 +02:00
}
}
}