@@ -16,102 +16,102 @@ |
||
16 | 16 | class Path extends AbstractObject |
17 | 17 | { |
18 | 18 | |
19 | - private static $methods = array( |
|
20 | - 'get', |
|
21 | - 'put', |
|
22 | - 'post', |
|
23 | - 'delete', |
|
24 | - 'options', |
|
25 | - 'head', |
|
26 | - 'patch', |
|
27 | - ); |
|
28 | - |
|
29 | - /** |
|
30 | - * @var Operation[] $operation |
|
31 | - */ |
|
32 | - private $operations = []; |
|
33 | - |
|
34 | - /** |
|
35 | - * @var Tag|null $tag ; |
|
36 | - */ |
|
37 | - private $tag; |
|
38 | - |
|
39 | - public function __construct(AbstractObject $parent, ?Tag $Tag = null) |
|
40 | - { |
|
41 | - parent::__construct($parent); |
|
42 | - $this->tag = $Tag; |
|
43 | - } |
|
44 | - |
|
45 | - /** |
|
46 | - * @param string $command |
|
47 | - * @param string $data |
|
48 | - * @return AbstractObject|boolean |
|
49 | - * @throws Exception |
|
50 | - */ |
|
51 | - public function handleCommand($command, $data = null) |
|
52 | - { |
|
53 | - switch (strtolower($command)) { |
|
54 | - case 'method': // alias |
|
55 | - case 'operation': |
|
56 | - $method = strtolower(self::wordShift($data)); |
|
57 | - |
|
58 | - if (!in_array($method, self::$methods)) { |
|
59 | - throw new Exception('Unrecognized operation method \'' . $method . '\''); |
|
60 | - } |
|
61 | - |
|
62 | - if (isset($this->operations[$method])) { |
|
63 | - $Operation = $this->operations[$method]; |
|
64 | - } else { |
|
65 | - $summary = $data; |
|
66 | - $Operation = new Operation($this, $summary, $this->tag); |
|
67 | - $this->operations[$method] = $Operation; |
|
68 | - } |
|
69 | - |
|
70 | - return $Operation; |
|
71 | - |
|
72 | - case 'description': |
|
73 | - if ($this->tag) { |
|
74 | - return $this->tag->handleCommand($command, $data); |
|
75 | - } |
|
76 | - break; |
|
77 | - } |
|
78 | - |
|
79 | - return parent::handleCommand($command, $data); |
|
80 | - } |
|
81 | - |
|
82 | - public function toArray(): array |
|
83 | - { |
|
84 | - $methods = self::$methods; |
|
85 | - uksort($this->operations, static function ($a, $b) use ($methods) { |
|
86 | - return array_search($a, $methods, true) - array_search($b, $methods, true); |
|
87 | - }); |
|
88 | - |
|
89 | - return self::arrayFilterNull(array_merge( |
|
90 | - self::objectsToArray($this->operations) |
|
91 | - , parent::toArray())); |
|
92 | - } |
|
93 | - |
|
94 | - public function __toString() |
|
95 | - { |
|
96 | - end($this->operations); |
|
97 | - return __CLASS__ . ' ' . key($this->operations); |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * Check if an operation with the given id is registered to this Path. |
|
102 | - * |
|
103 | - * @param string $operationId |
|
104 | - * @return boolean |
|
105 | - */ |
|
106 | - public function hasOperationId($operationId): bool |
|
107 | - { |
|
108 | - foreach ($this->operations as $operation) { |
|
109 | - if ($operation->getId() === $operationId) { |
|
110 | - return true; |
|
111 | - } |
|
112 | - } |
|
113 | - |
|
114 | - return false; |
|
115 | - } |
|
19 | + private static $methods = array( |
|
20 | + 'get', |
|
21 | + 'put', |
|
22 | + 'post', |
|
23 | + 'delete', |
|
24 | + 'options', |
|
25 | + 'head', |
|
26 | + 'patch', |
|
27 | + ); |
|
28 | + |
|
29 | + /** |
|
30 | + * @var Operation[] $operation |
|
31 | + */ |
|
32 | + private $operations = []; |
|
33 | + |
|
34 | + /** |
|
35 | + * @var Tag|null $tag ; |
|
36 | + */ |
|
37 | + private $tag; |
|
38 | + |
|
39 | + public function __construct(AbstractObject $parent, ?Tag $Tag = null) |
|
40 | + { |
|
41 | + parent::__construct($parent); |
|
42 | + $this->tag = $Tag; |
|
43 | + } |
|
44 | + |
|
45 | + /** |
|
46 | + * @param string $command |
|
47 | + * @param string $data |
|
48 | + * @return AbstractObject|boolean |
|
49 | + * @throws Exception |
|
50 | + */ |
|
51 | + public function handleCommand($command, $data = null) |
|
52 | + { |
|
53 | + switch (strtolower($command)) { |
|
54 | + case 'method': // alias |
|
55 | + case 'operation': |
|
56 | + $method = strtolower(self::wordShift($data)); |
|
57 | + |
|
58 | + if (!in_array($method, self::$methods)) { |
|
59 | + throw new Exception('Unrecognized operation method \'' . $method . '\''); |
|
60 | + } |
|
61 | + |
|
62 | + if (isset($this->operations[$method])) { |
|
63 | + $Operation = $this->operations[$method]; |
|
64 | + } else { |
|
65 | + $summary = $data; |
|
66 | + $Operation = new Operation($this, $summary, $this->tag); |
|
67 | + $this->operations[$method] = $Operation; |
|
68 | + } |
|
69 | + |
|
70 | + return $Operation; |
|
71 | + |
|
72 | + case 'description': |
|
73 | + if ($this->tag) { |
|
74 | + return $this->tag->handleCommand($command, $data); |
|
75 | + } |
|
76 | + break; |
|
77 | + } |
|
78 | + |
|
79 | + return parent::handleCommand($command, $data); |
|
80 | + } |
|
81 | + |
|
82 | + public function toArray(): array |
|
83 | + { |
|
84 | + $methods = self::$methods; |
|
85 | + uksort($this->operations, static function ($a, $b) use ($methods) { |
|
86 | + return array_search($a, $methods, true) - array_search($b, $methods, true); |
|
87 | + }); |
|
88 | + |
|
89 | + return self::arrayFilterNull(array_merge( |
|
90 | + self::objectsToArray($this->operations) |
|
91 | + , parent::toArray())); |
|
92 | + } |
|
93 | + |
|
94 | + public function __toString() |
|
95 | + { |
|
96 | + end($this->operations); |
|
97 | + return __CLASS__ . ' ' . key($this->operations); |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * Check if an operation with the given id is registered to this Path. |
|
102 | + * |
|
103 | + * @param string $operationId |
|
104 | + * @return boolean |
|
105 | + */ |
|
106 | + public function hasOperationId($operationId): bool |
|
107 | + { |
|
108 | + foreach ($this->operations as $operation) { |
|
109 | + if ($operation->getId() === $operationId) { |
|
110 | + return true; |
|
111 | + } |
|
112 | + } |
|
113 | + |
|
114 | + return false; |
|
115 | + } |
|
116 | 116 | |
117 | 117 | } |
@@ -82,7 +82,7 @@ |
||
82 | 82 | public function toArray(): array |
83 | 83 | { |
84 | 84 | $methods = self::$methods; |
85 | - uksort($this->operations, static function ($a, $b) use ($methods) { |
|
85 | + uksort($this->operations, static function($a, $b) use ($methods) { |
|
86 | 86 | return array_search($a, $methods, true) - array_search($b, $methods, true); |
87 | 87 | }); |
88 | 88 |
@@ -17,88 +17,88 @@ |
||
17 | 17 | class Schema extends AbstractDocumentableObject implements IDefinition |
18 | 18 | { |
19 | 19 | |
20 | - /** |
|
21 | - * @var string |
|
22 | - */ |
|
23 | - private $description; |
|
24 | - |
|
25 | - /** |
|
26 | - * @var string |
|
27 | - */ |
|
28 | - private $title; |
|
29 | - |
|
30 | - /** |
|
31 | - * @var bool |
|
32 | - */ |
|
33 | - private $readOnly; |
|
34 | - |
|
35 | - /** |
|
36 | - * @var AbstractType |
|
37 | - */ |
|
38 | - private $type; |
|
39 | - |
|
40 | - /** |
|
41 | - * @param string $description |
|
42 | - * @throws Exception |
|
43 | - */ |
|
44 | - public function __construct(AbstractObject $parent, $definition = 'object', $description = null) |
|
45 | - { |
|
46 | - parent::__construct($parent); |
|
47 | - |
|
48 | - // Check if definition set |
|
49 | - if ($this->getSwagger()->hasDefinition($definition)) { |
|
50 | - $this->type = new Type\ReferenceObjectType($this, $definition); |
|
51 | - } else { |
|
52 | - $this->type = Type\AbstractType::typeFactory($this, $definition); |
|
53 | - } |
|
54 | - |
|
55 | - $this->description = $description; |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * @param string $command |
|
60 | - * @param string $data |
|
61 | - * @return AbstractObject|boolean |
|
62 | - * @throws Exception |
|
63 | - */ |
|
64 | - public function handleCommand($command, $data = null) |
|
65 | - { |
|
66 | - // Pass through to Type |
|
67 | - if ($this->type && $this->type->handleCommand($command, $data)) { |
|
68 | - return $this; |
|
69 | - } |
|
70 | - |
|
71 | - // handle all the rest manually |
|
72 | - switch (strtolower($command)) { |
|
73 | - case 'description': |
|
74 | - $this->description = $data; |
|
75 | - return $this; |
|
76 | - |
|
77 | - case 'title': |
|
78 | - $this->title = $data; |
|
79 | - return $this; |
|
80 | - } |
|
81 | - |
|
82 | - return parent::handleCommand($command, $data); |
|
83 | - } |
|
84 | - |
|
85 | - public function toArray(): array |
|
86 | - { |
|
87 | - return self::arrayFilterNull(array_merge($this->type->toArray(), array( |
|
88 | - 'title' => empty($this->title) ? null : $this->title, |
|
89 | - 'description' => empty($this->description) ? null : $this->description, |
|
90 | - 'readOnly' => $this->readOnly |
|
91 | - ), parent::toArray())); |
|
92 | - } |
|
93 | - |
|
94 | - public function __toString() |
|
95 | - { |
|
96 | - return __CLASS__; |
|
97 | - } |
|
98 | - |
|
99 | - public function setReadOnly(): void |
|
100 | - { |
|
101 | - $this->readOnly = true; |
|
102 | - } |
|
20 | + /** |
|
21 | + * @var string |
|
22 | + */ |
|
23 | + private $description; |
|
24 | + |
|
25 | + /** |
|
26 | + * @var string |
|
27 | + */ |
|
28 | + private $title; |
|
29 | + |
|
30 | + /** |
|
31 | + * @var bool |
|
32 | + */ |
|
33 | + private $readOnly; |
|
34 | + |
|
35 | + /** |
|
36 | + * @var AbstractType |
|
37 | + */ |
|
38 | + private $type; |
|
39 | + |
|
40 | + /** |
|
41 | + * @param string $description |
|
42 | + * @throws Exception |
|
43 | + */ |
|
44 | + public function __construct(AbstractObject $parent, $definition = 'object', $description = null) |
|
45 | + { |
|
46 | + parent::__construct($parent); |
|
47 | + |
|
48 | + // Check if definition set |
|
49 | + if ($this->getSwagger()->hasDefinition($definition)) { |
|
50 | + $this->type = new Type\ReferenceObjectType($this, $definition); |
|
51 | + } else { |
|
52 | + $this->type = Type\AbstractType::typeFactory($this, $definition); |
|
53 | + } |
|
54 | + |
|
55 | + $this->description = $description; |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * @param string $command |
|
60 | + * @param string $data |
|
61 | + * @return AbstractObject|boolean |
|
62 | + * @throws Exception |
|
63 | + */ |
|
64 | + public function handleCommand($command, $data = null) |
|
65 | + { |
|
66 | + // Pass through to Type |
|
67 | + if ($this->type && $this->type->handleCommand($command, $data)) { |
|
68 | + return $this; |
|
69 | + } |
|
70 | + |
|
71 | + // handle all the rest manually |
|
72 | + switch (strtolower($command)) { |
|
73 | + case 'description': |
|
74 | + $this->description = $data; |
|
75 | + return $this; |
|
76 | + |
|
77 | + case 'title': |
|
78 | + $this->title = $data; |
|
79 | + return $this; |
|
80 | + } |
|
81 | + |
|
82 | + return parent::handleCommand($command, $data); |
|
83 | + } |
|
84 | + |
|
85 | + public function toArray(): array |
|
86 | + { |
|
87 | + return self::arrayFilterNull(array_merge($this->type->toArray(), array( |
|
88 | + 'title' => empty($this->title) ? null : $this->title, |
|
89 | + 'description' => empty($this->description) ? null : $this->description, |
|
90 | + 'readOnly' => $this->readOnly |
|
91 | + ), parent::toArray())); |
|
92 | + } |
|
93 | + |
|
94 | + public function __toString() |
|
95 | + { |
|
96 | + return __CLASS__; |
|
97 | + } |
|
98 | + |
|
99 | + public function setReadOnly(): void |
|
100 | + { |
|
101 | + $this->readOnly = true; |
|
102 | + } |
|
103 | 103 | |
104 | 104 | } |
@@ -16,230 +16,230 @@ |
||
16 | 16 | class Operation extends AbstractDocumentableObject |
17 | 17 | { |
18 | 18 | |
19 | - private $tags = []; |
|
20 | - private $summary; |
|
21 | - private $description; |
|
22 | - private $consumes = []; |
|
23 | - private $produces = []; |
|
24 | - |
|
25 | - /** |
|
26 | - * @var IParameter[] |
|
27 | - */ |
|
28 | - private $parameters = []; |
|
29 | - private $responses = []; |
|
30 | - private $schemes = []; |
|
31 | - private $deprecated = false; |
|
32 | - private $security = []; |
|
33 | - |
|
34 | - /** |
|
35 | - * @var string |
|
36 | - */ |
|
37 | - private $operationId; |
|
38 | - |
|
39 | - /** |
|
40 | - * @param string $summary |
|
41 | - */ |
|
42 | - public function __construct(AbstractObject $parent, $summary = null, ?Tag $tag = null) |
|
43 | - { |
|
44 | - parent::__construct($parent); |
|
45 | - $this->summary = $summary; |
|
46 | - if ($tag) { |
|
47 | - $this->tags[] = $tag->getName(); |
|
48 | - } |
|
49 | - } |
|
50 | - |
|
51 | - public function getConsumes(): array |
|
52 | - { |
|
53 | - return $this->consumes; |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * @param string $command |
|
58 | - * @param string $data |
|
59 | - * @return AbstractObject|boolean |
|
60 | - * @throws Exception |
|
61 | - * @throws Exception |
|
62 | - * @throws Exception |
|
63 | - * @throws Exception |
|
64 | - * @throws Exception |
|
65 | - * @throws Exception |
|
66 | - * @throws Exception |
|
67 | - */ |
|
68 | - public function handleCommand($command, $data = null) |
|
69 | - { |
|
70 | - switch (strtolower($command)) { |
|
71 | - // string |
|
72 | - case 'summary': |
|
73 | - case 'description': |
|
74 | - $this->$command = $data; |
|
75 | - return $this; |
|
76 | - |
|
77 | - // string[] |
|
78 | - case 'tags': |
|
79 | - case 'schemes': |
|
80 | - $this->$command = array_merge($this->$command, self::wordSplit($data)); |
|
81 | - return $this; |
|
82 | - |
|
83 | - // MIME[] |
|
84 | - case 'consumes': |
|
85 | - case 'produces': |
|
86 | - $this->$command = array_merge($this->$command, self::translateMimeTypes(self::wordSplit($data))); |
|
87 | - return $this; |
|
88 | - |
|
89 | - // boolean |
|
90 | - case 'deprecated': |
|
91 | - $this->deprecated = true; |
|
92 | - return $this; |
|
93 | - |
|
94 | - case 'error': |
|
95 | - $code = self::wordShift($data); |
|
96 | - $reasoncode = Response::getCode($code); |
|
97 | - if ($reasoncode === null) { |
|
98 | - throw new Exception("Invalid error code: '$code'"); |
|
99 | - } |
|
100 | - $description = $data; |
|
101 | - $Error = new Error($this, $reasoncode, $description); |
|
102 | - $this->responses[$reasoncode] = $Error; |
|
103 | - return $Error; |
|
104 | - |
|
105 | - case 'errors': |
|
106 | - foreach (self::wordSplit($data) as $code) { |
|
107 | - $reasoncode = Response::getCode($code); |
|
108 | - if ($reasoncode === null) { |
|
109 | - throw new Exception("Invalid error code: '$code'"); |
|
110 | - } |
|
111 | - $this->responses[$reasoncode] = new Error($this, $reasoncode); |
|
112 | - } |
|
113 | - return $this; |
|
114 | - |
|
115 | - case 'path': |
|
116 | - case 'query': |
|
117 | - case 'query?': |
|
118 | - case 'header': |
|
119 | - case 'header?': |
|
120 | - case 'form': |
|
121 | - case 'form?': |
|
122 | - $in = rtrim($command, '?'); |
|
123 | - $parameter = new Parameter($this, $in, $data, !str_ends_with($command, '?')); |
|
124 | - $this->parameters[$parameter->getName()] = $parameter; |
|
125 | - return $parameter; |
|
126 | - |
|
127 | - case 'body': |
|
128 | - case 'body?': |
|
129 | - $parameter = new BodyParameter($this, $data, !str_ends_with($command, '?')); |
|
130 | - $this->parameters[$parameter->getName()] = $parameter; |
|
131 | - return $parameter; |
|
132 | - |
|
133 | - case 'param': |
|
134 | - case 'parameter': |
|
135 | - $parameter = new ParameterReference($this, $data); |
|
136 | - $this->parameters[$parameter->getName()] = $parameter; |
|
137 | - return $this; |
|
138 | - |
|
139 | - case 'response': |
|
140 | - $code = self::wordShift($data); |
|
141 | - $reasoncode = Response::getCode($code); |
|
142 | - if ($reasoncode === null) { |
|
143 | - $reference = $code; |
|
144 | - $code = self::wordShift($data); |
|
145 | - $reasoncode = Response::getCode($code); |
|
146 | - if ($reasoncode === null) { |
|
147 | - throw new Exception("Invalid response code: '$reference'"); |
|
148 | - } |
|
149 | - $this->responses[$reasoncode] = new ResponseReference($this, $reference); |
|
150 | - return $this; |
|
151 | - } |
|
152 | - |
|
153 | - $definition = self::wordShift($data); |
|
154 | - $description = $data; |
|
155 | - $Response = new Response($this, $reasoncode, $definition, $description); |
|
156 | - $this->responses[$reasoncode] = $Response; |
|
157 | - return $Response; |
|
158 | - |
|
159 | - case 'require': |
|
160 | - $name = self::wordShift($data); |
|
161 | - if (empty($name)) { |
|
162 | - throw new Exception('Empty security requirement name'); |
|
163 | - } |
|
164 | - $scopes = self::wordSplit($data); |
|
165 | - sort($scopes); |
|
166 | - $this->security[] = array( |
|
167 | - $name => empty($scopes) ? [] : $scopes, |
|
168 | - ); |
|
169 | - return $this; |
|
170 | - |
|
171 | - case 'id': |
|
172 | - $operationId = self::trim($data); |
|
173 | - if ($this->getSwagger()->hasOperationId($operationId)) { |
|
174 | - throw new Exception("Duplicate operation id '{$operationId}'"); |
|
175 | - } |
|
176 | - $this->operationId = $operationId; |
|
177 | - return $this; |
|
178 | - } |
|
179 | - |
|
180 | - return parent::handleCommand($command, $data); |
|
181 | - } |
|
182 | - |
|
183 | - /** |
|
184 | - * @throws Exception |
|
185 | - */ |
|
186 | - public function toArray(): array |
|
187 | - { |
|
188 | - if (empty($this->responses)) { |
|
189 | - throw new Exception('No response defined for operation'); |
|
190 | - } |
|
191 | - ksort($this->responses); |
|
192 | - |
|
193 | - $tags = array_unique($this->tags); |
|
194 | - sort($tags); |
|
195 | - |
|
196 | - $schemes = array_unique($this->schemes); |
|
197 | - sort($schemes); |
|
198 | - |
|
199 | - $consumes = array_unique($this->consumes); |
|
200 | - sort($consumes); |
|
201 | - |
|
202 | - $produces = array_unique($this->produces); |
|
203 | - sort($produces); |
|
204 | - |
|
205 | - foreach ($this->security as $security) { |
|
206 | - foreach ($security as $name => $scope) { |
|
207 | - if ($this->getSwagger()->getSecurity($name) === false) { |
|
208 | - throw new Exception("Required security scheme not defined: '{$name}'"); |
|
209 | - } |
|
210 | - } |
|
211 | - } |
|
212 | - |
|
213 | - $parameters = $this->parameters ? array_values($this->parameters) : null; |
|
214 | - |
|
215 | - return self::arrayFilterNull(array_merge(array( |
|
216 | - 'deprecated' => $this->deprecated ? true : null, |
|
217 | - 'tags' => $tags, |
|
218 | - 'summary' => empty($this->summary) ? null : $this->summary, |
|
219 | - 'description' => empty($this->description) ? null : $this->description, |
|
220 | - 'operationId' => $this->operationId, |
|
221 | - 'consumes' => $consumes, |
|
222 | - 'produces' => $produces, |
|
223 | - 'parameters' => $parameters ? self::objectsToArray($parameters) : null, |
|
224 | - 'schemes' => $schemes, |
|
225 | - 'responses' => $this->responses ? self::objectsToArray($this->responses) : null, |
|
226 | - 'security' => $this->security, |
|
227 | - ), parent::toArray())); |
|
228 | - } |
|
229 | - |
|
230 | - /** |
|
231 | - * Return the operation ID |
|
232 | - * |
|
233 | - * @return string|null |
|
234 | - */ |
|
235 | - public function getId(): ?string |
|
236 | - { |
|
237 | - return $this->operationId; |
|
238 | - } |
|
239 | - |
|
240 | - public function __toString() |
|
241 | - { |
|
242 | - return __CLASS__ . ' ' . $this->summary; |
|
243 | - } |
|
19 | + private $tags = []; |
|
20 | + private $summary; |
|
21 | + private $description; |
|
22 | + private $consumes = []; |
|
23 | + private $produces = []; |
|
24 | + |
|
25 | + /** |
|
26 | + * @var IParameter[] |
|
27 | + */ |
|
28 | + private $parameters = []; |
|
29 | + private $responses = []; |
|
30 | + private $schemes = []; |
|
31 | + private $deprecated = false; |
|
32 | + private $security = []; |
|
33 | + |
|
34 | + /** |
|
35 | + * @var string |
|
36 | + */ |
|
37 | + private $operationId; |
|
38 | + |
|
39 | + /** |
|
40 | + * @param string $summary |
|
41 | + */ |
|
42 | + public function __construct(AbstractObject $parent, $summary = null, ?Tag $tag = null) |
|
43 | + { |
|
44 | + parent::__construct($parent); |
|
45 | + $this->summary = $summary; |
|
46 | + if ($tag) { |
|
47 | + $this->tags[] = $tag->getName(); |
|
48 | + } |
|
49 | + } |
|
50 | + |
|
51 | + public function getConsumes(): array |
|
52 | + { |
|
53 | + return $this->consumes; |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * @param string $command |
|
58 | + * @param string $data |
|
59 | + * @return AbstractObject|boolean |
|
60 | + * @throws Exception |
|
61 | + * @throws Exception |
|
62 | + * @throws Exception |
|
63 | + * @throws Exception |
|
64 | + * @throws Exception |
|
65 | + * @throws Exception |
|
66 | + * @throws Exception |
|
67 | + */ |
|
68 | + public function handleCommand($command, $data = null) |
|
69 | + { |
|
70 | + switch (strtolower($command)) { |
|
71 | + // string |
|
72 | + case 'summary': |
|
73 | + case 'description': |
|
74 | + $this->$command = $data; |
|
75 | + return $this; |
|
76 | + |
|
77 | + // string[] |
|
78 | + case 'tags': |
|
79 | + case 'schemes': |
|
80 | + $this->$command = array_merge($this->$command, self::wordSplit($data)); |
|
81 | + return $this; |
|
82 | + |
|
83 | + // MIME[] |
|
84 | + case 'consumes': |
|
85 | + case 'produces': |
|
86 | + $this->$command = array_merge($this->$command, self::translateMimeTypes(self::wordSplit($data))); |
|
87 | + return $this; |
|
88 | + |
|
89 | + // boolean |
|
90 | + case 'deprecated': |
|
91 | + $this->deprecated = true; |
|
92 | + return $this; |
|
93 | + |
|
94 | + case 'error': |
|
95 | + $code = self::wordShift($data); |
|
96 | + $reasoncode = Response::getCode($code); |
|
97 | + if ($reasoncode === null) { |
|
98 | + throw new Exception("Invalid error code: '$code'"); |
|
99 | + } |
|
100 | + $description = $data; |
|
101 | + $Error = new Error($this, $reasoncode, $description); |
|
102 | + $this->responses[$reasoncode] = $Error; |
|
103 | + return $Error; |
|
104 | + |
|
105 | + case 'errors': |
|
106 | + foreach (self::wordSplit($data) as $code) { |
|
107 | + $reasoncode = Response::getCode($code); |
|
108 | + if ($reasoncode === null) { |
|
109 | + throw new Exception("Invalid error code: '$code'"); |
|
110 | + } |
|
111 | + $this->responses[$reasoncode] = new Error($this, $reasoncode); |
|
112 | + } |
|
113 | + return $this; |
|
114 | + |
|
115 | + case 'path': |
|
116 | + case 'query': |
|
117 | + case 'query?': |
|
118 | + case 'header': |
|
119 | + case 'header?': |
|
120 | + case 'form': |
|
121 | + case 'form?': |
|
122 | + $in = rtrim($command, '?'); |
|
123 | + $parameter = new Parameter($this, $in, $data, !str_ends_with($command, '?')); |
|
124 | + $this->parameters[$parameter->getName()] = $parameter; |
|
125 | + return $parameter; |
|
126 | + |
|
127 | + case 'body': |
|
128 | + case 'body?': |
|
129 | + $parameter = new BodyParameter($this, $data, !str_ends_with($command, '?')); |
|
130 | + $this->parameters[$parameter->getName()] = $parameter; |
|
131 | + return $parameter; |
|
132 | + |
|
133 | + case 'param': |
|
134 | + case 'parameter': |
|
135 | + $parameter = new ParameterReference($this, $data); |
|
136 | + $this->parameters[$parameter->getName()] = $parameter; |
|
137 | + return $this; |
|
138 | + |
|
139 | + case 'response': |
|
140 | + $code = self::wordShift($data); |
|
141 | + $reasoncode = Response::getCode($code); |
|
142 | + if ($reasoncode === null) { |
|
143 | + $reference = $code; |
|
144 | + $code = self::wordShift($data); |
|
145 | + $reasoncode = Response::getCode($code); |
|
146 | + if ($reasoncode === null) { |
|
147 | + throw new Exception("Invalid response code: '$reference'"); |
|
148 | + } |
|
149 | + $this->responses[$reasoncode] = new ResponseReference($this, $reference); |
|
150 | + return $this; |
|
151 | + } |
|
152 | + |
|
153 | + $definition = self::wordShift($data); |
|
154 | + $description = $data; |
|
155 | + $Response = new Response($this, $reasoncode, $definition, $description); |
|
156 | + $this->responses[$reasoncode] = $Response; |
|
157 | + return $Response; |
|
158 | + |
|
159 | + case 'require': |
|
160 | + $name = self::wordShift($data); |
|
161 | + if (empty($name)) { |
|
162 | + throw new Exception('Empty security requirement name'); |
|
163 | + } |
|
164 | + $scopes = self::wordSplit($data); |
|
165 | + sort($scopes); |
|
166 | + $this->security[] = array( |
|
167 | + $name => empty($scopes) ? [] : $scopes, |
|
168 | + ); |
|
169 | + return $this; |
|
170 | + |
|
171 | + case 'id': |
|
172 | + $operationId = self::trim($data); |
|
173 | + if ($this->getSwagger()->hasOperationId($operationId)) { |
|
174 | + throw new Exception("Duplicate operation id '{$operationId}'"); |
|
175 | + } |
|
176 | + $this->operationId = $operationId; |
|
177 | + return $this; |
|
178 | + } |
|
179 | + |
|
180 | + return parent::handleCommand($command, $data); |
|
181 | + } |
|
182 | + |
|
183 | + /** |
|
184 | + * @throws Exception |
|
185 | + */ |
|
186 | + public function toArray(): array |
|
187 | + { |
|
188 | + if (empty($this->responses)) { |
|
189 | + throw new Exception('No response defined for operation'); |
|
190 | + } |
|
191 | + ksort($this->responses); |
|
192 | + |
|
193 | + $tags = array_unique($this->tags); |
|
194 | + sort($tags); |
|
195 | + |
|
196 | + $schemes = array_unique($this->schemes); |
|
197 | + sort($schemes); |
|
198 | + |
|
199 | + $consumes = array_unique($this->consumes); |
|
200 | + sort($consumes); |
|
201 | + |
|
202 | + $produces = array_unique($this->produces); |
|
203 | + sort($produces); |
|
204 | + |
|
205 | + foreach ($this->security as $security) { |
|
206 | + foreach ($security as $name => $scope) { |
|
207 | + if ($this->getSwagger()->getSecurity($name) === false) { |
|
208 | + throw new Exception("Required security scheme not defined: '{$name}'"); |
|
209 | + } |
|
210 | + } |
|
211 | + } |
|
212 | + |
|
213 | + $parameters = $this->parameters ? array_values($this->parameters) : null; |
|
214 | + |
|
215 | + return self::arrayFilterNull(array_merge(array( |
|
216 | + 'deprecated' => $this->deprecated ? true : null, |
|
217 | + 'tags' => $tags, |
|
218 | + 'summary' => empty($this->summary) ? null : $this->summary, |
|
219 | + 'description' => empty($this->description) ? null : $this->description, |
|
220 | + 'operationId' => $this->operationId, |
|
221 | + 'consumes' => $consumes, |
|
222 | + 'produces' => $produces, |
|
223 | + 'parameters' => $parameters ? self::objectsToArray($parameters) : null, |
|
224 | + 'schemes' => $schemes, |
|
225 | + 'responses' => $this->responses ? self::objectsToArray($this->responses) : null, |
|
226 | + 'security' => $this->security, |
|
227 | + ), parent::toArray())); |
|
228 | + } |
|
229 | + |
|
230 | + /** |
|
231 | + * Return the operation ID |
|
232 | + * |
|
233 | + * @return string|null |
|
234 | + */ |
|
235 | + public function getId(): ?string |
|
236 | + { |
|
237 | + return $this->operationId; |
|
238 | + } |
|
239 | + |
|
240 | + public function __toString() |
|
241 | + { |
|
242 | + return __CLASS__ . ' ' . $this->summary; |
|
243 | + } |
|
244 | 244 | |
245 | 245 | } |
@@ -15,77 +15,77 @@ |
||
15 | 15 | class BodyParameter extends AbstractObject implements IParameter |
16 | 16 | { |
17 | 17 | |
18 | - /** |
|
19 | - * @var string|boolean |
|
20 | - */ |
|
21 | - private $name; |
|
22 | - private $description; |
|
23 | - private $required; |
|
24 | - |
|
25 | - /** |
|
26 | - * @var Schema |
|
27 | - */ |
|
28 | - private $schema; |
|
29 | - |
|
30 | - /** |
|
31 | - * @throws Exception |
|
32 | - */ |
|
33 | - public function __construct(AbstractObject $parent, $data, $required = false) |
|
34 | - { |
|
35 | - parent::__construct($parent); |
|
36 | - |
|
37 | - $type = self::wordShift($data); |
|
38 | - if (empty($type)) { |
|
39 | - throw new Exception('No type definition for body parameter'); |
|
40 | - } |
|
41 | - |
|
42 | - $this->name = self::wordShift($data); |
|
43 | - if (empty($this->name)) { |
|
44 | - throw new Exception('No name for body parameter'); |
|
45 | - } |
|
46 | - |
|
47 | - $this->description = $data; |
|
48 | - $this->required = (bool)$required; |
|
49 | - |
|
50 | - $this->schema = new Schema($this, $type); |
|
51 | - } |
|
52 | - |
|
53 | - /** |
|
54 | - * @param string $command |
|
55 | - * @param string $data |
|
56 | - * @return AbstractObject|boolean |
|
57 | - * @throws Exception |
|
58 | - */ |
|
59 | - public function handleCommand($command, $data = null) |
|
60 | - { |
|
61 | - // Pass through to Type |
|
62 | - $return = $this->schema->handleCommand($command, $data); |
|
63 | - if ($return) { |
|
64 | - return $return; |
|
65 | - } |
|
66 | - |
|
67 | - return parent::handleCommand($command, $data); |
|
68 | - } |
|
69 | - |
|
70 | - public function toArray(): array |
|
71 | - { |
|
72 | - return self::arrayFilterNull(array_merge(array( |
|
73 | - 'name' => $this->name, |
|
74 | - 'in' => 'body', |
|
75 | - 'description' => empty($this->description) ? null : $this->description, |
|
76 | - 'required' => $this->required ? true : null, |
|
77 | - 'schema' => $this->schema->toArray(), |
|
78 | - ), parent::toArray())); |
|
79 | - } |
|
80 | - |
|
81 | - public function __toString() |
|
82 | - { |
|
83 | - return __CLASS__ . ' ' . $this->name; |
|
84 | - } |
|
85 | - |
|
86 | - public function getName() |
|
87 | - { |
|
88 | - return $this->name; |
|
89 | - } |
|
18 | + /** |
|
19 | + * @var string|boolean |
|
20 | + */ |
|
21 | + private $name; |
|
22 | + private $description; |
|
23 | + private $required; |
|
24 | + |
|
25 | + /** |
|
26 | + * @var Schema |
|
27 | + */ |
|
28 | + private $schema; |
|
29 | + |
|
30 | + /** |
|
31 | + * @throws Exception |
|
32 | + */ |
|
33 | + public function __construct(AbstractObject $parent, $data, $required = false) |
|
34 | + { |
|
35 | + parent::__construct($parent); |
|
36 | + |
|
37 | + $type = self::wordShift($data); |
|
38 | + if (empty($type)) { |
|
39 | + throw new Exception('No type definition for body parameter'); |
|
40 | + } |
|
41 | + |
|
42 | + $this->name = self::wordShift($data); |
|
43 | + if (empty($this->name)) { |
|
44 | + throw new Exception('No name for body parameter'); |
|
45 | + } |
|
46 | + |
|
47 | + $this->description = $data; |
|
48 | + $this->required = (bool)$required; |
|
49 | + |
|
50 | + $this->schema = new Schema($this, $type); |
|
51 | + } |
|
52 | + |
|
53 | + /** |
|
54 | + * @param string $command |
|
55 | + * @param string $data |
|
56 | + * @return AbstractObject|boolean |
|
57 | + * @throws Exception |
|
58 | + */ |
|
59 | + public function handleCommand($command, $data = null) |
|
60 | + { |
|
61 | + // Pass through to Type |
|
62 | + $return = $this->schema->handleCommand($command, $data); |
|
63 | + if ($return) { |
|
64 | + return $return; |
|
65 | + } |
|
66 | + |
|
67 | + return parent::handleCommand($command, $data); |
|
68 | + } |
|
69 | + |
|
70 | + public function toArray(): array |
|
71 | + { |
|
72 | + return self::arrayFilterNull(array_merge(array( |
|
73 | + 'name' => $this->name, |
|
74 | + 'in' => 'body', |
|
75 | + 'description' => empty($this->description) ? null : $this->description, |
|
76 | + 'required' => $this->required ? true : null, |
|
77 | + 'schema' => $this->schema->toArray(), |
|
78 | + ), parent::toArray())); |
|
79 | + } |
|
80 | + |
|
81 | + public function __toString() |
|
82 | + { |
|
83 | + return __CLASS__ . ' ' . $this->name; |
|
84 | + } |
|
85 | + |
|
86 | + public function getName() |
|
87 | + { |
|
88 | + return $this->name; |
|
89 | + } |
|
90 | 90 | |
91 | 91 | } |
@@ -16,379 +16,379 @@ |
||
16 | 16 | class Swagger extends AbstractDocumentableObject |
17 | 17 | { |
18 | 18 | |
19 | - private $swagger = '2.0'; |
|
20 | - private $host; |
|
21 | - private $basePath; |
|
22 | - |
|
23 | - /** |
|
24 | - * @var TypeRegistry |
|
25 | - */ |
|
26 | - private $typeRegistry; |
|
27 | - |
|
28 | - /** |
|
29 | - * @var Info $Info |
|
30 | - */ |
|
31 | - private $info; |
|
32 | - private $schemes = []; |
|
33 | - private $consumes = []; |
|
34 | - private $produces = []; |
|
35 | - |
|
36 | - /** |
|
37 | - * @var Path[] $Paths |
|
38 | - */ |
|
39 | - private $paths = []; |
|
40 | - |
|
41 | - /** |
|
42 | - * @var Schema[] $definitions |
|
43 | - */ |
|
44 | - private $definitions = []; |
|
45 | - |
|
46 | - /** |
|
47 | - * @var IParameter[] $parameters |
|
48 | - */ |
|
49 | - private $parameters = []; |
|
50 | - |
|
51 | - /** |
|
52 | - * @var Response[] $responses |
|
53 | - */ |
|
54 | - private $responses = []; |
|
55 | - |
|
56 | - /** |
|
57 | - * @var Tag[] $Tags |
|
58 | - */ |
|
59 | - private $tags = []; |
|
60 | - |
|
61 | - /** |
|
62 | - * Default tag for new endpoints/operations. Set by the api command. |
|
63 | - * |
|
64 | - * @var Tag |
|
65 | - */ |
|
66 | - private $defaultTag; |
|
67 | - private $securityDefinitions = []; |
|
68 | - private $security = []; |
|
69 | - |
|
70 | - /** |
|
71 | - * @param string $host |
|
72 | - * @param string $basePath |
|
73 | - * @param TypeRegistry $typeRegistry |
|
74 | - */ |
|
75 | - public function __construct($host = null, $basePath = null, $typeRegistry = null) |
|
76 | - { |
|
77 | - parent::__construct(); |
|
78 | - |
|
79 | - $this->host = $host; |
|
80 | - $this->basePath = $basePath; |
|
81 | - |
|
82 | - $this->info = new Info($this); |
|
83 | - |
|
84 | - $this->typeRegistry = $typeRegistry ?: new TypeRegistry; |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * Return all consumes |
|
89 | - * |
|
90 | - * @return array |
|
91 | - * @todo Deprecate in favour of a getConsume($name); |
|
92 | - */ |
|
93 | - public function getConsumes(): array |
|
94 | - { |
|
95 | - return $this->consumes; |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * Return the named security if it exists, otherwise return FALSE |
|
100 | - * |
|
101 | - * @param string $name |
|
102 | - * |
|
103 | - * @return boolean|SecurityScheme |
|
104 | - */ |
|
105 | - public function getSecurity($name) |
|
106 | - { |
|
107 | - return $this->securityDefinitions[$name] ?? false; |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * @param string $command |
|
112 | - * @param string $data |
|
113 | - * |
|
114 | - * @return AbstractObject|boolean |
|
115 | - * @throws Exception |
|
116 | - * @throws Exception |
|
117 | - * @throws Exception |
|
118 | - * @throws Exception |
|
119 | - * @throws Exception |
|
120 | - * @throws Exception |
|
121 | - * @throws Exception |
|
122 | - * @throws Exception |
|
123 | - * @throws Exception |
|
124 | - */ |
|
125 | - public function handleCommand($command, $data = null) |
|
126 | - { |
|
127 | - switch (strtolower($command)) { |
|
128 | - // pass to Info |
|
129 | - case 'title': |
|
130 | - case 'description': |
|
131 | - case 'version': |
|
132 | - case 'terms': // alias |
|
133 | - case 'tos': // alias |
|
134 | - case 'termsofservice': |
|
135 | - case 'contact': |
|
136 | - case 'license': |
|
137 | - return $this->info->handleCommand($command, $data); |
|
138 | - |
|
139 | - // string[] |
|
140 | - case 'scheme': |
|
141 | - case 'schemes': |
|
142 | - $this->schemes = array_unique(array_merge($this->schemes, self::wordSplit($data))); |
|
143 | - |
|
144 | - return $this; |
|
145 | - |
|
146 | - // MIME[] |
|
147 | - case 'consume': |
|
148 | - case 'consumes': |
|
149 | - $this->consumes = array_merge($this->consumes, self::translateMimeTypes(self::wordSplit($data))); |
|
150 | - |
|
151 | - return $this; |
|
152 | - |
|
153 | - case 'produce': |
|
154 | - case 'produces': |
|
155 | - $this->produces = array_merge($this->produces, self::translateMimeTypes(self::wordSplit($data))); |
|
156 | - |
|
157 | - return $this; |
|
158 | - |
|
159 | - case 'model': |
|
160 | - case 'model!': |
|
161 | - case 'definition': |
|
162 | - case 'definition!': |
|
163 | - $name = self::wordShift($data); |
|
164 | - if (empty($name)) { |
|
165 | - throw new Exception('Missing definition name'); |
|
166 | - } |
|
167 | - $typeDef = self::wordShift($data); |
|
168 | - if (empty($typeDef)) { |
|
169 | - $typeDef = 'object'; |
|
170 | - } |
|
171 | - |
|
172 | - $definition = new Schema($this, $typeDef); |
|
173 | - if (str_ends_with($command, '!')) { |
|
174 | - $definition->setReadOnly(); |
|
175 | - } |
|
176 | - $this->definitions[$name] = $definition; |
|
177 | - |
|
178 | - return $definition; |
|
179 | - |
|
180 | - case 'path': |
|
181 | - case 'query': |
|
182 | - case 'query?': |
|
183 | - case 'header': |
|
184 | - case 'header?': |
|
185 | - case 'form': |
|
186 | - case 'form?': |
|
187 | - $in = rtrim($command, '?'); |
|
188 | - $Parameter = new Parameter($this, $in, $data, !str_ends_with($command, '?')); |
|
189 | - $this->parameters[$Parameter->getName()] = $Parameter; |
|
190 | - |
|
191 | - return $Parameter; |
|
192 | - |
|
193 | - case 'body': |
|
194 | - case 'body?': |
|
195 | - $Parameter = new BodyParameter($this, $data, !str_ends_with($command, '?')); |
|
196 | - $this->parameters[$Parameter->getName()] = $Parameter; |
|
197 | - |
|
198 | - return $Parameter; |
|
199 | - |
|
200 | - case 'response': |
|
201 | - $name = self::wordShift($data); |
|
202 | - $definition = self::wordShift($data); |
|
203 | - $description = $data; |
|
204 | - if (empty($description)) { |
|
205 | - throw new Exception('Response definition missing description'); |
|
206 | - } |
|
207 | - $Response = new Response($this, $name, $definition === 'null' ? null : $definition, $description); |
|
208 | - $this->responses[$name] = $Response; |
|
209 | - |
|
210 | - return $Response; |
|
211 | - |
|
212 | - case 'api': // alias |
|
213 | - case 'tag': |
|
214 | - $tagname = self::wordShift($data); |
|
215 | - if (empty($tagname)) { |
|
216 | - throw new Exception('Missing tag name'); |
|
217 | - } |
|
218 | - |
|
219 | - $Tag = null; |
|
220 | - foreach ($this->tags as $T) { |
|
221 | - if ($T->getName() === $tagname) { |
|
222 | - $Tag = $T; |
|
223 | - break; |
|
224 | - } |
|
225 | - } |
|
226 | - if (!$Tag) { |
|
227 | - $Tag = new Tag($this, $tagname, $data); |
|
228 | - $this->tags[] = $Tag; |
|
229 | - } |
|
230 | - |
|
231 | - // backwards compatibility |
|
232 | - if ($command === 'api') { |
|
233 | - $this->defaultTag = $Tag; |
|
234 | - } |
|
235 | - |
|
236 | - return $Tag; |
|
237 | - |
|
238 | - case 'endpoint': |
|
239 | - $path = self::wordShift($data); |
|
240 | - if (false === $path) { |
|
241 | - $path = '/'; |
|
242 | - } else if ($path[0] !== '/') { |
|
243 | - $path = '/' . $path; |
|
244 | - } |
|
245 | - |
|
246 | - $Tag = null; |
|
247 | - if (($tagname = self::wordShift($data)) !== false) { |
|
248 | - foreach ($this->tags as $T) { |
|
249 | - if (strtolower($T->getName()) === strtolower($tagname)) { |
|
250 | - $Tag = $T; |
|
251 | - break; |
|
252 | - } |
|
253 | - } |
|
254 | - if (!$Tag) { |
|
255 | - $Tag = new Tag($this, $tagname, $data); |
|
256 | - $this->tags[] = $Tag; |
|
257 | - } |
|
258 | - } |
|
259 | - |
|
260 | - if (!isset($this->paths[$path])) { |
|
261 | - $this->paths[$path] = new Path($this, $Tag ?: $this->defaultTag); |
|
262 | - } |
|
263 | - |
|
264 | - return $this->paths[$path]; |
|
265 | - |
|
266 | - case 'security': |
|
267 | - $name = self::wordShift($data); |
|
268 | - if (empty($name)) { |
|
269 | - throw new Exception('Missing security name'); |
|
270 | - } |
|
271 | - $type = self::wordShift($data); |
|
272 | - if (empty($type)) { |
|
273 | - throw new Exception('Missing security type'); |
|
274 | - } |
|
275 | - $SecurityScheme = new SecurityScheme($this, $type, $data); |
|
276 | - $this->securityDefinitions[$name] = $SecurityScheme; |
|
277 | - |
|
278 | - return $SecurityScheme; |
|
279 | - |
|
280 | - case 'require': |
|
281 | - $name = self::wordShift($data); |
|
282 | - if (empty($name)) { |
|
283 | - throw new Exception('Missing require name'); |
|
284 | - } |
|
285 | - $scopes = self::wordSplit($data); |
|
286 | - sort($scopes); |
|
287 | - $this->security[] = [ |
|
288 | - $name => empty($scopes) ? [] : $scopes, |
|
289 | - ]; |
|
290 | - |
|
291 | - return $this; |
|
292 | - } |
|
293 | - |
|
294 | - return parent::handleCommand($command, $data); |
|
295 | - } |
|
296 | - |
|
297 | - /** |
|
298 | - * @inheritDoc |
|
299 | - * @throws Exception |
|
300 | - * @throws Exception |
|
301 | - */ |
|
302 | - public function toArray(): array |
|
303 | - { |
|
304 | - if (empty($this->paths)) { |
|
305 | - throw new Exception('No path defined'); |
|
306 | - } |
|
307 | - |
|
308 | - $schemes = array_unique($this->schemes); |
|
309 | - sort($schemes); |
|
310 | - |
|
311 | - $consumes = array_unique($this->consumes); |
|
312 | - sort($consumes); |
|
313 | - |
|
314 | - $produces = array_unique($this->produces); |
|
315 | - sort($produces); |
|
316 | - |
|
317 | - foreach ($this->security as $security) { |
|
318 | - foreach ($security as $name => $scopes) { |
|
319 | - if (!isset($this->securityDefinitions[$name])) { |
|
320 | - throw new Exception('Required security scheme not defined: \'' . $name . '\''); |
|
321 | - } |
|
322 | - } |
|
323 | - } |
|
324 | - |
|
325 | - return self::arrayFilterNull(array_merge([ |
|
326 | - 'swagger' => $this->swagger, |
|
327 | - 'info' => $this->info->toArray(), |
|
328 | - 'host' => empty($this->host) ? null : $this->host, |
|
329 | - 'basePath' => empty($this->basePath) ? null : $this->basePath, |
|
330 | - 'consumes' => $consumes, |
|
331 | - 'produces' => $produces, |
|
332 | - 'schemes' => $schemes, |
|
333 | - 'paths' => self::objectsToArray($this->paths), |
|
334 | - 'definitions' => self::objectsToArray($this->definitions), |
|
335 | - 'parameters' => self::objectsToArray($this->parameters), |
|
336 | - 'responses' => self::objectsToArray($this->responses), |
|
337 | - 'securityDefinitions' => self::objectsToArray($this->securityDefinitions), |
|
338 | - 'security' => $this->security, |
|
339 | - 'tags' => self::objectsToArray($this->tags), |
|
340 | - ], parent::toArray())); |
|
341 | - } |
|
342 | - |
|
343 | - public function __toString() |
|
344 | - { |
|
345 | - return __CLASS__; |
|
346 | - } |
|
347 | - |
|
348 | - /** |
|
349 | - * Check if an operation with the given id exists. |
|
350 | - * |
|
351 | - * @param string $operationId |
|
352 | - * |
|
353 | - * @return boolean |
|
354 | - */ |
|
355 | - public function hasOperationId($operationId): bool |
|
356 | - { |
|
357 | - foreach ($this->paths as $path) { |
|
358 | - if ($path->hasOperationId($operationId)) { |
|
359 | - return true; |
|
360 | - } |
|
361 | - } |
|
362 | - |
|
363 | - return false; |
|
364 | - } |
|
365 | - |
|
366 | - /** |
|
367 | - * Check if a definition with the given name exists |
|
368 | - * |
|
369 | - * @param string $name |
|
370 | - * |
|
371 | - * @return boolean |
|
372 | - */ |
|
373 | - public function hasDefinition($name): bool |
|
374 | - { |
|
375 | - return isset($this->definitions[$name]); |
|
376 | - } |
|
377 | - |
|
378 | - /** |
|
379 | - * @inheritDoc |
|
380 | - */ |
|
381 | - protected function getSwagger(): Swagger |
|
382 | - { |
|
383 | - return $this; |
|
384 | - } |
|
385 | - |
|
386 | - /** |
|
387 | - * @inheritDoc |
|
388 | - */ |
|
389 | - protected function getTypeRegistry(): TypeRegistry |
|
390 | - { |
|
391 | - return $this->typeRegistry; |
|
392 | - } |
|
19 | + private $swagger = '2.0'; |
|
20 | + private $host; |
|
21 | + private $basePath; |
|
22 | + |
|
23 | + /** |
|
24 | + * @var TypeRegistry |
|
25 | + */ |
|
26 | + private $typeRegistry; |
|
27 | + |
|
28 | + /** |
|
29 | + * @var Info $Info |
|
30 | + */ |
|
31 | + private $info; |
|
32 | + private $schemes = []; |
|
33 | + private $consumes = []; |
|
34 | + private $produces = []; |
|
35 | + |
|
36 | + /** |
|
37 | + * @var Path[] $Paths |
|
38 | + */ |
|
39 | + private $paths = []; |
|
40 | + |
|
41 | + /** |
|
42 | + * @var Schema[] $definitions |
|
43 | + */ |
|
44 | + private $definitions = []; |
|
45 | + |
|
46 | + /** |
|
47 | + * @var IParameter[] $parameters |
|
48 | + */ |
|
49 | + private $parameters = []; |
|
50 | + |
|
51 | + /** |
|
52 | + * @var Response[] $responses |
|
53 | + */ |
|
54 | + private $responses = []; |
|
55 | + |
|
56 | + /** |
|
57 | + * @var Tag[] $Tags |
|
58 | + */ |
|
59 | + private $tags = []; |
|
60 | + |
|
61 | + /** |
|
62 | + * Default tag for new endpoints/operations. Set by the api command. |
|
63 | + * |
|
64 | + * @var Tag |
|
65 | + */ |
|
66 | + private $defaultTag; |
|
67 | + private $securityDefinitions = []; |
|
68 | + private $security = []; |
|
69 | + |
|
70 | + /** |
|
71 | + * @param string $host |
|
72 | + * @param string $basePath |
|
73 | + * @param TypeRegistry $typeRegistry |
|
74 | + */ |
|
75 | + public function __construct($host = null, $basePath = null, $typeRegistry = null) |
|
76 | + { |
|
77 | + parent::__construct(); |
|
78 | + |
|
79 | + $this->host = $host; |
|
80 | + $this->basePath = $basePath; |
|
81 | + |
|
82 | + $this->info = new Info($this); |
|
83 | + |
|
84 | + $this->typeRegistry = $typeRegistry ?: new TypeRegistry; |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * Return all consumes |
|
89 | + * |
|
90 | + * @return array |
|
91 | + * @todo Deprecate in favour of a getConsume($name); |
|
92 | + */ |
|
93 | + public function getConsumes(): array |
|
94 | + { |
|
95 | + return $this->consumes; |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * Return the named security if it exists, otherwise return FALSE |
|
100 | + * |
|
101 | + * @param string $name |
|
102 | + * |
|
103 | + * @return boolean|SecurityScheme |
|
104 | + */ |
|
105 | + public function getSecurity($name) |
|
106 | + { |
|
107 | + return $this->securityDefinitions[$name] ?? false; |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * @param string $command |
|
112 | + * @param string $data |
|
113 | + * |
|
114 | + * @return AbstractObject|boolean |
|
115 | + * @throws Exception |
|
116 | + * @throws Exception |
|
117 | + * @throws Exception |
|
118 | + * @throws Exception |
|
119 | + * @throws Exception |
|
120 | + * @throws Exception |
|
121 | + * @throws Exception |
|
122 | + * @throws Exception |
|
123 | + * @throws Exception |
|
124 | + */ |
|
125 | + public function handleCommand($command, $data = null) |
|
126 | + { |
|
127 | + switch (strtolower($command)) { |
|
128 | + // pass to Info |
|
129 | + case 'title': |
|
130 | + case 'description': |
|
131 | + case 'version': |
|
132 | + case 'terms': // alias |
|
133 | + case 'tos': // alias |
|
134 | + case 'termsofservice': |
|
135 | + case 'contact': |
|
136 | + case 'license': |
|
137 | + return $this->info->handleCommand($command, $data); |
|
138 | + |
|
139 | + // string[] |
|
140 | + case 'scheme': |
|
141 | + case 'schemes': |
|
142 | + $this->schemes = array_unique(array_merge($this->schemes, self::wordSplit($data))); |
|
143 | + |
|
144 | + return $this; |
|
145 | + |
|
146 | + // MIME[] |
|
147 | + case 'consume': |
|
148 | + case 'consumes': |
|
149 | + $this->consumes = array_merge($this->consumes, self::translateMimeTypes(self::wordSplit($data))); |
|
150 | + |
|
151 | + return $this; |
|
152 | + |
|
153 | + case 'produce': |
|
154 | + case 'produces': |
|
155 | + $this->produces = array_merge($this->produces, self::translateMimeTypes(self::wordSplit($data))); |
|
156 | + |
|
157 | + return $this; |
|
158 | + |
|
159 | + case 'model': |
|
160 | + case 'model!': |
|
161 | + case 'definition': |
|
162 | + case 'definition!': |
|
163 | + $name = self::wordShift($data); |
|
164 | + if (empty($name)) { |
|
165 | + throw new Exception('Missing definition name'); |
|
166 | + } |
|
167 | + $typeDef = self::wordShift($data); |
|
168 | + if (empty($typeDef)) { |
|
169 | + $typeDef = 'object'; |
|
170 | + } |
|
171 | + |
|
172 | + $definition = new Schema($this, $typeDef); |
|
173 | + if (str_ends_with($command, '!')) { |
|
174 | + $definition->setReadOnly(); |
|
175 | + } |
|
176 | + $this->definitions[$name] = $definition; |
|
177 | + |
|
178 | + return $definition; |
|
179 | + |
|
180 | + case 'path': |
|
181 | + case 'query': |
|
182 | + case 'query?': |
|
183 | + case 'header': |
|
184 | + case 'header?': |
|
185 | + case 'form': |
|
186 | + case 'form?': |
|
187 | + $in = rtrim($command, '?'); |
|
188 | + $Parameter = new Parameter($this, $in, $data, !str_ends_with($command, '?')); |
|
189 | + $this->parameters[$Parameter->getName()] = $Parameter; |
|
190 | + |
|
191 | + return $Parameter; |
|
192 | + |
|
193 | + case 'body': |
|
194 | + case 'body?': |
|
195 | + $Parameter = new BodyParameter($this, $data, !str_ends_with($command, '?')); |
|
196 | + $this->parameters[$Parameter->getName()] = $Parameter; |
|
197 | + |
|
198 | + return $Parameter; |
|
199 | + |
|
200 | + case 'response': |
|
201 | + $name = self::wordShift($data); |
|
202 | + $definition = self::wordShift($data); |
|
203 | + $description = $data; |
|
204 | + if (empty($description)) { |
|
205 | + throw new Exception('Response definition missing description'); |
|
206 | + } |
|
207 | + $Response = new Response($this, $name, $definition === 'null' ? null : $definition, $description); |
|
208 | + $this->responses[$name] = $Response; |
|
209 | + |
|
210 | + return $Response; |
|
211 | + |
|
212 | + case 'api': // alias |
|
213 | + case 'tag': |
|
214 | + $tagname = self::wordShift($data); |
|
215 | + if (empty($tagname)) { |
|
216 | + throw new Exception('Missing tag name'); |
|
217 | + } |
|
218 | + |
|
219 | + $Tag = null; |
|
220 | + foreach ($this->tags as $T) { |
|
221 | + if ($T->getName() === $tagname) { |
|
222 | + $Tag = $T; |
|
223 | + break; |
|
224 | + } |
|
225 | + } |
|
226 | + if (!$Tag) { |
|
227 | + $Tag = new Tag($this, $tagname, $data); |
|
228 | + $this->tags[] = $Tag; |
|
229 | + } |
|
230 | + |
|
231 | + // backwards compatibility |
|
232 | + if ($command === 'api') { |
|
233 | + $this->defaultTag = $Tag; |
|
234 | + } |
|
235 | + |
|
236 | + return $Tag; |
|
237 | + |
|
238 | + case 'endpoint': |
|
239 | + $path = self::wordShift($data); |
|
240 | + if (false === $path) { |
|
241 | + $path = '/'; |
|
242 | + } else if ($path[0] !== '/') { |
|
243 | + $path = '/' . $path; |
|
244 | + } |
|
245 | + |
|
246 | + $Tag = null; |
|
247 | + if (($tagname = self::wordShift($data)) !== false) { |
|
248 | + foreach ($this->tags as $T) { |
|
249 | + if (strtolower($T->getName()) === strtolower($tagname)) { |
|
250 | + $Tag = $T; |
|
251 | + break; |
|
252 | + } |
|
253 | + } |
|
254 | + if (!$Tag) { |
|
255 | + $Tag = new Tag($this, $tagname, $data); |
|
256 | + $this->tags[] = $Tag; |
|
257 | + } |
|
258 | + } |
|
259 | + |
|
260 | + if (!isset($this->paths[$path])) { |
|
261 | + $this->paths[$path] = new Path($this, $Tag ?: $this->defaultTag); |
|
262 | + } |
|
263 | + |
|
264 | + return $this->paths[$path]; |
|
265 | + |
|
266 | + case 'security': |
|
267 | + $name = self::wordShift($data); |
|
268 | + if (empty($name)) { |
|
269 | + throw new Exception('Missing security name'); |
|
270 | + } |
|
271 | + $type = self::wordShift($data); |
|
272 | + if (empty($type)) { |
|
273 | + throw new Exception('Missing security type'); |
|
274 | + } |
|
275 | + $SecurityScheme = new SecurityScheme($this, $type, $data); |
|
276 | + $this->securityDefinitions[$name] = $SecurityScheme; |
|
277 | + |
|
278 | + return $SecurityScheme; |
|
279 | + |
|
280 | + case 'require': |
|
281 | + $name = self::wordShift($data); |
|
282 | + if (empty($name)) { |
|
283 | + throw new Exception('Missing require name'); |
|
284 | + } |
|
285 | + $scopes = self::wordSplit($data); |
|
286 | + sort($scopes); |
|
287 | + $this->security[] = [ |
|
288 | + $name => empty($scopes) ? [] : $scopes, |
|
289 | + ]; |
|
290 | + |
|
291 | + return $this; |
|
292 | + } |
|
293 | + |
|
294 | + return parent::handleCommand($command, $data); |
|
295 | + } |
|
296 | + |
|
297 | + /** |
|
298 | + * @inheritDoc |
|
299 | + * @throws Exception |
|
300 | + * @throws Exception |
|
301 | + */ |
|
302 | + public function toArray(): array |
|
303 | + { |
|
304 | + if (empty($this->paths)) { |
|
305 | + throw new Exception('No path defined'); |
|
306 | + } |
|
307 | + |
|
308 | + $schemes = array_unique($this->schemes); |
|
309 | + sort($schemes); |
|
310 | + |
|
311 | + $consumes = array_unique($this->consumes); |
|
312 | + sort($consumes); |
|
313 | + |
|
314 | + $produces = array_unique($this->produces); |
|
315 | + sort($produces); |
|
316 | + |
|
317 | + foreach ($this->security as $security) { |
|
318 | + foreach ($security as $name => $scopes) { |
|
319 | + if (!isset($this->securityDefinitions[$name])) { |
|
320 | + throw new Exception('Required security scheme not defined: \'' . $name . '\''); |
|
321 | + } |
|
322 | + } |
|
323 | + } |
|
324 | + |
|
325 | + return self::arrayFilterNull(array_merge([ |
|
326 | + 'swagger' => $this->swagger, |
|
327 | + 'info' => $this->info->toArray(), |
|
328 | + 'host' => empty($this->host) ? null : $this->host, |
|
329 | + 'basePath' => empty($this->basePath) ? null : $this->basePath, |
|
330 | + 'consumes' => $consumes, |
|
331 | + 'produces' => $produces, |
|
332 | + 'schemes' => $schemes, |
|
333 | + 'paths' => self::objectsToArray($this->paths), |
|
334 | + 'definitions' => self::objectsToArray($this->definitions), |
|
335 | + 'parameters' => self::objectsToArray($this->parameters), |
|
336 | + 'responses' => self::objectsToArray($this->responses), |
|
337 | + 'securityDefinitions' => self::objectsToArray($this->securityDefinitions), |
|
338 | + 'security' => $this->security, |
|
339 | + 'tags' => self::objectsToArray($this->tags), |
|
340 | + ], parent::toArray())); |
|
341 | + } |
|
342 | + |
|
343 | + public function __toString() |
|
344 | + { |
|
345 | + return __CLASS__; |
|
346 | + } |
|
347 | + |
|
348 | + /** |
|
349 | + * Check if an operation with the given id exists. |
|
350 | + * |
|
351 | + * @param string $operationId |
|
352 | + * |
|
353 | + * @return boolean |
|
354 | + */ |
|
355 | + public function hasOperationId($operationId): bool |
|
356 | + { |
|
357 | + foreach ($this->paths as $path) { |
|
358 | + if ($path->hasOperationId($operationId)) { |
|
359 | + return true; |
|
360 | + } |
|
361 | + } |
|
362 | + |
|
363 | + return false; |
|
364 | + } |
|
365 | + |
|
366 | + /** |
|
367 | + * Check if a definition with the given name exists |
|
368 | + * |
|
369 | + * @param string $name |
|
370 | + * |
|
371 | + * @return boolean |
|
372 | + */ |
|
373 | + public function hasDefinition($name): bool |
|
374 | + { |
|
375 | + return isset($this->definitions[$name]); |
|
376 | + } |
|
377 | + |
|
378 | + /** |
|
379 | + * @inheritDoc |
|
380 | + */ |
|
381 | + protected function getSwagger(): Swagger |
|
382 | + { |
|
383 | + return $this; |
|
384 | + } |
|
385 | + |
|
386 | + /** |
|
387 | + * @inheritDoc |
|
388 | + */ |
|
389 | + protected function getTypeRegistry(): TypeRegistry |
|
390 | + { |
|
391 | + return $this->typeRegistry; |
|
392 | + } |
|
393 | 393 | |
394 | 394 | } |
@@ -16,177 +16,177 @@ |
||
16 | 16 | class Response extends AbstractObject |
17 | 17 | { |
18 | 18 | |
19 | - const OK = 200; |
|
20 | - const CREATED = 201; |
|
21 | - const ACCEPTED = 202; |
|
22 | - const NON_AUTHORITATIVE_INFORMATION = 203; |
|
23 | - const NO_CONTENT = 204; |
|
24 | - const RESET_CONTENT = 205; |
|
25 | - const PARTIAL_CONTENT = 206; |
|
26 | - const BAD_REQUEST = 400; |
|
27 | - const UNAUTHORIZED = 401; |
|
28 | - const PAYMENT_REQUIRED = 402; |
|
29 | - const FORBIDDEN = 403; |
|
30 | - const NOT_FOUND = 404; |
|
31 | - const METHOD_NOT_ALLOWED = 405; |
|
32 | - const NOT_ACCEPTABLE = 406; |
|
33 | - const PROXY_AUTHENTICATION_REQUIRED = 407; |
|
34 | - const REQUEST_TIMEOUT = 408; |
|
35 | - const CONFLICT = 409; |
|
36 | - const GONE = 410; |
|
37 | - const LENGTH_REQUIRED = 411; |
|
38 | - const PRECONDITION_FAILED = 412; |
|
39 | - const REQUEST_ENTITY_TOO_LARGE = 413; |
|
40 | - const REQUEST_URI_TOO_LONG = 414; |
|
41 | - const UNSUPPORTED_MEDIA_TYPE = 415; |
|
42 | - const REQUESTED_RANGE_NOT_SATISFIABLE = 416; |
|
43 | - const EXPECTATION_FAILED = 417; |
|
44 | - const UNPROCESSABLE_ENTITY = 422; |
|
45 | - const TOO_MANY_REQUESTS = 429; |
|
46 | - const INTERNAL_SERVER_ERROR = 500; |
|
47 | - const NOT_IMPLEMENTED = 501; // When method is supported for none of the resources |
|
48 | - |
|
49 | - protected static $httpCodes = array( |
|
50 | - self::OK => 'OK', |
|
51 | - self::CREATED => 'Created', |
|
52 | - self::ACCEPTED => 'Accepted', |
|
53 | - self::NON_AUTHORITATIVE_INFORMATION => 'Non-Authoritative Information', |
|
54 | - self::NO_CONTENT => 'No Content', |
|
55 | - self::RESET_CONTENT => 'Reset Content', |
|
56 | - self::PARTIAL_CONTENT => 'Partial Content', |
|
57 | - self::BAD_REQUEST => 'Bad Request', |
|
58 | - self::UNAUTHORIZED => 'Unauthorized', |
|
59 | - self::PAYMENT_REQUIRED => 'Payment Required', |
|
60 | - self::FORBIDDEN => 'Forbidden', |
|
61 | - self::NOT_FOUND => 'Not Found', |
|
62 | - self::METHOD_NOT_ALLOWED => 'Method Not Allowed', |
|
63 | - self::NOT_ACCEPTABLE => 'Not Acceptable', |
|
64 | - self::PROXY_AUTHENTICATION_REQUIRED => 'Proxy Authentication Required', |
|
65 | - self::REQUEST_TIMEOUT => 'Request Timeout', |
|
66 | - self::CONFLICT => 'Conflict', |
|
67 | - self::GONE => 'Gone', |
|
68 | - self::LENGTH_REQUIRED => 'Length Required', |
|
69 | - self::PRECONDITION_FAILED => 'Precondition Failed', |
|
70 | - self::REQUEST_ENTITY_TOO_LARGE => 'Request Entity Too Large', |
|
71 | - self::REQUEST_URI_TOO_LONG => 'Request-URI Too Long', |
|
72 | - self::UNSUPPORTED_MEDIA_TYPE => 'Unsupported Media Type', |
|
73 | - self::REQUESTED_RANGE_NOT_SATISFIABLE => 'Requested Range Not Satisfiable', |
|
74 | - self::EXPECTATION_FAILED => 'Expectation Failed', |
|
75 | - self::UNPROCESSABLE_ENTITY => 'Unprocessable Entity', |
|
76 | - self::TOO_MANY_REQUESTS => 'Too Many Requests', |
|
77 | - self::INTERNAL_SERVER_ERROR => 'Internal Server Error', |
|
78 | - self::NOT_IMPLEMENTED => 'Not Implemented', |
|
79 | - ); |
|
80 | - private $description = ''; |
|
81 | - private $schema; |
|
82 | - |
|
83 | - /** |
|
84 | - * @var Header[] |
|
85 | - */ |
|
86 | - private $Headers = []; |
|
87 | - |
|
88 | - /** |
|
89 | - * JSON examples |
|
90 | - * @var array |
|
91 | - */ |
|
92 | - private $examples = []; |
|
93 | - |
|
94 | - /** |
|
95 | - * @throws Exception |
|
96 | - */ |
|
97 | - public function __construct(AbstractObject $parent, $code, $definition = null, $description = null) |
|
98 | - { |
|
99 | - parent::__construct($parent); |
|
100 | - |
|
101 | - if ($definition) { |
|
102 | - $this->schema = new Schema($this, $definition); |
|
103 | - } |
|
104 | - |
|
105 | - if (!empty($description)) { |
|
106 | - $this->description = $description; |
|
107 | - } elseif (isset(self::$httpCodes[$code])) { |
|
108 | - $this->description = self::$httpCodes[$code]; |
|
109 | - } |
|
110 | - } |
|
111 | - |
|
112 | - public static function getCode($search) |
|
113 | - { |
|
114 | - static $lookup = null; |
|
115 | - |
|
116 | - if (is_numeric($search)) { |
|
117 | - return (int)$search; |
|
118 | - } |
|
119 | - |
|
120 | - // build static lookup table |
|
121 | - if (!$lookup) { |
|
122 | - $lookup = []; |
|
123 | - foreach (self::$httpCodes as $code => $text) { |
|
124 | - $lookup[preg_replace('/[^a-z]+/', '', strtolower($text))] = $code; |
|
125 | - } |
|
126 | - } |
|
127 | - |
|
128 | - $search = preg_replace('/[^a-z]+/', '', strtolower($search)); |
|
129 | - return $lookup[$search] ?? null; |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * @param string $command |
|
134 | - * @param string $data |
|
135 | - * @return AbstractObject|boolean |
|
136 | - * @throws Exception |
|
137 | - * @throws Exception |
|
138 | - * @throws Exception |
|
139 | - * @throws Exception |
|
140 | - * @throws Exception |
|
141 | - */ |
|
142 | - public function handleCommand($command, $data = null) |
|
143 | - { |
|
144 | - switch (strtolower($command)) { |
|
145 | - case 'header': |
|
146 | - $type = self::wordShift($data); |
|
147 | - if (empty($type)) { |
|
148 | - throw new Exception('Missing type for header'); |
|
149 | - } |
|
150 | - $name = self::wordShift($data); |
|
151 | - if (empty($name)) { |
|
152 | - throw new Exception('Missing name for header type \'' . $type . '\''); |
|
153 | - } |
|
154 | - $Header = new Header($this, $type, $data); |
|
155 | - $this->Headers[$name] = $Header; |
|
156 | - return $Header; |
|
157 | - |
|
158 | - case 'example': |
|
159 | - $name = self::wordShift($data); |
|
160 | - if (empty($name)) { |
|
161 | - throw new Exception('Missing name for example'); |
|
162 | - } |
|
163 | - if ($data === '') { |
|
164 | - throw new Exception('Missing content for example `' . $name . '`'); |
|
165 | - } |
|
166 | - $json = preg_replace_callback('/([^{}:]+)/', static function ($match) { |
|
167 | - json_decode($match[1]); |
|
168 | - return json_last_error() === JSON_ERROR_NONE ? $match[1] : json_encode($match[1]); |
|
169 | - }, trim($data)); |
|
170 | - $this->examples[$name] = json_decode($json, true); |
|
171 | - return $this; |
|
172 | - } |
|
173 | - |
|
174 | - return parent::handleCommand($command, $data); |
|
175 | - } |
|
176 | - |
|
177 | - public function toArray(): array |
|
178 | - { |
|
179 | - return self::arrayFilterNull(array_merge(array( |
|
180 | - 'description' => $this->description, |
|
181 | - 'schema' => $this->schema?->toArray(), |
|
182 | - 'headers' => self::objectsToArray($this->Headers), |
|
183 | - 'examples' => $this->examples, |
|
184 | - ), parent::toArray())); |
|
185 | - } |
|
186 | - |
|
187 | - public function __toString() |
|
188 | - { |
|
189 | - return __CLASS__; |
|
190 | - } |
|
19 | + const OK = 200; |
|
20 | + const CREATED = 201; |
|
21 | + const ACCEPTED = 202; |
|
22 | + const NON_AUTHORITATIVE_INFORMATION = 203; |
|
23 | + const NO_CONTENT = 204; |
|
24 | + const RESET_CONTENT = 205; |
|
25 | + const PARTIAL_CONTENT = 206; |
|
26 | + const BAD_REQUEST = 400; |
|
27 | + const UNAUTHORIZED = 401; |
|
28 | + const PAYMENT_REQUIRED = 402; |
|
29 | + const FORBIDDEN = 403; |
|
30 | + const NOT_FOUND = 404; |
|
31 | + const METHOD_NOT_ALLOWED = 405; |
|
32 | + const NOT_ACCEPTABLE = 406; |
|
33 | + const PROXY_AUTHENTICATION_REQUIRED = 407; |
|
34 | + const REQUEST_TIMEOUT = 408; |
|
35 | + const CONFLICT = 409; |
|
36 | + const GONE = 410; |
|
37 | + const LENGTH_REQUIRED = 411; |
|
38 | + const PRECONDITION_FAILED = 412; |
|
39 | + const REQUEST_ENTITY_TOO_LARGE = 413; |
|
40 | + const REQUEST_URI_TOO_LONG = 414; |
|
41 | + const UNSUPPORTED_MEDIA_TYPE = 415; |
|
42 | + const REQUESTED_RANGE_NOT_SATISFIABLE = 416; |
|
43 | + const EXPECTATION_FAILED = 417; |
|
44 | + const UNPROCESSABLE_ENTITY = 422; |
|
45 | + const TOO_MANY_REQUESTS = 429; |
|
46 | + const INTERNAL_SERVER_ERROR = 500; |
|
47 | + const NOT_IMPLEMENTED = 501; // When method is supported for none of the resources |
|
48 | + |
|
49 | + protected static $httpCodes = array( |
|
50 | + self::OK => 'OK', |
|
51 | + self::CREATED => 'Created', |
|
52 | + self::ACCEPTED => 'Accepted', |
|
53 | + self::NON_AUTHORITATIVE_INFORMATION => 'Non-Authoritative Information', |
|
54 | + self::NO_CONTENT => 'No Content', |
|
55 | + self::RESET_CONTENT => 'Reset Content', |
|
56 | + self::PARTIAL_CONTENT => 'Partial Content', |
|
57 | + self::BAD_REQUEST => 'Bad Request', |
|
58 | + self::UNAUTHORIZED => 'Unauthorized', |
|
59 | + self::PAYMENT_REQUIRED => 'Payment Required', |
|
60 | + self::FORBIDDEN => 'Forbidden', |
|
61 | + self::NOT_FOUND => 'Not Found', |
|
62 | + self::METHOD_NOT_ALLOWED => 'Method Not Allowed', |
|
63 | + self::NOT_ACCEPTABLE => 'Not Acceptable', |
|
64 | + self::PROXY_AUTHENTICATION_REQUIRED => 'Proxy Authentication Required', |
|
65 | + self::REQUEST_TIMEOUT => 'Request Timeout', |
|
66 | + self::CONFLICT => 'Conflict', |
|
67 | + self::GONE => 'Gone', |
|
68 | + self::LENGTH_REQUIRED => 'Length Required', |
|
69 | + self::PRECONDITION_FAILED => 'Precondition Failed', |
|
70 | + self::REQUEST_ENTITY_TOO_LARGE => 'Request Entity Too Large', |
|
71 | + self::REQUEST_URI_TOO_LONG => 'Request-URI Too Long', |
|
72 | + self::UNSUPPORTED_MEDIA_TYPE => 'Unsupported Media Type', |
|
73 | + self::REQUESTED_RANGE_NOT_SATISFIABLE => 'Requested Range Not Satisfiable', |
|
74 | + self::EXPECTATION_FAILED => 'Expectation Failed', |
|
75 | + self::UNPROCESSABLE_ENTITY => 'Unprocessable Entity', |
|
76 | + self::TOO_MANY_REQUESTS => 'Too Many Requests', |
|
77 | + self::INTERNAL_SERVER_ERROR => 'Internal Server Error', |
|
78 | + self::NOT_IMPLEMENTED => 'Not Implemented', |
|
79 | + ); |
|
80 | + private $description = ''; |
|
81 | + private $schema; |
|
82 | + |
|
83 | + /** |
|
84 | + * @var Header[] |
|
85 | + */ |
|
86 | + private $Headers = []; |
|
87 | + |
|
88 | + /** |
|
89 | + * JSON examples |
|
90 | + * @var array |
|
91 | + */ |
|
92 | + private $examples = []; |
|
93 | + |
|
94 | + /** |
|
95 | + * @throws Exception |
|
96 | + */ |
|
97 | + public function __construct(AbstractObject $parent, $code, $definition = null, $description = null) |
|
98 | + { |
|
99 | + parent::__construct($parent); |
|
100 | + |
|
101 | + if ($definition) { |
|
102 | + $this->schema = new Schema($this, $definition); |
|
103 | + } |
|
104 | + |
|
105 | + if (!empty($description)) { |
|
106 | + $this->description = $description; |
|
107 | + } elseif (isset(self::$httpCodes[$code])) { |
|
108 | + $this->description = self::$httpCodes[$code]; |
|
109 | + } |
|
110 | + } |
|
111 | + |
|
112 | + public static function getCode($search) |
|
113 | + { |
|
114 | + static $lookup = null; |
|
115 | + |
|
116 | + if (is_numeric($search)) { |
|
117 | + return (int)$search; |
|
118 | + } |
|
119 | + |
|
120 | + // build static lookup table |
|
121 | + if (!$lookup) { |
|
122 | + $lookup = []; |
|
123 | + foreach (self::$httpCodes as $code => $text) { |
|
124 | + $lookup[preg_replace('/[^a-z]+/', '', strtolower($text))] = $code; |
|
125 | + } |
|
126 | + } |
|
127 | + |
|
128 | + $search = preg_replace('/[^a-z]+/', '', strtolower($search)); |
|
129 | + return $lookup[$search] ?? null; |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * @param string $command |
|
134 | + * @param string $data |
|
135 | + * @return AbstractObject|boolean |
|
136 | + * @throws Exception |
|
137 | + * @throws Exception |
|
138 | + * @throws Exception |
|
139 | + * @throws Exception |
|
140 | + * @throws Exception |
|
141 | + */ |
|
142 | + public function handleCommand($command, $data = null) |
|
143 | + { |
|
144 | + switch (strtolower($command)) { |
|
145 | + case 'header': |
|
146 | + $type = self::wordShift($data); |
|
147 | + if (empty($type)) { |
|
148 | + throw new Exception('Missing type for header'); |
|
149 | + } |
|
150 | + $name = self::wordShift($data); |
|
151 | + if (empty($name)) { |
|
152 | + throw new Exception('Missing name for header type \'' . $type . '\''); |
|
153 | + } |
|
154 | + $Header = new Header($this, $type, $data); |
|
155 | + $this->Headers[$name] = $Header; |
|
156 | + return $Header; |
|
157 | + |
|
158 | + case 'example': |
|
159 | + $name = self::wordShift($data); |
|
160 | + if (empty($name)) { |
|
161 | + throw new Exception('Missing name for example'); |
|
162 | + } |
|
163 | + if ($data === '') { |
|
164 | + throw new Exception('Missing content for example `' . $name . '`'); |
|
165 | + } |
|
166 | + $json = preg_replace_callback('/([^{}:]+)/', static function ($match) { |
|
167 | + json_decode($match[1]); |
|
168 | + return json_last_error() === JSON_ERROR_NONE ? $match[1] : json_encode($match[1]); |
|
169 | + }, trim($data)); |
|
170 | + $this->examples[$name] = json_decode($json, true); |
|
171 | + return $this; |
|
172 | + } |
|
173 | + |
|
174 | + return parent::handleCommand($command, $data); |
|
175 | + } |
|
176 | + |
|
177 | + public function toArray(): array |
|
178 | + { |
|
179 | + return self::arrayFilterNull(array_merge(array( |
|
180 | + 'description' => $this->description, |
|
181 | + 'schema' => $this->schema?->toArray(), |
|
182 | + 'headers' => self::objectsToArray($this->Headers), |
|
183 | + 'examples' => $this->examples, |
|
184 | + ), parent::toArray())); |
|
185 | + } |
|
186 | + |
|
187 | + public function __toString() |
|
188 | + { |
|
189 | + return __CLASS__; |
|
190 | + } |
|
191 | 191 | |
192 | 192 | } |
@@ -15,199 +15,199 @@ |
||
15 | 15 | class StringType extends AbstractType |
16 | 16 | { |
17 | 17 | |
18 | - private static $formats = array( |
|
19 | - 'string' => '', |
|
20 | - 'byte' => 'byte', |
|
21 | - 'binary' => 'binary', |
|
22 | - 'password' => 'password', |
|
23 | - 'enum' => '', |
|
24 | - ); |
|
25 | - |
|
26 | - /** |
|
27 | - * Name of the type |
|
28 | - * @var string |
|
29 | - */ |
|
30 | - protected $format = ''; |
|
31 | - protected $pattern; |
|
32 | - protected $default; |
|
33 | - protected $maxLength; |
|
34 | - protected $minLength; |
|
35 | - protected $enum = []; |
|
36 | - |
|
37 | - /** |
|
38 | - * @param string $command The comment command |
|
39 | - * @param string $data Any data added after the command |
|
40 | - * @return AbstractType|boolean |
|
41 | - * @throws Exception |
|
42 | - * @throws Exception |
|
43 | - * @throws Exception |
|
44 | - */ |
|
45 | - public function handleCommand($command, $data = null) |
|
46 | - { |
|
47 | - switch (strtolower($command)) { |
|
48 | - case 'default': |
|
49 | - $this->default = $this->validateDefault($data); |
|
50 | - return $this; |
|
51 | - |
|
52 | - case 'pattern': |
|
53 | - $this->pattern = $data; |
|
54 | - return $this; |
|
55 | - |
|
56 | - case 'enum': |
|
57 | - if ($this->minLength !== null || $this->maxLength !== null) { |
|
58 | - throw new Exception("Enumeration not allowed in ranged string: '{$data}'"); |
|
59 | - } |
|
60 | - $words = self::wordSplit($data); |
|
61 | - $this->enum = is_array($this->enum) ? array_merge($this->enum, $words) : $words; |
|
62 | - return $this; |
|
63 | - } |
|
64 | - |
|
65 | - return parent::handleCommand($command, $data); |
|
66 | - } |
|
67 | - |
|
68 | - /** |
|
69 | - * Validate a default string value, depending on subtype |
|
70 | - * |
|
71 | - * @param string $value the value to validate |
|
72 | - * @return string the value after validation (might become trimmed) |
|
73 | - * @throws Exception |
|
74 | - */ |
|
75 | - protected function validateDefault($value): string |
|
76 | - { |
|
77 | - if (empty($value)) { |
|
78 | - if ($this->format) { |
|
79 | - $type = $this->format; |
|
80 | - } else { |
|
81 | - $type = $this->enum ? 'enum' : 'string'; |
|
82 | - } |
|
83 | - throw new Exception("Empty {$type} default"); |
|
84 | - } |
|
85 | - |
|
86 | - if (!empty($this->enum) && !in_array($value, $this->enum, true)) { |
|
87 | - throw new Exception("Invalid enum default: '{$value}'"); |
|
88 | - } |
|
89 | - |
|
90 | - if ($this->maxLength !== null && mb_strlen($value) > $this->maxLength) { |
|
91 | - if ($this->format) { |
|
92 | - $type = $this->format; |
|
93 | - } else { |
|
94 | - $type = $this->enum ? 'enum' : 'string'; |
|
95 | - } |
|
96 | - throw new Exception("Default {$type} length beyond maximum: '{$value}'"); |
|
97 | - } |
|
98 | - |
|
99 | - if ($this->minLength !== null && mb_strlen($value) < $this->minLength) { |
|
100 | - if ($this->format) { |
|
101 | - $type = $this->format; |
|
102 | - } else { |
|
103 | - $type = $this->enum ? 'enum' : 'string'; |
|
104 | - } |
|
105 | - throw new Exception("Default {$type} length beyond minimum: '{$value}'"); |
|
106 | - } |
|
107 | - |
|
108 | - return $value; |
|
109 | - } |
|
110 | - |
|
111 | - public function toArray(): array |
|
112 | - { |
|
113 | - return self::arrayFilterNull(array_merge(array( |
|
114 | - 'type' => 'string', |
|
115 | - 'format' => empty($this->format) ? null : $this->format, |
|
116 | - 'pattern' => $this->pattern, |
|
117 | - 'default' => $this->default, |
|
118 | - 'minLength' => $this->minLength ? (int)$this->minLength : null, |
|
119 | - 'maxLength' => $this->maxLength ? (int)$this->maxLength : null, |
|
120 | - 'enum' => $this->enum, |
|
121 | - ), parent::toArray())); |
|
122 | - } |
|
123 | - |
|
124 | - public function __toString() |
|
125 | - { |
|
126 | - return __CLASS__; |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * @throws Exception |
|
131 | - */ |
|
132 | - protected function parseDefinition($definition): void |
|
133 | - { |
|
134 | - $definition = self::trim($definition); |
|
135 | - |
|
136 | - $match = []; |
|
137 | - if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_CONTENT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
138 | - throw new Exception("Unparseable string definition: '{$definition}'"); |
|
139 | - } |
|
140 | - |
|
141 | - $this->parseFormat($definition, $match); |
|
142 | - $this->parseContent($definition, $match); |
|
143 | - $this->parseRange($definition, $match); |
|
144 | - $this->parseDefault($definition, $match); |
|
145 | - } |
|
146 | - |
|
147 | - /** |
|
148 | - * @param string $definition |
|
149 | - * @param string[] $match |
|
150 | - * @throws Exception |
|
151 | - */ |
|
152 | - private function parseFormat($definition, $match): void |
|
153 | - { |
|
154 | - $type = strtolower($match[1]); |
|
155 | - if (!isset(self::$formats[$type])) { |
|
156 | - throw new Exception("Not a string: '{$definition}'"); |
|
157 | - } |
|
158 | - $this->format = self::$formats[$type]; |
|
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * @param string $definition |
|
163 | - * @param string[] $match |
|
164 | - */ |
|
165 | - private function parseContent($definition, $match): void |
|
166 | - { |
|
167 | - if (strtolower($match[1]) === 'enum') { |
|
168 | - $this->enum = explode(',', $match[2]); |
|
169 | - } else { |
|
170 | - $this->pattern = empty($match[2]) ? null : $match[2]; |
|
171 | - } |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * @param string $definition |
|
176 | - * @param string[] $match |
|
177 | - * @throws Exception |
|
178 | - * @throws Exception |
|
179 | - */ |
|
180 | - private function parseRange($definition, $match): void |
|
181 | - { |
|
182 | - |
|
183 | - if (!empty($match[3])) { |
|
184 | - if ($match[1] === 'enum') { |
|
185 | - throw new Exception("Range not allowed in enumeration definition: '{$definition}'"); |
|
186 | - } |
|
187 | - if ($match[4] === '' && $match[5] === '') { |
|
188 | - throw new Exception("Empty string range: '{$definition}'"); |
|
189 | - } |
|
190 | - $exclusiveMinimum = $match[3] === '<'; |
|
191 | - $this->minLength = $match[4] === '' ? null : $match[4]; |
|
192 | - $this->maxLength = $match[5] === '' ? null : $match[5]; |
|
193 | - $exclusiveMaximum = isset($match[6]) ? ($match[6] === '>') : null; |
|
194 | - if ($this->minLength !== null && $this->maxLength !== null && $this->minLength > $this->maxLength) { |
|
195 | - self::swap($this->minLength, $this->maxLength); |
|
196 | - self::swap($exclusiveMinimum, $exclusiveMaximum); |
|
197 | - } |
|
198 | - $this->minLength = $this->minLength === null ? null : max(0, $exclusiveMinimum ? $this->minLength + 1 : $this->minLength); |
|
199 | - $this->maxLength = $this->maxLength === null ? null : max(0, $exclusiveMaximum ? $this->maxLength - 1 : $this->maxLength); |
|
200 | - } |
|
201 | - } |
|
202 | - |
|
203 | - /** |
|
204 | - * @param string $definition |
|
205 | - * @param string[] $match |
|
206 | - * @throws Exception |
|
207 | - */ |
|
208 | - private function parseDefault($definition, $match): void |
|
209 | - { |
|
210 | - $this->default = isset($match[7]) && $match[7] !== '' ? $this->validateDefault($match[7]) : null; |
|
211 | - } |
|
18 | + private static $formats = array( |
|
19 | + 'string' => '', |
|
20 | + 'byte' => 'byte', |
|
21 | + 'binary' => 'binary', |
|
22 | + 'password' => 'password', |
|
23 | + 'enum' => '', |
|
24 | + ); |
|
25 | + |
|
26 | + /** |
|
27 | + * Name of the type |
|
28 | + * @var string |
|
29 | + */ |
|
30 | + protected $format = ''; |
|
31 | + protected $pattern; |
|
32 | + protected $default; |
|
33 | + protected $maxLength; |
|
34 | + protected $minLength; |
|
35 | + protected $enum = []; |
|
36 | + |
|
37 | + /** |
|
38 | + * @param string $command The comment command |
|
39 | + * @param string $data Any data added after the command |
|
40 | + * @return AbstractType|boolean |
|
41 | + * @throws Exception |
|
42 | + * @throws Exception |
|
43 | + * @throws Exception |
|
44 | + */ |
|
45 | + public function handleCommand($command, $data = null) |
|
46 | + { |
|
47 | + switch (strtolower($command)) { |
|
48 | + case 'default': |
|
49 | + $this->default = $this->validateDefault($data); |
|
50 | + return $this; |
|
51 | + |
|
52 | + case 'pattern': |
|
53 | + $this->pattern = $data; |
|
54 | + return $this; |
|
55 | + |
|
56 | + case 'enum': |
|
57 | + if ($this->minLength !== null || $this->maxLength !== null) { |
|
58 | + throw new Exception("Enumeration not allowed in ranged string: '{$data}'"); |
|
59 | + } |
|
60 | + $words = self::wordSplit($data); |
|
61 | + $this->enum = is_array($this->enum) ? array_merge($this->enum, $words) : $words; |
|
62 | + return $this; |
|
63 | + } |
|
64 | + |
|
65 | + return parent::handleCommand($command, $data); |
|
66 | + } |
|
67 | + |
|
68 | + /** |
|
69 | + * Validate a default string value, depending on subtype |
|
70 | + * |
|
71 | + * @param string $value the value to validate |
|
72 | + * @return string the value after validation (might become trimmed) |
|
73 | + * @throws Exception |
|
74 | + */ |
|
75 | + protected function validateDefault($value): string |
|
76 | + { |
|
77 | + if (empty($value)) { |
|
78 | + if ($this->format) { |
|
79 | + $type = $this->format; |
|
80 | + } else { |
|
81 | + $type = $this->enum ? 'enum' : 'string'; |
|
82 | + } |
|
83 | + throw new Exception("Empty {$type} default"); |
|
84 | + } |
|
85 | + |
|
86 | + if (!empty($this->enum) && !in_array($value, $this->enum, true)) { |
|
87 | + throw new Exception("Invalid enum default: '{$value}'"); |
|
88 | + } |
|
89 | + |
|
90 | + if ($this->maxLength !== null && mb_strlen($value) > $this->maxLength) { |
|
91 | + if ($this->format) { |
|
92 | + $type = $this->format; |
|
93 | + } else { |
|
94 | + $type = $this->enum ? 'enum' : 'string'; |
|
95 | + } |
|
96 | + throw new Exception("Default {$type} length beyond maximum: '{$value}'"); |
|
97 | + } |
|
98 | + |
|
99 | + if ($this->minLength !== null && mb_strlen($value) < $this->minLength) { |
|
100 | + if ($this->format) { |
|
101 | + $type = $this->format; |
|
102 | + } else { |
|
103 | + $type = $this->enum ? 'enum' : 'string'; |
|
104 | + } |
|
105 | + throw new Exception("Default {$type} length beyond minimum: '{$value}'"); |
|
106 | + } |
|
107 | + |
|
108 | + return $value; |
|
109 | + } |
|
110 | + |
|
111 | + public function toArray(): array |
|
112 | + { |
|
113 | + return self::arrayFilterNull(array_merge(array( |
|
114 | + 'type' => 'string', |
|
115 | + 'format' => empty($this->format) ? null : $this->format, |
|
116 | + 'pattern' => $this->pattern, |
|
117 | + 'default' => $this->default, |
|
118 | + 'minLength' => $this->minLength ? (int)$this->minLength : null, |
|
119 | + 'maxLength' => $this->maxLength ? (int)$this->maxLength : null, |
|
120 | + 'enum' => $this->enum, |
|
121 | + ), parent::toArray())); |
|
122 | + } |
|
123 | + |
|
124 | + public function __toString() |
|
125 | + { |
|
126 | + return __CLASS__; |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * @throws Exception |
|
131 | + */ |
|
132 | + protected function parseDefinition($definition): void |
|
133 | + { |
|
134 | + $definition = self::trim($definition); |
|
135 | + |
|
136 | + $match = []; |
|
137 | + if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_CONTENT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
138 | + throw new Exception("Unparseable string definition: '{$definition}'"); |
|
139 | + } |
|
140 | + |
|
141 | + $this->parseFormat($definition, $match); |
|
142 | + $this->parseContent($definition, $match); |
|
143 | + $this->parseRange($definition, $match); |
|
144 | + $this->parseDefault($definition, $match); |
|
145 | + } |
|
146 | + |
|
147 | + /** |
|
148 | + * @param string $definition |
|
149 | + * @param string[] $match |
|
150 | + * @throws Exception |
|
151 | + */ |
|
152 | + private function parseFormat($definition, $match): void |
|
153 | + { |
|
154 | + $type = strtolower($match[1]); |
|
155 | + if (!isset(self::$formats[$type])) { |
|
156 | + throw new Exception("Not a string: '{$definition}'"); |
|
157 | + } |
|
158 | + $this->format = self::$formats[$type]; |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * @param string $definition |
|
163 | + * @param string[] $match |
|
164 | + */ |
|
165 | + private function parseContent($definition, $match): void |
|
166 | + { |
|
167 | + if (strtolower($match[1]) === 'enum') { |
|
168 | + $this->enum = explode(',', $match[2]); |
|
169 | + } else { |
|
170 | + $this->pattern = empty($match[2]) ? null : $match[2]; |
|
171 | + } |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * @param string $definition |
|
176 | + * @param string[] $match |
|
177 | + * @throws Exception |
|
178 | + * @throws Exception |
|
179 | + */ |
|
180 | + private function parseRange($definition, $match): void |
|
181 | + { |
|
182 | + |
|
183 | + if (!empty($match[3])) { |
|
184 | + if ($match[1] === 'enum') { |
|
185 | + throw new Exception("Range not allowed in enumeration definition: '{$definition}'"); |
|
186 | + } |
|
187 | + if ($match[4] === '' && $match[5] === '') { |
|
188 | + throw new Exception("Empty string range: '{$definition}'"); |
|
189 | + } |
|
190 | + $exclusiveMinimum = $match[3] === '<'; |
|
191 | + $this->minLength = $match[4] === '' ? null : $match[4]; |
|
192 | + $this->maxLength = $match[5] === '' ? null : $match[5]; |
|
193 | + $exclusiveMaximum = isset($match[6]) ? ($match[6] === '>') : null; |
|
194 | + if ($this->minLength !== null && $this->maxLength !== null && $this->minLength > $this->maxLength) { |
|
195 | + self::swap($this->minLength, $this->maxLength); |
|
196 | + self::swap($exclusiveMinimum, $exclusiveMaximum); |
|
197 | + } |
|
198 | + $this->minLength = $this->minLength === null ? null : max(0, $exclusiveMinimum ? $this->minLength + 1 : $this->minLength); |
|
199 | + $this->maxLength = $this->maxLength === null ? null : max(0, $exclusiveMaximum ? $this->maxLength - 1 : $this->maxLength); |
|
200 | + } |
|
201 | + } |
|
202 | + |
|
203 | + /** |
|
204 | + * @param string $definition |
|
205 | + * @param string[] $match |
|
206 | + * @throws Exception |
|
207 | + */ |
|
208 | + private function parseDefault($definition, $match): void |
|
209 | + { |
|
210 | + $this->default = isset($match[7]) && $match[7] !== '' ? $this->validateDefault($match[7]) : null; |
|
211 | + } |
|
212 | 212 | |
213 | 213 | } |
@@ -80,7 +80,7 @@ discard block |
||
80 | 80 | } else { |
81 | 81 | $type = $this->enum ? 'enum' : 'string'; |
82 | 82 | } |
83 | - throw new Exception("Empty {$type} default"); |
|
83 | + throw new Exception("empty {$type} default"); |
|
84 | 84 | } |
85 | 85 | |
86 | 86 | if (!empty($this->enum) && !in_array($value, $this->enum, true)) { |
@@ -93,7 +93,7 @@ discard block |
||
93 | 93 | } else { |
94 | 94 | $type = $this->enum ? 'enum' : 'string'; |
95 | 95 | } |
96 | - throw new Exception("Default {$type} length beyond maximum: '{$value}'"); |
|
96 | + throw new Exception("default {$type} length beyond maximum: '{$value}'"); |
|
97 | 97 | } |
98 | 98 | |
99 | 99 | if ($this->minLength !== null && mb_strlen($value) < $this->minLength) { |
@@ -102,7 +102,7 @@ discard block |
||
102 | 102 | } else { |
103 | 103 | $type = $this->enum ? 'enum' : 'string'; |
104 | 104 | } |
105 | - throw new Exception("Default {$type} length beyond minimum: '{$value}'"); |
|
105 | + throw new Exception("default {$type} length beyond minimum: '{$value}'"); |
|
106 | 106 | } |
107 | 107 | |
108 | 108 | return $value; |
@@ -15,195 +15,195 @@ |
||
15 | 15 | */ |
16 | 16 | abstract class AbstractType extends AbstractObject |
17 | 17 | { |
18 | - protected const REGEX_START = '/^'; |
|
19 | - protected const REGEX_FORMAT = '([a-z][a-z0-9]*)'; |
|
20 | - protected const REGEX_CONTENT = '(?:\((.*)\))?'; |
|
21 | - protected const REGEX_RANGE = '(?:([[<])(\\d*),(\\d*)([\\]>]))?'; |
|
22 | - protected const REGEX_DEFAULT = '(?:=(.+))?'; |
|
23 | - protected const REGEX_END = '$/i'; |
|
24 | - |
|
25 | - private static $classTypes = array( |
|
26 | - 'integer' => 'Integer', |
|
27 | - 'int' => 'Integer', |
|
28 | - 'int32' => 'Integer', |
|
29 | - 'int64' => 'Integer', |
|
30 | - 'long' => 'Integer', |
|
31 | - 'float' => 'Number', |
|
32 | - 'double' => 'Number', |
|
33 | - 'string' => 'String', |
|
34 | - 'uuid' => 'StringUuid', |
|
35 | - 'byte' => 'String', |
|
36 | - 'binary' => 'String', |
|
37 | - 'password' => 'String', |
|
38 | - 'enum' => 'String', |
|
39 | - 'boolean' => 'Boolean', |
|
40 | - 'bool' => 'Boolean', |
|
41 | - 'array' => 'Array', |
|
42 | - 'csv' => 'Array', |
|
43 | - 'ssv' => 'Array', |
|
44 | - 'tsv' => 'Array', |
|
45 | - 'pipes' => 'Array', |
|
46 | - 'date' => 'Date', |
|
47 | - 'datetime' => 'Date', |
|
48 | - 'date-time' => 'Date', |
|
49 | - 'object' => 'Object', |
|
50 | - 'refobject' => 'ReferenceObject', |
|
51 | - 'allof' => 'AllOf', |
|
52 | - ); |
|
53 | - |
|
54 | - private $example; |
|
55 | - |
|
56 | - /** |
|
57 | - * @param AbstractObject $parent |
|
58 | - * @param $definition |
|
59 | - * @constructor |
|
60 | - */ |
|
61 | - public function __construct(AbstractObject $parent, $definition) |
|
62 | - { |
|
63 | - parent::__construct($parent); |
|
64 | - |
|
65 | - $this->parseDefinition($definition); |
|
66 | - } |
|
67 | - |
|
68 | - abstract protected function parseDefinition($definition); |
|
69 | - |
|
70 | - /** |
|
71 | - * @param AbstractObject $parent |
|
72 | - * @param string $definition |
|
73 | - * @param string $error |
|
74 | - * @return self |
|
75 | - * @throws Exception |
|
76 | - */ |
|
77 | - public static function typeFactory($parent, $definition, $error = "Unparseable schema type definition: '%s'") |
|
78 | - { |
|
79 | - // Parse regex |
|
80 | - $match = []; |
|
81 | - if (preg_match('/^([a-z]+)/i', $definition, $match) === 1) { |
|
82 | - $format = strtolower($match[1]); |
|
83 | - } elseif (preg_match('/^(\[)(?:.*?)\]$/', $definition, $match) === 1) { |
|
84 | - $format = 'array'; |
|
85 | - } elseif (preg_match('/^(\{)(?:.*?)\}$/', $definition, $match) === 1) { |
|
86 | - $format = 'object'; |
|
87 | - } else { |
|
88 | - throw new Exception(sprintf($error, $definition)); |
|
89 | - } |
|
90 | - |
|
91 | - // Internal type if type known and not overwritten by definition |
|
92 | - if ($parent->getTypeRegistry()->has($format)) { |
|
93 | - $class = $parent->getTypeRegistry()->get($format); |
|
94 | - return new $class($parent, $definition); |
|
95 | - } |
|
96 | - |
|
97 | - if (isset(self::$classTypes[$format])) { |
|
98 | - $type = self::$classTypes[$format]; |
|
99 | - $class = "\\SwaggerGen\\Swagger\\Type\\{$type}Type"; |
|
100 | - return new $class($parent, $definition); |
|
101 | - } |
|
102 | - |
|
103 | - return new ReferenceObjectType($parent, $definition); |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * Swap values of two variables. |
|
108 | - * Used for sorting. |
|
109 | - * @param mixed $a |
|
110 | - * @param mixed $b |
|
111 | - */ |
|
112 | - protected static function swap(&$a, &$b): void |
|
113 | - { |
|
114 | - $tmp = $a; |
|
115 | - $a = $b; |
|
116 | - $b = $tmp; |
|
117 | - } |
|
118 | - |
|
119 | - /** |
|
120 | - * @param string $list |
|
121 | - * @return array |
|
122 | - */ |
|
123 | - protected static function parseList($list): array |
|
124 | - { |
|
125 | - $ret = []; |
|
126 | - while ($item = self::parseListItem($list)) { |
|
127 | - $ret[] = $item; |
|
128 | - } |
|
129 | - return $ret; |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Extract an item from a comma-separated list of items. |
|
134 | - * |
|
135 | - * i.e. `a(x(x,x)),b(x)` returns `a(x(x,x))` and changes `$list` into `b(x)`. |
|
136 | - * Note: brace nesting is not checked, e.g. `a{b(})` is a valid list item. |
|
137 | - * |
|
138 | - * @param string $list the list to parse |
|
139 | - * @return string the extracted item |
|
140 | - */ |
|
141 | - protected static function parseListItem(&$list): string |
|
142 | - { |
|
143 | - $item = ''; |
|
144 | - |
|
145 | - $depth = 0; |
|
146 | - $index = 0; |
|
147 | - while ($index < strlen($list)) { |
|
148 | - $c = $list[$index++]; |
|
149 | - |
|
150 | - if (str_contains('{([<', $c)) { |
|
151 | - ++$depth; |
|
152 | - } elseif (str_contains('})]>', $c)) { |
|
153 | - --$depth; |
|
154 | - } elseif ($c === ',' && $depth === 0) { |
|
155 | - break; |
|
156 | - } |
|
157 | - |
|
158 | - $item .= $c; |
|
159 | - } |
|
160 | - $list = substr($list, $index); |
|
161 | - |
|
162 | - return $item; |
|
163 | - } |
|
164 | - |
|
165 | - /** |
|
166 | - * Overwrites default AbstractObject parser, since Types should not handle |
|
167 | - * extensions themselves. |
|
168 | - * |
|
169 | - * @param string $command |
|
170 | - * @param string $data |
|
171 | - * @return AbstractType|boolean |
|
172 | - * @throws Exception |
|
173 | - */ |
|
174 | - public function handleCommand($command, $data = null) |
|
175 | - { |
|
176 | - if (strtolower($command) === 'example') { |
|
177 | - if ($data === '') { |
|
178 | - throw new Exception("Missing content for type example"); |
|
179 | - } |
|
180 | - $json = preg_replace_callback('/([^{}:,]+)/', static function ($match) { |
|
181 | - json_decode($match[1]); |
|
182 | - return json_last_error() === JSON_ERROR_NONE ? $match[1] : json_encode($match[1]); |
|
183 | - }, trim($data)); |
|
184 | - $this->example = json_decode($json, true); |
|
185 | - |
|
186 | - // In case input contains special chars, the above preg_replace would fail |
|
187 | - // Input could be a well-formed json already though |
|
188 | - if ($this->example === null) { |
|
189 | - $this->example = json_decode($data, true); |
|
190 | - } |
|
191 | - // If all fails, use input as-is |
|
192 | - if ($this->example === null) { |
|
193 | - $this->example = $data; |
|
194 | - } |
|
195 | - |
|
196 | - return $this; |
|
197 | - } |
|
198 | - |
|
199 | - return false; |
|
200 | - } |
|
201 | - |
|
202 | - public function toArray(): array |
|
203 | - { |
|
204 | - return self::arrayFilterNull(array_merge(array( |
|
205 | - 'example' => $this->example, |
|
206 | - ), parent::toArray())); |
|
207 | - } |
|
18 | + protected const REGEX_START = '/^'; |
|
19 | + protected const REGEX_FORMAT = '([a-z][a-z0-9]*)'; |
|
20 | + protected const REGEX_CONTENT = '(?:\((.*)\))?'; |
|
21 | + protected const REGEX_RANGE = '(?:([[<])(\\d*),(\\d*)([\\]>]))?'; |
|
22 | + protected const REGEX_DEFAULT = '(?:=(.+))?'; |
|
23 | + protected const REGEX_END = '$/i'; |
|
24 | + |
|
25 | + private static $classTypes = array( |
|
26 | + 'integer' => 'Integer', |
|
27 | + 'int' => 'Integer', |
|
28 | + 'int32' => 'Integer', |
|
29 | + 'int64' => 'Integer', |
|
30 | + 'long' => 'Integer', |
|
31 | + 'float' => 'Number', |
|
32 | + 'double' => 'Number', |
|
33 | + 'string' => 'String', |
|
34 | + 'uuid' => 'StringUuid', |
|
35 | + 'byte' => 'String', |
|
36 | + 'binary' => 'String', |
|
37 | + 'password' => 'String', |
|
38 | + 'enum' => 'String', |
|
39 | + 'boolean' => 'Boolean', |
|
40 | + 'bool' => 'Boolean', |
|
41 | + 'array' => 'Array', |
|
42 | + 'csv' => 'Array', |
|
43 | + 'ssv' => 'Array', |
|
44 | + 'tsv' => 'Array', |
|
45 | + 'pipes' => 'Array', |
|
46 | + 'date' => 'Date', |
|
47 | + 'datetime' => 'Date', |
|
48 | + 'date-time' => 'Date', |
|
49 | + 'object' => 'Object', |
|
50 | + 'refobject' => 'ReferenceObject', |
|
51 | + 'allof' => 'AllOf', |
|
52 | + ); |
|
53 | + |
|
54 | + private $example; |
|
55 | + |
|
56 | + /** |
|
57 | + * @param AbstractObject $parent |
|
58 | + * @param $definition |
|
59 | + * @constructor |
|
60 | + */ |
|
61 | + public function __construct(AbstractObject $parent, $definition) |
|
62 | + { |
|
63 | + parent::__construct($parent); |
|
64 | + |
|
65 | + $this->parseDefinition($definition); |
|
66 | + } |
|
67 | + |
|
68 | + abstract protected function parseDefinition($definition); |
|
69 | + |
|
70 | + /** |
|
71 | + * @param AbstractObject $parent |
|
72 | + * @param string $definition |
|
73 | + * @param string $error |
|
74 | + * @return self |
|
75 | + * @throws Exception |
|
76 | + */ |
|
77 | + public static function typeFactory($parent, $definition, $error = "Unparseable schema type definition: '%s'") |
|
78 | + { |
|
79 | + // Parse regex |
|
80 | + $match = []; |
|
81 | + if (preg_match('/^([a-z]+)/i', $definition, $match) === 1) { |
|
82 | + $format = strtolower($match[1]); |
|
83 | + } elseif (preg_match('/^(\[)(?:.*?)\]$/', $definition, $match) === 1) { |
|
84 | + $format = 'array'; |
|
85 | + } elseif (preg_match('/^(\{)(?:.*?)\}$/', $definition, $match) === 1) { |
|
86 | + $format = 'object'; |
|
87 | + } else { |
|
88 | + throw new Exception(sprintf($error, $definition)); |
|
89 | + } |
|
90 | + |
|
91 | + // Internal type if type known and not overwritten by definition |
|
92 | + if ($parent->getTypeRegistry()->has($format)) { |
|
93 | + $class = $parent->getTypeRegistry()->get($format); |
|
94 | + return new $class($parent, $definition); |
|
95 | + } |
|
96 | + |
|
97 | + if (isset(self::$classTypes[$format])) { |
|
98 | + $type = self::$classTypes[$format]; |
|
99 | + $class = "\\SwaggerGen\\Swagger\\Type\\{$type}Type"; |
|
100 | + return new $class($parent, $definition); |
|
101 | + } |
|
102 | + |
|
103 | + return new ReferenceObjectType($parent, $definition); |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * Swap values of two variables. |
|
108 | + * Used for sorting. |
|
109 | + * @param mixed $a |
|
110 | + * @param mixed $b |
|
111 | + */ |
|
112 | + protected static function swap(&$a, &$b): void |
|
113 | + { |
|
114 | + $tmp = $a; |
|
115 | + $a = $b; |
|
116 | + $b = $tmp; |
|
117 | + } |
|
118 | + |
|
119 | + /** |
|
120 | + * @param string $list |
|
121 | + * @return array |
|
122 | + */ |
|
123 | + protected static function parseList($list): array |
|
124 | + { |
|
125 | + $ret = []; |
|
126 | + while ($item = self::parseListItem($list)) { |
|
127 | + $ret[] = $item; |
|
128 | + } |
|
129 | + return $ret; |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Extract an item from a comma-separated list of items. |
|
134 | + * |
|
135 | + * i.e. `a(x(x,x)),b(x)` returns `a(x(x,x))` and changes `$list` into `b(x)`. |
|
136 | + * Note: brace nesting is not checked, e.g. `a{b(})` is a valid list item. |
|
137 | + * |
|
138 | + * @param string $list the list to parse |
|
139 | + * @return string the extracted item |
|
140 | + */ |
|
141 | + protected static function parseListItem(&$list): string |
|
142 | + { |
|
143 | + $item = ''; |
|
144 | + |
|
145 | + $depth = 0; |
|
146 | + $index = 0; |
|
147 | + while ($index < strlen($list)) { |
|
148 | + $c = $list[$index++]; |
|
149 | + |
|
150 | + if (str_contains('{([<', $c)) { |
|
151 | + ++$depth; |
|
152 | + } elseif (str_contains('})]>', $c)) { |
|
153 | + --$depth; |
|
154 | + } elseif ($c === ',' && $depth === 0) { |
|
155 | + break; |
|
156 | + } |
|
157 | + |
|
158 | + $item .= $c; |
|
159 | + } |
|
160 | + $list = substr($list, $index); |
|
161 | + |
|
162 | + return $item; |
|
163 | + } |
|
164 | + |
|
165 | + /** |
|
166 | + * Overwrites default AbstractObject parser, since Types should not handle |
|
167 | + * extensions themselves. |
|
168 | + * |
|
169 | + * @param string $command |
|
170 | + * @param string $data |
|
171 | + * @return AbstractType|boolean |
|
172 | + * @throws Exception |
|
173 | + */ |
|
174 | + public function handleCommand($command, $data = null) |
|
175 | + { |
|
176 | + if (strtolower($command) === 'example') { |
|
177 | + if ($data === '') { |
|
178 | + throw new Exception("Missing content for type example"); |
|
179 | + } |
|
180 | + $json = preg_replace_callback('/([^{}:,]+)/', static function ($match) { |
|
181 | + json_decode($match[1]); |
|
182 | + return json_last_error() === JSON_ERROR_NONE ? $match[1] : json_encode($match[1]); |
|
183 | + }, trim($data)); |
|
184 | + $this->example = json_decode($json, true); |
|
185 | + |
|
186 | + // In case input contains special chars, the above preg_replace would fail |
|
187 | + // Input could be a well-formed json already though |
|
188 | + if ($this->example === null) { |
|
189 | + $this->example = json_decode($data, true); |
|
190 | + } |
|
191 | + // If all fails, use input as-is |
|
192 | + if ($this->example === null) { |
|
193 | + $this->example = $data; |
|
194 | + } |
|
195 | + |
|
196 | + return $this; |
|
197 | + } |
|
198 | + |
|
199 | + return false; |
|
200 | + } |
|
201 | + |
|
202 | + public function toArray(): array |
|
203 | + { |
|
204 | + return self::arrayFilterNull(array_merge(array( |
|
205 | + 'example' => $this->example, |
|
206 | + ), parent::toArray())); |
|
207 | + } |
|
208 | 208 | |
209 | 209 | } |
@@ -15,48 +15,48 @@ |
||
15 | 15 | class ReferenceObjectType extends AbstractType |
16 | 16 | { |
17 | 17 | |
18 | - private $reference; |
|
19 | - |
|
20 | - public function toArray(): array |
|
21 | - { |
|
22 | - return self::arrayFilterNull(array_merge(array( |
|
23 | - '$ref' => '#/definitions/' . $this->reference, |
|
24 | - ), parent::toArray())); |
|
25 | - } |
|
26 | - |
|
27 | - public function __toString() |
|
28 | - { |
|
29 | - return __CLASS__ . ' ' . $this->reference; |
|
30 | - } |
|
31 | - |
|
32 | - /** |
|
33 | - * @throws Exception |
|
34 | - */ |
|
35 | - protected function parseDefinition($definition): void |
|
36 | - { |
|
37 | - $definition = self::trim($definition); |
|
38 | - |
|
39 | - $match = []; |
|
40 | - if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_CONTENT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
41 | - throw new Exception('Unparseable string definition: \'' . $definition . '\''); |
|
42 | - } |
|
43 | - |
|
44 | - $type = strtolower($match[1]); |
|
45 | - |
|
46 | - $reference = null; |
|
47 | - if ($type === 'refobject') { |
|
48 | - if (isset($match[2])) { |
|
49 | - $reference = $match[2]; |
|
50 | - } |
|
51 | - } else { |
|
52 | - $reference = $match[1]; |
|
53 | - } |
|
54 | - |
|
55 | - if (empty($reference)) { |
|
56 | - throw new Exception('Referenced object name missing: \'' . $definition . '\''); |
|
57 | - } |
|
58 | - |
|
59 | - $this->reference = $reference; |
|
60 | - } |
|
18 | + private $reference; |
|
19 | + |
|
20 | + public function toArray(): array |
|
21 | + { |
|
22 | + return self::arrayFilterNull(array_merge(array( |
|
23 | + '$ref' => '#/definitions/' . $this->reference, |
|
24 | + ), parent::toArray())); |
|
25 | + } |
|
26 | + |
|
27 | + public function __toString() |
|
28 | + { |
|
29 | + return __CLASS__ . ' ' . $this->reference; |
|
30 | + } |
|
31 | + |
|
32 | + /** |
|
33 | + * @throws Exception |
|
34 | + */ |
|
35 | + protected function parseDefinition($definition): void |
|
36 | + { |
|
37 | + $definition = self::trim($definition); |
|
38 | + |
|
39 | + $match = []; |
|
40 | + if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_CONTENT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
41 | + throw new Exception('Unparseable string definition: \'' . $definition . '\''); |
|
42 | + } |
|
43 | + |
|
44 | + $type = strtolower($match[1]); |
|
45 | + |
|
46 | + $reference = null; |
|
47 | + if ($type === 'refobject') { |
|
48 | + if (isset($match[2])) { |
|
49 | + $reference = $match[2]; |
|
50 | + } |
|
51 | + } else { |
|
52 | + $reference = $match[1]; |
|
53 | + } |
|
54 | + |
|
55 | + if (empty($reference)) { |
|
56 | + throw new Exception('Referenced object name missing: \'' . $definition . '\''); |
|
57 | + } |
|
58 | + |
|
59 | + $this->reference = $reference; |
|
60 | + } |
|
61 | 61 | |
62 | 62 | } |