2012-04-11 20:00:21 +02:00
|
|
|
|
<?php
|
|
|
|
|
|
2012-04-13 11:03:05 +02:00
|
|
|
|
/*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2012-04-12 18:37:42 +02:00
|
|
|
|
namespace Nelmio\ApiDocBundle\Annotation;
|
2012-04-11 20:00:21 +02:00
|
|
|
|
|
2012-07-20 00:58:58 +02:00
|
|
|
|
use Symfony\Component\Routing\Route;
|
|
|
|
|
|
2012-04-11 20:00:21 +02:00
|
|
|
|
/**
|
|
|
|
|
* @Annotation
|
|
|
|
|
*/
|
|
|
|
|
class ApiDoc
|
|
|
|
|
{
|
|
|
|
|
/**
|
2012-11-19 18:21:12 +01:00
|
|
|
|
* Requirements are mandatory parameters in a route.
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
private $requirements = array();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Filters are optional parameters in the query string.
|
|
|
|
|
*
|
2012-04-11 20:00:21 +02:00
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
private $filters = array();
|
|
|
|
|
|
2012-11-19 18:21:12 +01:00
|
|
|
|
/**
|
|
|
|
|
* Parameters are data a client can send.
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
private $parameters = array();
|
|
|
|
|
|
2012-04-11 20:00:21 +02:00
|
|
|
|
/**
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2012-07-23 15:44:37 -04:00
|
|
|
|
private $input = null;
|
2012-08-27 13:25:03 -04:00
|
|
|
|
|
2012-08-27 12:56:19 -04:00
|
|
|
|
/**
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2012-11-19 18:21:12 +01:00
|
|
|
|
private $output = null;
|
2012-04-11 20:00:21 +02:00
|
|
|
|
|
|
|
|
|
/**
|
2012-11-19 18:21:12 +01:00
|
|
|
|
* Most of the time, a single line of text describing the action.
|
|
|
|
|
*
|
2012-04-11 20:00:21 +02:00
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2012-04-12 17:24:38 +02:00
|
|
|
|
private $description = null;
|
|
|
|
|
|
2013-02-13 17:08:00 -02:00
|
|
|
|
/**
|
|
|
|
|
* Section to group actions together.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2013-02-14 13:52:20 -02:00
|
|
|
|
private $section = null;
|
2013-02-13 17:08:00 -02:00
|
|
|
|
|
2012-07-13 13:32:53 +01:00
|
|
|
|
/**
|
2012-11-19 18:21:12 +01:00
|
|
|
|
* Extended documentation.
|
|
|
|
|
*
|
2012-07-13 13:32:53 +01:00
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
private $documentation = null;
|
|
|
|
|
|
2012-04-12 17:24:38 +02:00
|
|
|
|
/**
|
|
|
|
|
* @var Boolean
|
|
|
|
|
*/
|
|
|
|
|
private $isResource = false;
|
2012-04-11 20:00:21 +02:00
|
|
|
|
|
2012-07-20 00:58:58 +02:00
|
|
|
|
/**
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
private $method;
|
|
|
|
|
|
2013-04-11 22:03:40 +02:00
|
|
|
|
/**
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
private $host;
|
|
|
|
|
|
2012-07-20 00:58:58 +02:00
|
|
|
|
/**
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
private $uri;
|
|
|
|
|
|
2012-08-27 12:56:19 -04:00
|
|
|
|
/**
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
private $response = array();
|
2012-07-20 00:58:58 +02:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var Route
|
|
|
|
|
*/
|
|
|
|
|
private $route;
|
|
|
|
|
|
2012-12-10 10:21:04 -08:00
|
|
|
|
/**
|
|
|
|
|
* @var boolean
|
|
|
|
|
*/
|
|
|
|
|
private $https = false;
|
|
|
|
|
|
2012-12-26 12:23:28 +01:00
|
|
|
|
/**
|
|
|
|
|
* @var boolean
|
|
|
|
|
*/
|
|
|
|
|
private $authentication = false;
|
|
|
|
|
|
2013-03-26 11:49:12 +01:00
|
|
|
|
/**
|
|
|
|
|
* @var int
|
|
|
|
|
*/
|
|
|
|
|
private $cache;
|
|
|
|
|
|
2013-03-18 08:40:03 +01:00
|
|
|
|
/**
|
|
|
|
|
* @var boolean
|
|
|
|
|
*/
|
|
|
|
|
private $deprecated = false;
|
|
|
|
|
|
2012-11-13 04:45:07 +00:00
|
|
|
|
/**
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
private $statusCodes = array();
|
|
|
|
|
|
2012-04-11 20:00:21 +02:00
|
|
|
|
public function __construct(array $data)
|
|
|
|
|
{
|
2012-11-19 18:21:12 +01:00
|
|
|
|
$this->isResource = isset($data['resource']) && $data['resource'];
|
|
|
|
|
|
|
|
|
|
if (isset($data['description'])) {
|
|
|
|
|
$this->description = $data['description'];
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-23 15:44:37 -04:00
|
|
|
|
if (isset($data['input'])) {
|
|
|
|
|
$this->input = $data['input'];
|
2012-05-23 00:33:01 +02:00
|
|
|
|
} elseif (isset($data['filters'])) {
|
2012-04-11 20:00:21 +02:00
|
|
|
|
foreach ($data['filters'] as $filter) {
|
|
|
|
|
if (!isset($filter['name'])) {
|
|
|
|
|
throw new \InvalidArgumentException('A "filter" element has to contain a "name" attribute');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$name = $filter['name'];
|
|
|
|
|
unset($filter['name']);
|
|
|
|
|
|
2012-07-18 12:23:57 +02:00
|
|
|
|
$this->addFilter($name, $filter);
|
2012-04-11 20:00:21 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-19 18:21:12 +01:00
|
|
|
|
if (isset($data['output'])) {
|
|
|
|
|
$this->output = $data['output'];
|
2012-08-27 12:56:19 -04:00
|
|
|
|
}
|
2012-04-12 17:24:38 +02:00
|
|
|
|
|
2012-11-13 04:45:07 +00:00
|
|
|
|
if (isset($data['statusCodes'])) {
|
2013-02-18 21:07:59 +01:00
|
|
|
|
foreach ($data['statusCodes'] as $statusCode => $description) {
|
|
|
|
|
$this->addStatusCode($statusCode, $description);
|
|
|
|
|
}
|
2012-11-13 04:45:07 +00:00
|
|
|
|
}
|
2012-12-26 12:23:28 +01:00
|
|
|
|
|
|
|
|
|
if (isset($data['authentication'])) {
|
|
|
|
|
$this->setAuthentication((bool) $data['authentication']);
|
|
|
|
|
}
|
2013-02-13 17:08:00 -02:00
|
|
|
|
|
2013-03-27 16:20:42 +01:00
|
|
|
|
if (isset($data['cache'])) {
|
|
|
|
|
$this->setCache($data['cache']);
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-13 17:08:00 -02:00
|
|
|
|
if (isset($data['section'])) {
|
|
|
|
|
$this->section = $data['section'];
|
|
|
|
|
}
|
2013-03-18 08:40:03 +01:00
|
|
|
|
|
|
|
|
|
if (isset($data['deprecated'])) {
|
|
|
|
|
$this->deprecated = $data['deprecated'];
|
|
|
|
|
}
|
2012-04-11 20:00:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-04-12 17:48:21 +02:00
|
|
|
|
/**
|
2012-07-20 00:58:58 +02:00
|
|
|
|
* @param string $name
|
|
|
|
|
* @param array $filter
|
2012-04-12 17:48:21 +02:00
|
|
|
|
*/
|
2012-07-20 00:58:58 +02:00
|
|
|
|
public function addFilter($name, array $filter)
|
2012-04-11 20:00:21 +02:00
|
|
|
|
{
|
2012-07-20 00:58:58 +02:00
|
|
|
|
$this->filters[$name] = $filter;
|
2012-04-11 20:00:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-18 21:07:59 +01:00
|
|
|
|
/**
|
|
|
|
|
* @param string $statusCode
|
|
|
|
|
* @param mixed $description
|
|
|
|
|
*/
|
|
|
|
|
public function addStatusCode($statusCode, $description)
|
|
|
|
|
{
|
|
|
|
|
$this->statusCodes[$statusCode] = !is_array($description) ? array($description) : $description;
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-18 12:23:57 +02:00
|
|
|
|
/**
|
|
|
|
|
* @param string $name
|
2012-07-20 00:58:58 +02:00
|
|
|
|
* @param array $requirement
|
2012-07-18 12:23:57 +02:00
|
|
|
|
*/
|
2012-07-20 00:58:58 +02:00
|
|
|
|
public function addRequirement($name, array $requirement)
|
2012-07-18 12:23:57 +02:00
|
|
|
|
{
|
2012-07-20 00:58:58 +02:00
|
|
|
|
$this->requirements[$name] = $requirement;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $requirements
|
|
|
|
|
*/
|
|
|
|
|
public function setRequirements(array $requirements)
|
|
|
|
|
{
|
|
|
|
|
$this->requirements = array_merge($this->requirements, $requirements);
|
2012-07-18 12:23:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-04-12 17:48:21 +02:00
|
|
|
|
/**
|
|
|
|
|
* @return string|null
|
|
|
|
|
*/
|
2012-07-23 15:44:37 -04:00
|
|
|
|
public function getInput()
|
2012-04-11 20:00:21 +02:00
|
|
|
|
{
|
2012-07-23 15:44:37 -04:00
|
|
|
|
return $this->input;
|
2012-04-11 20:00:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-27 12:56:19 -04:00
|
|
|
|
/**
|
|
|
|
|
* @return string|null
|
|
|
|
|
*/
|
2012-11-19 18:21:12 +01:00
|
|
|
|
public function getOutput()
|
2012-08-27 12:56:19 -04:00
|
|
|
|
{
|
2012-11-19 18:21:12 +01:00
|
|
|
|
return $this->output;
|
2012-08-27 12:56:19 -04:00
|
|
|
|
}
|
|
|
|
|
|
2012-04-12 17:48:21 +02:00
|
|
|
|
/**
|
2012-07-20 00:58:58 +02:00
|
|
|
|
* @return string
|
2012-04-12 17:48:21 +02:00
|
|
|
|
*/
|
2012-04-12 17:24:38 +02:00
|
|
|
|
public function getDescription()
|
|
|
|
|
{
|
|
|
|
|
return $this->description;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-19 16:23:26 +02:00
|
|
|
|
/**
|
|
|
|
|
* @param string $description
|
|
|
|
|
*/
|
|
|
|
|
public function setDescription($description)
|
|
|
|
|
{
|
|
|
|
|
$this->description = $description;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-13 17:08:00 -02:00
|
|
|
|
/**
|
|
|
|
|
* @param string $section
|
|
|
|
|
*/
|
|
|
|
|
public function setSection($section)
|
|
|
|
|
{
|
|
|
|
|
$this->section = $section;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getSection()
|
|
|
|
|
{
|
|
|
|
|
return $this->section;
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-13 13:32:53 +01:00
|
|
|
|
/**
|
|
|
|
|
* @param string $documentation
|
|
|
|
|
*/
|
|
|
|
|
public function setDocumentation($documentation)
|
|
|
|
|
{
|
|
|
|
|
$this->documentation = $documentation;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-12 17:48:21 +02:00
|
|
|
|
/**
|
|
|
|
|
* @return Boolean
|
|
|
|
|
*/
|
2012-04-12 17:24:38 +02:00
|
|
|
|
public function isResource()
|
2012-04-11 20:00:21 +02:00
|
|
|
|
{
|
2012-04-12 17:24:38 +02:00
|
|
|
|
return $this->isResource;
|
2012-04-11 20:00:21 +02:00
|
|
|
|
}
|
2012-07-20 00:58:58 +02:00
|
|
|
|
|
2012-11-14 23:59:06 +01:00
|
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param array $parameter
|
|
|
|
|
*/
|
|
|
|
|
public function addParameter($name, array $parameter)
|
|
|
|
|
{
|
|
|
|
|
$this->parameters[$name] = $parameter;
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-20 00:58:58 +02:00
|
|
|
|
/**
|
|
|
|
|
* @param array $parameters
|
|
|
|
|
*/
|
|
|
|
|
public function setParameters(array $parameters)
|
|
|
|
|
{
|
|
|
|
|
$this->parameters = $parameters;
|
|
|
|
|
}
|
2012-08-27 13:25:03 -04:00
|
|
|
|
|
2012-08-27 12:56:19 -04:00
|
|
|
|
/**
|
|
|
|
|
* Sets the responsed data as processed by the parsers - same format as parameters
|
|
|
|
|
*
|
|
|
|
|
* @param array $response
|
|
|
|
|
*/
|
|
|
|
|
public function setResponse(array $response)
|
|
|
|
|
{
|
|
|
|
|
$this->response = $response;
|
|
|
|
|
}
|
2012-08-27 13:25:03 -04:00
|
|
|
|
|
2012-07-20 00:58:58 +02:00
|
|
|
|
/**
|
|
|
|
|
* @param Route $route
|
|
|
|
|
*/
|
|
|
|
|
public function setRoute(Route $route)
|
|
|
|
|
{
|
2012-11-19 18:21:12 +01:00
|
|
|
|
$this->route = $route;
|
2013-04-12 10:45:20 +03:00
|
|
|
|
|
|
|
|
|
if (method_exists($route, 'getHost')) {
|
|
|
|
|
$this->host = $route->getHost() ? : null;
|
|
|
|
|
} else {
|
|
|
|
|
$this->host = null;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-19 18:21:12 +01:00
|
|
|
|
$this->uri = $route->getPattern();
|
|
|
|
|
$this->method = $route->getRequirement('_method') ?: 'ANY';
|
2012-07-20 00:58:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return Route
|
|
|
|
|
*/
|
|
|
|
|
public function getRoute()
|
|
|
|
|
{
|
|
|
|
|
return $this->route;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-11 22:03:40 +02:00
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getHost()
|
|
|
|
|
{
|
|
|
|
|
return $this->host;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $host
|
|
|
|
|
*/
|
|
|
|
|
public function setHost($host)
|
|
|
|
|
{
|
|
|
|
|
$this->host = $host;
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-10 10:21:04 -08:00
|
|
|
|
/**
|
|
|
|
|
* @return boolean
|
|
|
|
|
*/
|
|
|
|
|
public function getHttps()
|
|
|
|
|
{
|
|
|
|
|
return $this->https;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param boolean $https
|
|
|
|
|
*/
|
|
|
|
|
public function setHttps($https)
|
|
|
|
|
{
|
|
|
|
|
$this->https = $https;
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-26 12:23:28 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return boolean
|
|
|
|
|
*/
|
|
|
|
|
public function getAuthentication()
|
|
|
|
|
{
|
|
|
|
|
return $this->authentication;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2013-03-18 08:40:03 +01:00
|
|
|
|
* @param boolean $authentication
|
2012-12-26 12:23:28 +01:00
|
|
|
|
*/
|
|
|
|
|
public function setAuthentication($authentication)
|
|
|
|
|
{
|
|
|
|
|
$this->authentication = $authentication;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-26 11:49:12 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public function getCache()
|
|
|
|
|
{
|
|
|
|
|
return $this->cache;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param int $cache
|
|
|
|
|
*/
|
|
|
|
|
public function setCache($cache)
|
|
|
|
|
{
|
|
|
|
|
$this->cache = (int) $cache;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-18 08:40:03 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return boolean
|
|
|
|
|
*/
|
|
|
|
|
public function getDeprecated()
|
|
|
|
|
{
|
|
|
|
|
return $this->deprecated;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-30 16:21:12 +04:00
|
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function getFilters()
|
|
|
|
|
{
|
|
|
|
|
return $this->filters;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function getRequirements()
|
|
|
|
|
{
|
|
|
|
|
return $this->requirements;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-18 08:40:03 +01:00
|
|
|
|
/**
|
|
|
|
|
* @param boolean $deprecated
|
|
|
|
|
*/
|
|
|
|
|
public function setDeprecated($deprecated)
|
|
|
|
|
{
|
|
|
|
|
$this->deprecated = (bool) $deprecated;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-20 00:58:58 +02:00
|
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function toArray()
|
|
|
|
|
{
|
|
|
|
|
$data = array(
|
2012-11-19 18:21:12 +01:00
|
|
|
|
'method' => $this->method,
|
|
|
|
|
'uri' => $this->uri,
|
2012-07-20 00:58:58 +02:00
|
|
|
|
);
|
|
|
|
|
|
2013-04-11 22:03:40 +02:00
|
|
|
|
if ($host = $this->host) {
|
|
|
|
|
$data['host'] = $host;
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-20 00:58:58 +02:00
|
|
|
|
if ($description = $this->description) {
|
|
|
|
|
$data['description'] = $description;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($documentation = $this->documentation) {
|
|
|
|
|
$data['documentation'] = $documentation;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($filters = $this->filters) {
|
|
|
|
|
$data['filters'] = $filters;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($parameters = $this->parameters) {
|
|
|
|
|
$data['parameters'] = $parameters;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($requirements = $this->requirements) {
|
|
|
|
|
$data['requirements'] = $requirements;
|
|
|
|
|
}
|
2012-08-27 13:25:03 -04:00
|
|
|
|
|
2012-08-27 12:56:19 -04:00
|
|
|
|
if ($response = $this->response) {
|
|
|
|
|
$data['response'] = $response;
|
|
|
|
|
}
|
2012-07-23 15:44:37 -04:00
|
|
|
|
|
2012-11-13 04:45:07 +00:00
|
|
|
|
if ($statusCodes = $this->statusCodes) {
|
|
|
|
|
$data['statusCodes'] = $statusCodes;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-17 16:40:31 +01:00
|
|
|
|
if ($section = $this->section) {
|
2013-02-14 13:52:20 -02:00
|
|
|
|
$data['section'] = $section;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-26 11:49:12 +01:00
|
|
|
|
if ($cache = $this->cache) {
|
|
|
|
|
$data['cache'] = $cache;
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-10 10:21:04 -08:00
|
|
|
|
$data['https'] = $this->https;
|
2012-12-26 12:23:28 +01:00
|
|
|
|
$data['authentication'] = $this->authentication;
|
2013-03-18 08:40:03 +01:00
|
|
|
|
$data['deprecated'] = $this->deprecated;
|
2012-12-10 10:21:04 -08:00
|
|
|
|
|
2012-07-20 00:58:58 +02:00
|
|
|
|
return $data;
|
|
|
|
|
}
|
2012-04-11 20:00:21 +02:00
|
|
|
|
}
|