|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Kreta package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Beñat Espiña <[email protected]> |
|
7
|
|
|
* (c) Gorka Laucirica <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
10
|
|
|
* file that was distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Kreta\SimpleApiDocBundle\Annotation; |
|
14
|
|
|
|
|
15
|
|
|
use Nelmio\ApiDocBundle\Annotation\ApiDoc as BaseApiDoc; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Api doc class. |
|
19
|
|
|
* |
|
20
|
|
|
* @author Beñat Espiña <[email protected]> |
|
21
|
|
|
* |
|
22
|
|
|
* @Annotation() |
|
23
|
|
|
*/ |
|
24
|
|
|
class ApiDoc extends BaseApiDoc |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Array that contains default descriptions of each status code. |
|
28
|
|
|
* |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $defaultStatusCodes = [ |
|
32
|
|
|
200 => '<data>', |
|
33
|
|
|
201 => '<data>', |
|
34
|
|
|
204 => '', |
|
35
|
|
|
403 => 'Not allowed to access this resource', |
|
36
|
|
|
404 => 'Does not exist any object with id passed', |
|
37
|
|
|
409 => 'The resource is currently in use', |
|
38
|
|
|
]; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Array that contains the format of the Api. |
|
42
|
|
|
* |
|
43
|
|
|
* @var array |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $format = [ |
|
46
|
|
|
'requirement' => 'json|jsonp', |
|
47
|
|
|
'description' => 'Supported formats, by default json', |
|
48
|
|
|
]; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Constructor. |
|
52
|
|
|
* |
|
53
|
|
|
* @param array $data The data |
|
54
|
|
|
*/ |
|
55
|
|
|
public function __construct(array $data) |
|
56
|
|
|
{ |
|
57
|
|
|
parent::__construct($data); |
|
58
|
|
|
$this->buildStatusCodes($data); |
|
59
|
|
|
$this->addRequirement('_format', $this->format); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Loads data given status codes. |
|
64
|
|
|
* |
|
65
|
|
|
* @param array $data Array that contains all the data |
|
66
|
|
|
* |
|
67
|
|
|
* @return self |
|
68
|
|
|
*/ |
|
69
|
|
|
protected function buildStatusCodes(array $data) |
|
70
|
|
|
{ |
|
71
|
|
|
if (isset($data['statusCodes'])) { |
|
72
|
|
|
$this->initializeStatusCodes(); |
|
73
|
|
|
foreach ($data['statusCodes'] as $key => $element) { |
|
74
|
|
|
if ((int) $key < 200) { |
|
75
|
|
|
$this->statusCodes($element); |
|
76
|
|
|
} else { |
|
77
|
|
|
$this->statusCodes($key, $element); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return $this; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Method that allows to choose between status |
|
87
|
|
|
* code passing the code and optional description. |
|
88
|
|
|
* |
|
89
|
|
|
* @param int $statusCode The status code |
|
90
|
|
|
* @param string|null $customDescription The description |
|
91
|
|
|
* |
|
92
|
|
|
* @return self |
|
93
|
|
|
*/ |
|
94
|
|
|
protected function statusCodes($statusCode, $customDescription = null) |
|
95
|
|
|
{ |
|
96
|
|
|
if ($customDescription) { |
|
|
|
|
|
|
97
|
|
|
$description = $customDescription; |
|
98
|
|
|
} |
|
99
|
|
|
if ($customDescription !== null || array_key_exists($statusCode, $this->defaultStatusCodes)) { |
|
100
|
|
|
if (!isset($description)) { |
|
101
|
|
|
$description = $this->defaultStatusCodes[$statusCode]; |
|
102
|
|
|
} |
|
103
|
|
|
$description = !is_array($description) ? [$description] : $description; |
|
104
|
|
|
$this->addStatusCode($statusCode, $description); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $this; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Purges the statusCodes array to populate with the new way. |
|
112
|
|
|
* |
|
113
|
|
|
* This method is required because the $statusCodes |
|
114
|
|
|
* is a private field, and the reflection is necessary. |
|
115
|
|
|
* |
|
116
|
|
|
* @return self |
|
117
|
|
|
*/ |
|
118
|
|
|
protected function initializeStatusCodes() |
|
119
|
|
|
{ |
|
120
|
|
|
$annotationReflection = new \ReflectionClass('Nelmio\ApiDocBundle\Annotation\ApiDoc'); |
|
121
|
|
|
$statusCodesReflection = $annotationReflection->getProperty('statusCodes'); |
|
122
|
|
|
$statusCodesReflection->setAccessible(true); |
|
123
|
|
|
$statusCodesReflection->setValue($this, []); |
|
124
|
|
|
|
|
125
|
|
|
return $this; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: