811 lines
16 KiB
PHP
Raw Normal View History

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
use Symfony\Component\Routing\Route;
2012-04-11 20:00:21 +02:00
/**
* @Annotation
*/
class ApiDoc
{
const DEFAULT_VIEW = 'default';
2012-04-11 20:00:21 +02:00
/**
* Requirements are mandatory parameters in a route.
*
* @var array
*/
private $requirements = array();
/**
* Which views is this route used. Defaults to "Default"
*
* @var array
*/
private $views = array();
/**
* Filters are optional parameters in the query string.
*
2012-04-11 20:00:21 +02:00
* @var array
*/
private $filters = array();
/**
* Parameters are data a client can send.
*
* @var array
*/
private $parameters = array();
2016-05-19 14:28:32 +03:00
/**
* Headers that client can send.
*
* @var array
*/
private $headers = array();
2012-04-11 20:00:21 +02:00
/**
* @var string
*/
private $input = null;
2012-08-27 13:25:03 -04:00
/**
* @var string
*/
private $inputs = null;
2012-08-27 12:56:19 -04:00
/**
* @var string
*/
private $output = null;
2012-04-11 20:00:21 +02:00
2013-08-16 19:18:05 +02:00
/**
* @var string
*/
private $link = null;
2012-04-11 20:00:21 +02:00
/**
* Most of the time, a single line of text describing the action.
*
2012-04-11 20:00:21 +02:00
* @var string
*/
private $description = null;
/**
* Section to group actions together.
*
* @var string
*/
private $section = null;
/**
* Extended documentation.
*
* @var string
*/
private $documentation = null;
/**
* @var Boolean
*/
2013-10-11 15:44:31 +02:00
private $resource = false;
2012-04-11 20:00:21 +02:00
/**
* @var string
*/
private $method;
2013-04-11 22:03:40 +02:00
/**
* @var string
*/
private $host;
/**
* @var string
*/
private $uri;
2012-08-27 12:56:19 -04:00
/**
* @var array
*/
private $response = array();
/**
* @var Route
*/
private $route;
/**
* @var boolean
*/
private $https = false;
2012-12-26 12:23:28 +01:00
/**
* @var boolean
*/
private $authentication = false;
/**
* @var array
*/
private $authenticationRoles = array();
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();
/**
* @var string|null
*/
private $resourceDescription = null;
/**
* @var array
*/
private $responseMap = array();
/**
* @var array
*/
private $parsedResponseMap = array();
2014-05-27 13:33:50 +02:00
/**
* @var array
*/
private $tags = array();
2012-04-11 20:00:21 +02:00
public function __construct(array $data)
{
2013-10-11 15:44:31 +02:00
$this->resource = !empty($data['resource']) ? $data['resource'] : false;
if (isset($data['description'])) {
$this->description = $data['description'];
}
if (isset($data['input'])) {
$this->input = $data['input'];
}
if (isset($data['inputs'])) {
$this->inputs = $data['inputs'];
}
if (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']);
$this->addFilter($name, $filter);
2012-04-11 20:00:21 +02:00
}
}
if (isset($data['requirements'])) {
foreach ($data['requirements'] as $requirement) {
if (!isset($requirement['name'])) {
throw new \InvalidArgumentException('A "requirement" element has to contain a "name" attribute');
}
$name = $requirement['name'];
unset($requirement['name']);
$this->addRequirement($name, $requirement);
}
}
if (isset($data['views'])) {
if (! is_array($data['views'])) {
$data['views'] = array($data['views']);
}
foreach ($data['views'] as $view) {
$this->addView($view);
}
}
if (isset($data['parameters'])) {
foreach ($data['parameters'] as $parameter) {
if (!isset($parameter['name'])) {
throw new \InvalidArgumentException('A "parameter" element has to contain a "name" attribute');
}
if (!isset($parameter['dataType'])) {
throw new \InvalidArgumentException(sprintf(
'"%s" parameter element has to contain a "dataType" attribute',
$parameter['name']
));
}
$name = $parameter['name'];
unset($parameter['name']);
$this->addParameter($name, $parameter);
}
}
2016-05-19 14:28:32 +03:00
if (isset($data['headers'])) {
foreach ($data['headers'] as $header) {
if (!isset($header['name'])) {
throw new \InvalidArgumentException('A "header" element has to contain a "name" attribute');
}
$name = $header['name'];
unset($header['name']);
$this->addHeader($name, $header);
}
}
if (isset($data['output'])) {
$this->output = $data['output'];
2012-08-27 12:56:19 -04:00
}
2012-11-13 04:45:07 +00:00
if (isset($data['statusCodes'])) {
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']);
}
if (isset($data['authenticationRoles'])) {
foreach ($data['authenticationRoles'] as $key => $role) {
$this->authenticationRoles[] = $role;
}
}
if (isset($data['cache'])) {
$this->setCache($data['cache']);
}
if (isset($data['section'])) {
$this->section = $data['section'];
}
2013-03-18 08:40:03 +01:00
if (isset($data['deprecated'])) {
$this->deprecated = $data['deprecated'];
}
2014-02-06 09:27:30 -08:00
2014-06-25 08:46:01 +02:00
if (isset($data['tags'])) {
if (is_array($data['tags'])) {
foreach ($data['tags'] as $tag => $colorCode) {
if (is_numeric($tag)) {
$this->addTag($colorCode);
} else {
$this->addTag($tag, $colorCode);
}
}
} else {
$this->tags[] = $data['tags'];
2014-06-25 08:46:01 +02:00
}
}
2014-05-27 13:33:50 +02:00
2014-02-06 09:27:30 -08:00
if (isset($data['https'])) {
$this->https = $data['https'];
}
if (isset($data['resourceDescription'])) {
$this->resourceDescription = $data['resourceDescription'];
}
if (isset($data['responseMap'])) {
$this->responseMap = $data['responseMap'];
if (isset($this->responseMap[200])) {
$this->output = $this->responseMap[200];
}
}
2012-04-11 20:00:21 +02:00
}
2012-04-12 17:48:21 +02:00
/**
* @param string $name
* @param array $filter
2012-04-12 17:48:21 +02:00
*/
public function addFilter($name, array $filter)
2012-04-11 20:00:21 +02:00
{
$this->filters[$name] = $filter;
2012-04-11 20:00:21 +02:00
}
/**
* @param string $statusCode
* @param mixed $description
*/
public function addStatusCode($statusCode, $description)
{
$this->statusCodes[$statusCode] = !is_array($description) ? array($description) : $description;
}
/**
* @param string $tag
* @param string $colorCode
*/
public function addTag($tag, $colorCode = '#d9534f')
{
$this->tags[$tag] = $colorCode;
}
/**
* @param string $name
* @param array $requirement
*/
public function addRequirement($name, array $requirement)
{
$this->requirements[$name] = $requirement;
}
/**
* @param array $requirements
*/
public function setRequirements(array $requirements)
{
$this->requirements = array_merge($this->requirements, $requirements);
}
2012-04-12 17:48:21 +02:00
/**
* @return string|null
*/
public function getInput()
2012-04-11 20:00:21 +02:00
{
return $this->input;
2012-04-11 20:00:21 +02:00
}
/**
* @return array|null
*/
public function getInputs()
{
return $this->inputs;
}
2012-08-27 12:56:19 -04:00
/**
* @return string|null
*/
public function getOutput()
2012-08-27 12:56:19 -04:00
{
return $this->output;
2012-08-27 12:56:19 -04:00
}
2012-04-12 17:48:21 +02:00
/**
* @return string
2012-04-12 17:48:21 +02:00
*/
public function getDescription()
{
return $this->description;
}
/**
* @param string $description
*/
public function setDescription($description)
{
$this->description = $description;
}
2013-08-16 19:18:05 +02:00
/**
* @param string $link
*/
public function setLink($link)
{
$this->link = $link;
}
/**
* @param string $section
*/
public function setSection($section)
{
$this->section = $section;
}
/**
* @return string
*/
public function getSection()
{
return $this->section;
}
/**
* @return array
*/
public function addView($view)
{
$this->views[] = $view;
}
/**
* @return array
*/
public function getViews()
{
return $this->views;
}
/**
* @param string $documentation
*/
public function setDocumentation($documentation)
{
$this->documentation = $documentation;
}
/**
* @return string
*/
public function getDocumentation()
{
return $this->documentation;
}
2012-04-12 17:48:21 +02:00
/**
* @return Boolean
*/
public function isResource()
2012-04-11 20:00:21 +02:00
{
2013-10-11 15:44:31 +02:00
return (bool) $this->resource;
}
/**
* @return mixed
*/
public function getResource()
{
return $this->resource && is_string($this->resource) ? $this->resource : false;
2012-04-11 20:00:21 +02:00
}
/**
* @param string $name
* @param array $parameter
*/
public function addParameter($name, array $parameter)
{
$this->parameters[$name] = $parameter;
}
/**
* @param array $parameters
*/
public function setParameters(array $parameters)
{
$this->parameters = $parameters;
}
2012-08-27 13:25:03 -04:00
2016-05-19 14:28:32 +03:00
/**
* @param $name
* @param array $header
*/
public function addHeader($name, array $header)
{
$this->headers[$name] = $header;
}
2012-08-27 12:56:19 -04:00
/**
2014-03-27 19:49:59 +00:00
* Sets the response data as processed by the parsers - same format as parameters
2012-08-27 12:56:19 -04:00
*
* @param array $response
*/
public function setResponse(array $response)
{
$this->response = $response;
}
2012-08-27 13:25:03 -04:00
/**
* @param Route $route
*/
public function setRoute(Route $route)
{
$this->route = $route;
if (method_exists($route, 'getHost')) {
$this->host = $route->getHost() ? : null;
2015-11-10 01:51:26 +01:00
//replace route placeholders
foreach ($route->getDefaults() as $key => $value) {
if (is_string($value)) {
$this->host = str_replace('{' . $key . '}', $value, $this->host);
}
2015-11-10 01:51:26 +01:00
}
} else {
$this->host = null;
}
2015-04-30 15:34:19 +02:00
$this->uri = $route->getPath();
$this->method = $route->getMethods() ? implode('|', $route->getMethods()) : 'ANY';
}
/**
* @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;
}
/**
* @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;
}
/**
* @return array
*/
public function getAuthenticationRoles()
{
return $this->authenticationRoles;
}
/**
* @param array $authenticationRoles
*/
public function setAuthenticationRoles($authenticationRoles)
{
$this->authenticationRoles = $authenticationRoles;
}
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;
}
/**
* @return array
*/
public function getParameters()
{
return $this->parameters;
}
2016-05-19 14:28:32 +03:00
/**
* @return array
*/
public function getHeaders()
{
return $this->headers;
}
2013-03-18 08:40:03 +01:00
/**
* @param boolean $deprecated
2016-05-19 14:28:32 +03:00
* @return $this
2013-03-18 08:40:03 +01:00
*/
public function setDeprecated($deprecated)
{
$this->deprecated = (bool) $deprecated;
2013-11-14 10:28:42 +01:00
2013-03-18 08:40:03 +01:00
return $this;
}
2014-09-29 16:17:36 +02:00
/**
* @return string
*/
public function getMethod()
{
return $this->method;
}
/**
* @return array
*/
public function toArray()
{
$data = array(
'method' => $this->method,
'uri' => $this->uri,
);
2013-04-11 22:03:40 +02:00
if ($host = $this->host) {
$data['host'] = $host;
}
if ($description = $this->description) {
$data['description'] = $description;
}
2013-08-16 19:18:05 +02:00
if ($link = $this->link) {
$data['link'] = $link;
}
if ($documentation = $this->documentation) {
$data['documentation'] = $documentation;
}
if ($filters = $this->filters) {
$data['filters'] = $filters;
}
if ($parameters = $this->parameters) {
$data['parameters'] = $parameters;
}
2016-05-19 14:28:32 +03:00
if ($headers = $this->headers) {
$data['headers'] = $headers;
}
if ($requirements = $this->requirements) {
$data['requirements'] = $requirements;
}
2012-08-27 13:25:03 -04:00
if ($views = $this->views) {
$data['views'] = $views;
}
2012-08-27 12:56:19 -04:00
if ($response = $this->response) {
$data['response'] = $response;
}
2016-01-26 04:21:08 +01:00
if ($parsedResponseMap = $this->parsedResponseMap) {
$data['parsedResponseMap'] = $parsedResponseMap;
}
2012-11-13 04:45:07 +00:00
if ($statusCodes = $this->statusCodes) {
$data['statusCodes'] = $statusCodes;
}
if ($section = $this->section) {
$data['section'] = $section;
}
2013-03-26 11:49:12 +01:00
if ($cache = $this->cache) {
$data['cache'] = $cache;
}
2014-05-27 13:33:50 +02:00
if ($tags = $this->tags) {
$data['tags'] = $tags;
}
if ($resourceDescription = $this->resourceDescription) {
$data['resourceDescription'] = $resourceDescription;
}
$data['https'] = $this->https;
2012-12-26 12:23:28 +01:00
$data['authentication'] = $this->authentication;
$data['authenticationRoles'] = $this->authenticationRoles;
2013-03-18 08:40:03 +01:00
$data['deprecated'] = $this->deprecated;
return $data;
}
/**
* @return null|string
*/
public function getResourceDescription()
{
return $this->resourceDescription;
}
/**
* @return array
*/
public function getResponseMap()
{
if (!isset($this->responseMap[200]) && null !== $this->output) {
$this->responseMap[200] = $this->output;
}
return $this->responseMap;
}
/**
* @return array
*/
public function getParsedResponseMap()
{
return $this->parsedResponseMap;
}
/**
* @param $model
* @param $type
* @param int $statusCode
*/
public function setResponseForStatusCode($model, $type, $statusCode = 200)
{
$this->parsedResponseMap[$statusCode] = array('type' => $type, 'model' => $model);
if ($statusCode == 200 && $this->response !== $model) {
$this->response = $model;
}
}
2012-04-11 20:00:21 +02:00
}