501 lines
12 KiB
PHP
Raw Normal View History

2012-04-11 20:00:21 +02:00
<?php
2024-10-01 23:00:23 +03:00
namespace Nelmio\ApiDocBundle\Attribute;
2012-04-11 20:00:21 +02:00
use Symfony\Component\Routing\Route;
2024-10-01 23:00:23 +03:00
#[\Attribute(\Attribute::TARGET_METHOD)]
2012-04-11 20:00:21 +02:00
class ApiDoc
{
2024-10-01 15:54:04 +03:00
public const DEFAULT_VIEW = 'default';
2012-04-11 20:00:21 +02:00
/**
* Requirements are mandatory parameters in a route.
*
2024-10-01 23:00:23 +03:00
* @var array<string, array<string, string>>
*/
2024-10-01 23:00:23 +03:00
private array $requirements = [];
/**
* Which views is this route used. Defaults to "Default"
*
2024-10-01 23:00:23 +03:00
* @var string[]
*/
2024-10-01 23:00:23 +03:00
private array $views = [];
/**
* Filters are optional parameters in the query string.
*
2024-10-01 23:00:23 +03:00
* @var array<string, array<string, string>>
2012-04-11 20:00:21 +02:00
*/
2024-10-01 23:00:23 +03:00
private array $filters = [];
2012-04-11 20:00:21 +02:00
/**
* Parameters are data a client can send.
*
2024-10-01 23:00:23 +03:00
* @var array<string, array<string, mixed>>
*/
2024-10-01 23:00:23 +03:00
private array $parameters = [];
2016-05-19 14:28:32 +03:00
/**
* Headers that client can send.
*
2024-10-01 23:00:23 +03:00
* @var array<string, array<string, mixed>>
2012-04-11 20:00:21 +02:00
*/
2024-10-01 23:00:23 +03:00
private array $headers = [];
2024-10-01 23:00:23 +03:00
private ?string $link = null;
/**
* Extended documentation.
*/
2024-10-01 23:00:23 +03:00
private ?string $documentation = null;
2024-10-01 23:00:23 +03:00
private Route $route;
private ?string $host = null;
private string $method;
private string $uri;
2012-04-11 20:00:21 +02:00
2024-10-01 23:00:23 +03:00
private array $response = [];
2013-04-11 22:03:40 +02:00
/**
2024-10-01 23:00:23 +03:00
* @var array<int|string, string[]>
2013-04-11 22:03:40 +02:00
*/
2024-10-01 23:00:23 +03:00
private array $statusCodes = [];
2013-04-11 22:03:40 +02:00
/**
2024-10-01 23:00:23 +03:00
* @var array<int, array<mixed>>
*/
2024-10-01 23:00:23 +03:00
private array $responseMap = [];
2024-10-01 23:00:23 +03:00
private array $parsedResponseMap = [];
2013-03-18 08:40:03 +01:00
/**
2024-10-01 23:00:23 +03:00
* @var array<string|int, string>
2013-03-18 08:40:03 +01:00
*/
2024-10-01 23:00:23 +03:00
private array $tags = [];
2014-05-27 13:33:50 +02:00
2024-10-01 16:11:23 +03:00
private ?string $scope = null;
2024-10-01 23:00:23 +03:00
/**
* @param string[]|string|null $description
*/
public function __construct(
private string|bool $resource = false,
private array|string|null $description = null,
private string|array|null $input = null,
private ?array $inputs = null,
private string|array|null $output = null,
private ?string $section = null,
private bool $deprecated = false,
private ?string $resourceDescription = null,
?array $filters = null,
?array $requirements = null,
array|string|null $views = null,
?array $parameters = null,
?array $headers = null,
?array $statusCodes = null,
array|string|int|null $tags = null,
?array $responseMap = null,
) {
if (null !== $filters) {
foreach ($filters as $filter) {
2012-04-11 20:00:21 +02:00
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
}
}
2024-10-01 23:00:23 +03:00
if (null !== $requirements) {
foreach ($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);
}
}
2024-10-01 23:00:23 +03:00
if (null !== $views) {
if (!is_array($views)) {
$views = [$views];
}
2024-10-01 23:00:23 +03:00
foreach ($views as $view) {
$this->addView($view);
}
}
2024-10-01 23:00:23 +03:00
if (null !== $parameters) {
foreach ($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);
}
}
2024-10-01 23:00:23 +03:00
if (null !== $headers) {
foreach ($headers as $header) {
2016-05-19 14:28:32 +03:00
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);
}
}
2024-10-01 23:00:23 +03:00
if (null !== $statusCodes) {
foreach ($statusCodes as $statusCode => $statusDescription) {
$this->addStatusCode($statusCode, $statusDescription);
}
2012-11-13 04:45:07 +00:00
}
2012-12-26 12:23:28 +01:00
2024-10-01 23:00:23 +03:00
if (null !== $tags) {
if (is_array($tags)) {
foreach ($tags as $tag => $colorCode) {
if (is_numeric($tag)) {
$this->addTag($colorCode);
} else {
$this->addTag($tag, $colorCode);
}
}
} else {
2024-10-01 23:00:23 +03:00
$this->tags[] = $tags;
2014-06-25 08:46:01 +02:00
}
}
2014-05-27 13:33:50 +02:00
2024-10-01 23:00:23 +03:00
if (null !== $responseMap) {
$this->responseMap = $responseMap;
if (isset($this->responseMap[200])) {
$this->output = $this->responseMap[200];
}
}
2012-04-11 20:00:21 +02:00
}
2024-10-01 23:00:23 +03:00
public function addFilter(string $name, array $filter): void
2012-04-11 20:00:21 +02:00
{
$this->filters[$name] = $filter;
2012-04-11 20:00:21 +02:00
}
2024-10-01 23:00:23 +03:00
public function addStatusCode(int|string $statusCode, string|array $description): void
{
2024-10-01 15:54:04 +03:00
$this->statusCodes[$statusCode] = !is_array($description) ? [$description] : $description;
}
2024-10-01 23:00:23 +03:00
public function addTag(int|string $tag, string $colorCode = '#d9534f'): void
{
$this->tags[$tag] = $colorCode;
}
2024-10-01 23:00:23 +03:00
public function addRequirement(string $name, array $requirement): void
{
$this->requirements[$name] = $requirement;
}
2024-10-01 15:54:04 +03:00
public function setRequirements(array $requirements): void
{
$this->requirements = array_merge($this->requirements, $requirements);
}
2024-10-01 23:00:23 +03:00
public function getInput(): string|array|null
2012-04-11 20:00:21 +02:00
{
return $this->input;
2012-04-11 20:00:21 +02:00
}
2024-10-01 23:00:23 +03:00
public function getInputs(): ?array
{
return $this->inputs;
}
2024-10-01 23:00:23 +03:00
public function getOutput(): array|string|null
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
/**
2024-10-01 23:00:23 +03:00
* @return string[]|string|null
2012-04-12 17:48:21 +02:00
*/
2024-10-01 23:00:23 +03:00
public function getDescription(): array|string|null
{
return $this->description;
}
/**
2024-10-01 23:00:23 +03:00
* @param string[]|string|null $description
*/
2024-10-01 23:00:23 +03:00
public function setDescription(array|string|null $description): void
{
$this->description = $description;
}
2024-10-01 23:00:23 +03:00
public function setLink(?string $link): void
2013-08-16 19:18:05 +02:00
{
$this->link = $link;
}
2024-10-01 23:00:23 +03:00
public function getSection(): ?string
{
return $this->section;
}
2024-10-01 23:00:23 +03:00
public function addView(string $view): void
{
$this->views[] = $view;
}
/**
2024-10-01 23:00:23 +03:00
* @return string[]
*/
2024-10-01 23:00:23 +03:00
public function getViews(): array
{
return $this->views;
}
2024-10-01 23:00:23 +03:00
public function setDocumentation(?string $documentation): void
{
$this->documentation = $documentation;
}
2024-10-01 23:00:23 +03:00
public function getDocumentation(): ?string
{
return $this->documentation;
}
2024-10-01 23:00:23 +03:00
public function isResource(): bool
2012-04-11 20:00:21 +02:00
{
2013-10-11 15:44:31 +02:00
return (bool) $this->resource;
}
2024-10-01 23:00:23 +03:00
public function getResource(): string|bool
2013-10-11 15:44:31 +02:00
{
return $this->resource && is_string($this->resource) ? $this->resource : false;
2012-04-11 20:00:21 +02:00
}
2024-10-01 23:00:23 +03:00
public function addParameter(string $name, array $parameter): void
{
$this->parameters[$name] = $parameter;
}
2024-10-01 15:54:04 +03:00
public function setParameters(array $parameters): void
{
$this->parameters = $parameters;
}
2012-08-27 13:25:03 -04:00
2024-10-01 15:54:04 +03:00
public function addHeader($name, array $header): void
2016-05-19 14:28:32 +03:00
{
$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
*/
2024-10-01 15:54:04 +03:00
public function setResponse(array $response): void
2012-08-27 12:56:19 -04:00
{
$this->response = $response;
}
2012-08-27 13:25:03 -04:00
2024-10-01 15:54:04 +03:00
public function setRoute(Route $route): void
{
2024-10-01 15:54:04 +03:00
$this->route = $route;
if (method_exists($route, 'getHost')) {
2024-10-01 15:54:04 +03:00
$this->host = $route->getHost() ?: null;
2015-11-10 01:51:26 +01:00
2024-10-01 15:54:04 +03:00
// replace route placeholders
foreach ($route->getDefaults() as $key => $value) {
if (null !== $this->host && is_string($value)) {
$this->host = str_replace('{' . $key . '}', $value, $this->host);
}
2015-11-10 01:51:26 +01:00
}
} else {
$this->host = null;
}
2024-10-01 15:54:04 +03:00
$this->uri = $route->getPath();
2015-04-30 15:34:19 +02:00
$this->method = $route->getMethods() ? implode('|', $route->getMethods()) : 'ANY';
}
2024-10-01 23:00:23 +03:00
public function getRoute(): Route
{
return $this->route;
}
2024-10-01 23:00:23 +03:00
public function getHost(): ?string
2013-04-11 22:03:40 +02:00
{
return $this->host;
}
2024-10-01 23:00:23 +03:00
public function getDeprecated(): bool
2013-03-18 08:40:03 +01:00
{
return $this->deprecated;
}
2013-04-30 16:21:12 +04:00
/**
2024-10-01 23:00:23 +03:00
* @return array<string, array<string, string>>
2013-04-30 16:21:12 +04:00
*/
2024-10-01 23:00:23 +03:00
public function getFilters(): array
2013-04-30 16:21:12 +04:00
{
return $this->filters;
}
2024-10-01 23:00:23 +03:00
public function getRequirements(): array
2013-04-30 16:21:12 +04:00
{
return $this->requirements;
}
2024-10-01 23:00:23 +03:00
public function getParameters(): array
{
return $this->parameters;
}
2024-10-01 23:00:23 +03:00
public function getHeaders(): array
2016-05-19 14:28:32 +03:00
{
return $this->headers;
}
2024-10-01 23:00:23 +03:00
public function setDeprecated(bool $deprecated): void
2013-03-18 08:40:03 +01:00
{
2024-10-01 23:00:23 +03:00
$this->deprecated = $deprecated;
2013-03-18 08:40:03 +01:00
}
2024-10-01 23:00:23 +03:00
public function getMethod(): string
2014-09-29 16:17:36 +02:00
{
return $this->method;
}
2024-10-01 16:11:23 +03:00
public function setScope(string $scope): void
{
$this->scope = $scope;
}
public function getScope(): ?string
{
return $this->scope;
}
/**
* @return array
*/
public function toArray()
{
2024-10-01 15:54:04 +03:00
$data = [
2024-10-01 23:00:23 +03:00
'method' => $this->method ?? null,
'uri' => $this->uri ?? null,
2024-10-01 15:54:04 +03:00
];
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;
}
2014-05-27 13:33:50 +02:00
if ($tags = $this->tags) {
$data['tags'] = $tags;
}
if ($resourceDescription = $this->resourceDescription) {
$data['resourceDescription'] = $resourceDescription;
}
2013-03-18 08:40:03 +01:00
$data['deprecated'] = $this->deprecated;
2024-10-01 16:11:23 +03:00
$data['scope'] = $this->scope;
return $data;
}
2024-10-01 23:00:23 +03:00
public function getResourceDescription(): ?string
{
return $this->resourceDescription;
}
2024-10-01 23:00:23 +03:00
public function getResponseMap(): array
{
if (!isset($this->responseMap[200]) && null !== $this->output) {
$this->responseMap[200] = $this->output;
}
return $this->responseMap;
}
2024-10-01 23:00:23 +03:00
public function getParsedResponseMap(): array
{
return $this->parsedResponseMap;
}
2024-10-01 23:00:23 +03:00
public function setResponseForStatusCode(array $model, array $type, int $statusCode = 200): void
{
2024-10-01 15:54:04 +03:00
$this->parsedResponseMap[$statusCode] = ['type' => $type, 'model' => $model];
2024-10-01 23:00:23 +03:00
if (200 === $statusCode && $this->response !== $model) {
$this->response = $model;
}
}
2012-04-11 20:00:21 +02:00
}