288 lines
5.6 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
{
/**
* 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();
/**
* Parameters are data a client can send.
*
* @var array
*/
private $parameters = array();
2012-04-11 20:00:21 +02:00
/**
* @var string
*/
private $input = null;
2012-08-27 13:25:03 -04:00
2012-08-27 12:56:19 -04:00
/**
* @var string
*/
private $output = 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;
/**
* Extended documentation.
*
* @var string
*/
private $documentation = null;
/**
* @var Boolean
*/
private $isResource = false;
2012-04-11 20:00:21 +02:00
/**
* @var string
*/
private $method;
/**
* @var string
*/
private $uri;
2012-08-27 12:56:19 -04:00
/**
* @var array
*/
private $response = array();
/**
* @var Route
*/
private $route;
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)
{
$this->isResource = isset($data['resource']) && $data['resource'];
if (isset($data['description'])) {
$this->description = $data['description'];
}
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']);
$this->addFilter($name, $filter);
2012-04-11 20:00:21 +02:00
}
}
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'])) {
$this->statusCodes = $data['statusCodes'];
}
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 $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
}
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;
}
/**
* @param string $documentation
*/
public function setDocumentation($documentation)
{
$this->documentation = $documentation;
}
2012-04-12 17:48:21 +02:00
/**
* @return Boolean
*/
public function isResource()
2012-04-11 20:00:21 +02:00
{
return $this->isResource;
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
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
/**
* @param Route $route
*/
public function setRoute(Route $route)
{
$this->route = $route;
$this->uri = $route->getPattern();
$this->method = $route->getRequirement('_method') ?: 'ANY';
}
/**
* @return Route
*/
public function getRoute()
{
return $this->route;
}
/**
* @return array
*/
public function toArray()
{
$data = array(
'method' => $this->method,
'uri' => $this->uri,
);
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-11-13 04:45:07 +00:00
if ($statusCodes = $this->statusCodes) {
$data['statusCodes'] = $statusCodes;
}
return $data;
}
2012-04-11 20:00:21 +02:00
}