NelmioApiDocBundle/Extractor/Handler/FosRestHandler.php

78 lines
2.9 KiB
PHP
Raw Normal View History

<?php
/*
* This file is part of the NelmioApiDocBundle.
*
* (c) Nelmio <hello@nelm.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\Extractor\Handler;
use Nelmio\ApiDocBundle\Extractor\HandlerInterface;
2013-04-16 13:46:15 +02:00
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Symfony\Component\Routing\Route;
2014-04-17 14:06:41 +02:00
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Regex;
2013-04-17 13:41:28 +02:00
use FOS\RestBundle\Controller\Annotations\RequestParam;
use FOS\RestBundle\Controller\Annotations\QueryParam;
2013-04-16 13:46:15 +02:00
class FosRestHandler implements HandlerInterface
{
2014-04-17 14:06:41 +02:00
/**
* @inheritdoc
*/
2013-04-17 14:24:45 +02:00
public function handle(ApiDoc $annotation, array $annotations, Route $route, \ReflectionMethod $method)
{
foreach ($annotations as $annot) {
2013-04-17 13:41:28 +02:00
if ($annot instanceof RequestParam) {
2013-04-16 13:46:15 +02:00
$annotation->addParameter($annot->name, array(
'required' => $annot->strict && $annot->default === null,
2014-04-17 14:06:41 +02:00
'dataType' => $this->handleRequirements($annot->requirements),
2013-04-16 13:46:15 +02:00
'description' => $annot->description,
'readonly' => false
));
2013-04-17 13:41:28 +02:00
} elseif ($annot instanceof QueryParam) {
if ($annot->strict && $annot->nullable === false && $annot->default === null) {
$annotation->addRequirement($annot->name, array(
2014-04-17 14:06:41 +02:00
'requirement' => $this->handleRequirements($annot->requirements),
'dataType' => '',
'description' => $annot->description,
));
2013-11-14 10:28:42 +01:00
} elseif ($annot->default !== null) {
2013-04-30 16:21:12 +04:00
$annotation->addFilter($annot->name, array(
2014-04-17 14:06:41 +02:00
'requirement' => $this->handleRequirements($annot->requirements),
2013-04-30 16:21:12 +04:00
'description' => $annot->description,
'default' => $annot->default,
));
} else {
$annotation->addFilter($annot->name, array(
2014-04-17 14:06:41 +02:00
'requirement' => $this->handleRequirements($annot->requirements),
'description' => $annot->description,
));
}
}
}
}
2014-04-17 14:06:41 +02:00
/**
* Handle FOSRestBundle requirements in order to return a string.
*
* @param mixed $requirements
* @return string
*/
private function handleRequirements($requirements)
{
if (is_object($requirements) && $requirements instanceof Constraint) {
if ($requirements instanceof Regex) {
return $requirements->getHtmlPattern();
}
$class = get_class($requirements);
return substr($class, strrpos($class, '\\')+1);
}
return (string)$requirements;
}
}