@@ -15,286 +15,286 @@ |
||
15 | 15 | class ObjectType extends AbstractType |
16 | 16 | { |
17 | 17 | |
18 | - /** @noinspection PhpRegExpUnsupportedModifierInspection */ |
|
19 | - const REGEX_OBJECT_CONTENT = '(?:(\{)(.*)\})?'; |
|
20 | - const REGEX_PROP_START = '/^'; |
|
21 | - const REGEX_PROP_NAME = '([^?!:]+)'; |
|
22 | - /** @noinspection PhpRegExpUnsupportedModifierInspection */ |
|
23 | - const REGEX_PROP_REQUIRED = '([\?!])?'; |
|
24 | - const REGEX_PROP_ASSIGN = ':'; |
|
25 | - const REGEX_PROP_DEFINITION = '(.+)'; |
|
26 | - /** @noinspection PhpRegExpUnsupportedModifierInspection |
|
27 | - * @noinspection PhpRegExpInvalidDelimiterInspection |
|
28 | - */ |
|
29 | - const REGEX_PROP_ADDITIONAL = '\.\.\.(!|.+)?'; |
|
30 | - const REGEX_PROP_END = '$/'; |
|
18 | + /** @noinspection PhpRegExpUnsupportedModifierInspection */ |
|
19 | + const REGEX_OBJECT_CONTENT = '(?:(\{)(.*)\})?'; |
|
20 | + const REGEX_PROP_START = '/^'; |
|
21 | + const REGEX_PROP_NAME = '([^?!:]+)'; |
|
22 | + /** @noinspection PhpRegExpUnsupportedModifierInspection */ |
|
23 | + const REGEX_PROP_REQUIRED = '([\?!])?'; |
|
24 | + const REGEX_PROP_ASSIGN = ':'; |
|
25 | + const REGEX_PROP_DEFINITION = '(.+)'; |
|
26 | + /** @noinspection PhpRegExpUnsupportedModifierInspection |
|
27 | + * @noinspection PhpRegExpInvalidDelimiterInspection |
|
28 | + */ |
|
29 | + const REGEX_PROP_ADDITIONAL = '\.\.\.(!|.+)?'; |
|
30 | + const REGEX_PROP_END = '$/'; |
|
31 | 31 | |
32 | - private $minProperties = null; |
|
33 | - private $maxProperties = null; |
|
34 | - private $discriminator = null; |
|
35 | - private $additionalProperties = null; |
|
36 | - private $required = array(); |
|
32 | + private $minProperties = null; |
|
33 | + private $maxProperties = null; |
|
34 | + private $discriminator = null; |
|
35 | + private $additionalProperties = null; |
|
36 | + private $required = array(); |
|
37 | 37 | |
38 | - /** |
|
39 | - * @var Property[] |
|
40 | - */ |
|
41 | - private $properties = array(); |
|
38 | + /** |
|
39 | + * @var Property[] |
|
40 | + */ |
|
41 | + private $properties = array(); |
|
42 | 42 | |
43 | - /** |
|
44 | - * @var Property |
|
45 | - */ |
|
46 | - private $mostRecentProperty = null; |
|
43 | + /** |
|
44 | + * @var Property |
|
45 | + */ |
|
46 | + private $mostRecentProperty = null; |
|
47 | 47 | |
48 | - /** |
|
49 | - * @throws Exception |
|
50 | - */ |
|
51 | - protected function parseDefinition($definition) |
|
52 | - { |
|
53 | - $definition = self::trim($definition); |
|
48 | + /** |
|
49 | + * @throws Exception |
|
50 | + */ |
|
51 | + protected function parseDefinition($definition) |
|
52 | + { |
|
53 | + $definition = self::trim($definition); |
|
54 | 54 | |
55 | - $match = array(); |
|
56 | - if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_CONTENT . self::REGEX_RANGE . self::REGEX_END, $definition, $match) === 1) { |
|
57 | - $match[1] = strtolower($match[1]); |
|
58 | - } elseif (preg_match(self::REGEX_START . self::REGEX_OBJECT_CONTENT . self::REGEX_RANGE . self::REGEX_END, $definition, $match) === 1) { |
|
59 | - $match[1] = 'object'; |
|
60 | - } else { |
|
61 | - throw new Exception('Unparseable object definition: \'' . $definition . '\''); |
|
62 | - } |
|
55 | + $match = array(); |
|
56 | + if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_CONTENT . self::REGEX_RANGE . self::REGEX_END, $definition, $match) === 1) { |
|
57 | + $match[1] = strtolower($match[1]); |
|
58 | + } elseif (preg_match(self::REGEX_START . self::REGEX_OBJECT_CONTENT . self::REGEX_RANGE . self::REGEX_END, $definition, $match) === 1) { |
|
59 | + $match[1] = 'object'; |
|
60 | + } else { |
|
61 | + throw new Exception('Unparseable object definition: \'' . $definition . '\''); |
|
62 | + } |
|
63 | 63 | |
64 | - $this->parseFormat($definition, $match); |
|
65 | - $this->parseProperties($definition, $match); |
|
66 | - $this->parseRange($definition, $match); |
|
67 | - } |
|
64 | + $this->parseFormat($definition, $match); |
|
65 | + $this->parseProperties($definition, $match); |
|
66 | + $this->parseRange($definition, $match); |
|
67 | + } |
|
68 | 68 | |
69 | - /** |
|
70 | - * @param string $definition |
|
71 | - * @param string[] $match |
|
72 | - * @throws Exception |
|
73 | - */ |
|
74 | - private function parseFormat($definition, $match) |
|
75 | - { |
|
76 | - if (strtolower($match[1]) !== 'object') { |
|
77 | - throw new Exception("Not an object: '{$definition}'"); |
|
78 | - } |
|
79 | - } |
|
69 | + /** |
|
70 | + * @param string $definition |
|
71 | + * @param string[] $match |
|
72 | + * @throws Exception |
|
73 | + */ |
|
74 | + private function parseFormat($definition, $match) |
|
75 | + { |
|
76 | + if (strtolower($match[1]) !== 'object') { |
|
77 | + throw new Exception("Not an object: '{$definition}'"); |
|
78 | + } |
|
79 | + } |
|
80 | 80 | |
81 | - /** |
|
82 | - * @param AbstractType|bool $type |
|
83 | - * @return void |
|
84 | - * @throws Exception |
|
85 | - */ |
|
86 | - private function setAdditionalProperties($type) |
|
87 | - { |
|
88 | - if ($this->additionalProperties !== null) { |
|
89 | - throw new Exception('Additional properties may only be set once'); |
|
90 | - } |
|
91 | - $this->additionalProperties = $type; |
|
92 | - } |
|
81 | + /** |
|
82 | + * @param AbstractType|bool $type |
|
83 | + * @return void |
|
84 | + * @throws Exception |
|
85 | + */ |
|
86 | + private function setAdditionalProperties($type) |
|
87 | + { |
|
88 | + if ($this->additionalProperties !== null) { |
|
89 | + throw new Exception('Additional properties may only be set once'); |
|
90 | + } |
|
91 | + $this->additionalProperties = $type; |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * @param string $definition |
|
96 | - * @param string[] $match |
|
97 | - * @throws Exception |
|
98 | - * @throws Exception |
|
99 | - * @throws Exception |
|
100 | - * @throws Exception |
|
101 | - * @throws Exception |
|
102 | - * @throws Exception |
|
103 | - */ |
|
104 | - private function parseProperties($definition, $match) |
|
105 | - { |
|
106 | - if (empty($match[2])) { |
|
107 | - return; |
|
108 | - } |
|
94 | + /** |
|
95 | + * @param string $definition |
|
96 | + * @param string[] $match |
|
97 | + * @throws Exception |
|
98 | + * @throws Exception |
|
99 | + * @throws Exception |
|
100 | + * @throws Exception |
|
101 | + * @throws Exception |
|
102 | + * @throws Exception |
|
103 | + */ |
|
104 | + private function parseProperties($definition, $match) |
|
105 | + { |
|
106 | + if (empty($match[2])) { |
|
107 | + return; |
|
108 | + } |
|
109 | 109 | |
110 | - while (($property = self::parseListItem($match[2])) !== '') { |
|
111 | - $prop_match = array(); |
|
112 | - if (preg_match(self::REGEX_PROP_START . self::REGEX_PROP_ADDITIONAL . self::REGEX_PROP_END, $property, $prop_match) === 1) { |
|
113 | - if (empty($prop_match[1])) { |
|
114 | - $this->setAdditionalProperties(true); |
|
115 | - } else if ($prop_match[1] === '!') { |
|
116 | - $this->setAdditionalProperties(false); |
|
117 | - } else { |
|
118 | - $this->setAdditionalProperties(self::typeFactory($this, $prop_match[1], "Unparseable additional properties definition: '...%s'")); |
|
119 | - } |
|
120 | - continue; |
|
121 | - } |
|
122 | - if (preg_match(self::REGEX_PROP_START . self::REGEX_PROP_NAME . self::REGEX_PROP_REQUIRED . self::REGEX_PROP_ASSIGN . self::REGEX_PROP_DEFINITION . self::REGEX_PROP_END, $property, $prop_match) !== 1) { |
|
123 | - throw new Exception("Unparseable property definition: '{$property}'"); |
|
124 | - } |
|
125 | - $this->properties[$prop_match[1]] = new Property($this, $prop_match[3]); |
|
126 | - if ($prop_match[2] !== '!' && $prop_match[2] !== '?') { |
|
127 | - $this->required[$prop_match[1]] = true; |
|
128 | - } |
|
129 | - } |
|
110 | + while (($property = self::parseListItem($match[2])) !== '') { |
|
111 | + $prop_match = array(); |
|
112 | + if (preg_match(self::REGEX_PROP_START . self::REGEX_PROP_ADDITIONAL . self::REGEX_PROP_END, $property, $prop_match) === 1) { |
|
113 | + if (empty($prop_match[1])) { |
|
114 | + $this->setAdditionalProperties(true); |
|
115 | + } else if ($prop_match[1] === '!') { |
|
116 | + $this->setAdditionalProperties(false); |
|
117 | + } else { |
|
118 | + $this->setAdditionalProperties(self::typeFactory($this, $prop_match[1], "Unparseable additional properties definition: '...%s'")); |
|
119 | + } |
|
120 | + continue; |
|
121 | + } |
|
122 | + if (preg_match(self::REGEX_PROP_START . self::REGEX_PROP_NAME . self::REGEX_PROP_REQUIRED . self::REGEX_PROP_ASSIGN . self::REGEX_PROP_DEFINITION . self::REGEX_PROP_END, $property, $prop_match) !== 1) { |
|
123 | + throw new Exception("Unparseable property definition: '{$property}'"); |
|
124 | + } |
|
125 | + $this->properties[$prop_match[1]] = new Property($this, $prop_match[3]); |
|
126 | + if ($prop_match[2] !== '!' && $prop_match[2] !== '?') { |
|
127 | + $this->required[$prop_match[1]] = true; |
|
128 | + } |
|
129 | + } |
|
130 | 130 | |
131 | - } |
|
131 | + } |
|
132 | 132 | |
133 | - /** |
|
134 | - * @param string $definition |
|
135 | - * @param string[] $match |
|
136 | - * @throws Exception |
|
137 | - */ |
|
138 | - private function parseRange($definition, $match) |
|
139 | - { |
|
140 | - if (!empty($match[3])) { |
|
141 | - if ($match[4] === '' && $match[5] === '') { |
|
142 | - throw new Exception("Empty object range: '{$definition}'"); |
|
143 | - } |
|
133 | + /** |
|
134 | + * @param string $definition |
|
135 | + * @param string[] $match |
|
136 | + * @throws Exception |
|
137 | + */ |
|
138 | + private function parseRange($definition, $match) |
|
139 | + { |
|
140 | + if (!empty($match[3])) { |
|
141 | + if ($match[4] === '' && $match[5] === '') { |
|
142 | + throw new Exception("Empty object range: '{$definition}'"); |
|
143 | + } |
|
144 | 144 | |
145 | - $exclusiveMinimum = $match[3] == '<'; |
|
146 | - $this->minProperties = $match[4] === '' ? null : (int)$match[4]; |
|
147 | - $this->maxProperties = $match[5] === '' ? null : (int)$match[5]; |
|
148 | - $exclusiveMaximum = isset($match[6]) ? ($match[6] == '>') : null; |
|
149 | - if ($this->minProperties && $this->maxProperties && $this->minProperties > $this->maxProperties) { |
|
150 | - self::swap($this->minProperties, $this->maxProperties); |
|
151 | - self::swap($exclusiveMinimum, $exclusiveMaximum); |
|
152 | - } |
|
153 | - $this->minProperties = $this->minProperties === null ? null : max(0, $exclusiveMinimum ? $this->minProperties + 1 : $this->minProperties); |
|
154 | - $this->maxProperties = $this->maxProperties === null ? null : max(0, $exclusiveMaximum ? $this->maxProperties - 1 : $this->maxProperties); |
|
155 | - } |
|
156 | - } |
|
145 | + $exclusiveMinimum = $match[3] == '<'; |
|
146 | + $this->minProperties = $match[4] === '' ? null : (int)$match[4]; |
|
147 | + $this->maxProperties = $match[5] === '' ? null : (int)$match[5]; |
|
148 | + $exclusiveMaximum = isset($match[6]) ? ($match[6] == '>') : null; |
|
149 | + if ($this->minProperties && $this->maxProperties && $this->minProperties > $this->maxProperties) { |
|
150 | + self::swap($this->minProperties, $this->maxProperties); |
|
151 | + self::swap($exclusiveMinimum, $exclusiveMaximum); |
|
152 | + } |
|
153 | + $this->minProperties = $this->minProperties === null ? null : max(0, $exclusiveMinimum ? $this->minProperties + 1 : $this->minProperties); |
|
154 | + $this->maxProperties = $this->maxProperties === null ? null : max(0, $exclusiveMaximum ? $this->maxProperties - 1 : $this->maxProperties); |
|
155 | + } |
|
156 | + } |
|
157 | 157 | |
158 | - /** |
|
159 | - * @param string|false $discriminator |
|
160 | - * @throws Exception |
|
161 | - * @throws Exception |
|
162 | - */ |
|
163 | - private function setDiscriminator($discriminator) |
|
164 | - { |
|
165 | - if (!empty($this->discriminator)) { |
|
166 | - throw new Exception("Discriminator may only be set once, " |
|
167 | - . "trying to change it " |
|
168 | - . "from '{$this->discriminator}' " |
|
169 | - . "to '{$discriminator}'"); |
|
170 | - } |
|
171 | - if (isset($this->properties[$discriminator]) && empty($this->required[$discriminator])) { |
|
172 | - throw new Exception("Discriminator must be a required property, " |
|
173 | - . "property '{$discriminator}' is not required"); |
|
174 | - } |
|
175 | - $this->discriminator = $discriminator; |
|
176 | - } |
|
158 | + /** |
|
159 | + * @param string|false $discriminator |
|
160 | + * @throws Exception |
|
161 | + * @throws Exception |
|
162 | + */ |
|
163 | + private function setDiscriminator($discriminator) |
|
164 | + { |
|
165 | + if (!empty($this->discriminator)) { |
|
166 | + throw new Exception("Discriminator may only be set once, " |
|
167 | + . "trying to change it " |
|
168 | + . "from '{$this->discriminator}' " |
|
169 | + . "to '{$discriminator}'"); |
|
170 | + } |
|
171 | + if (isset($this->properties[$discriminator]) && empty($this->required[$discriminator])) { |
|
172 | + throw new Exception("Discriminator must be a required property, " |
|
173 | + . "property '{$discriminator}' is not required"); |
|
174 | + } |
|
175 | + $this->discriminator = $discriminator; |
|
176 | + } |
|
177 | 177 | |
178 | - /** |
|
179 | - * @param string $command The comment command |
|
180 | - * @param string $data Any data added after the command |
|
181 | - * @return AbstractType|boolean |
|
182 | - * @throws Exception |
|
183 | - * @throws Exception |
|
184 | - * @throws Exception |
|
185 | - * @throws Exception |
|
186 | - * @throws Exception |
|
187 | - * @throws Exception |
|
188 | - * @throws Exception |
|
189 | - * @throws Exception |
|
190 | - * @throws Exception |
|
191 | - * @throws Exception |
|
192 | - * @throws Exception |
|
193 | - * @throws Exception |
|
194 | - */ |
|
195 | - public function handleCommand($command, $data = null) |
|
196 | - { |
|
197 | - switch (strtolower($command)) { |
|
198 | - // type name description... |
|
199 | - case 'additionalproperties': |
|
200 | - $value = self::wordShift($data); |
|
201 | - if (is_bool($value)) { |
|
202 | - $type = $value; |
|
203 | - } else if ($value === 'false') { |
|
204 | - $type = false; |
|
205 | - } else if ($value === 'true') { |
|
206 | - $type = true; |
|
207 | - } else { |
|
208 | - $type = self::typeFactory($this, $value, "Unparseable additional properties definition: '%s'"); |
|
209 | - } |
|
210 | - $this->setAdditionalProperties($type); |
|
211 | - return $this; |
|
212 | - case 'discriminator': |
|
213 | - $discriminator = self::wordShift($data); |
|
214 | - $this->setDiscriminator($discriminator); |
|
215 | - return $this; |
|
216 | - case 'property': |
|
217 | - case 'property?': |
|
218 | - case 'property!': |
|
219 | - $definition = self::wordShift($data); |
|
220 | - if (empty($definition)) { |
|
221 | - throw new Exception("Missing property definition"); |
|
222 | - } |
|
178 | + /** |
|
179 | + * @param string $command The comment command |
|
180 | + * @param string $data Any data added after the command |
|
181 | + * @return AbstractType|boolean |
|
182 | + * @throws Exception |
|
183 | + * @throws Exception |
|
184 | + * @throws Exception |
|
185 | + * @throws Exception |
|
186 | + * @throws Exception |
|
187 | + * @throws Exception |
|
188 | + * @throws Exception |
|
189 | + * @throws Exception |
|
190 | + * @throws Exception |
|
191 | + * @throws Exception |
|
192 | + * @throws Exception |
|
193 | + * @throws Exception |
|
194 | + */ |
|
195 | + public function handleCommand($command, $data = null) |
|
196 | + { |
|
197 | + switch (strtolower($command)) { |
|
198 | + // type name description... |
|
199 | + case 'additionalproperties': |
|
200 | + $value = self::wordShift($data); |
|
201 | + if (is_bool($value)) { |
|
202 | + $type = $value; |
|
203 | + } else if ($value === 'false') { |
|
204 | + $type = false; |
|
205 | + } else if ($value === 'true') { |
|
206 | + $type = true; |
|
207 | + } else { |
|
208 | + $type = self::typeFactory($this, $value, "Unparseable additional properties definition: '%s'"); |
|
209 | + } |
|
210 | + $this->setAdditionalProperties($type); |
|
211 | + return $this; |
|
212 | + case 'discriminator': |
|
213 | + $discriminator = self::wordShift($data); |
|
214 | + $this->setDiscriminator($discriminator); |
|
215 | + return $this; |
|
216 | + case 'property': |
|
217 | + case 'property?': |
|
218 | + case 'property!': |
|
219 | + $definition = self::wordShift($data); |
|
220 | + if (empty($definition)) { |
|
221 | + throw new Exception("Missing property definition"); |
|
222 | + } |
|
223 | 223 | |
224 | - $name = self::wordShift($data); |
|
225 | - if (empty($name)) { |
|
226 | - throw new Exception("Missing property name: '{$definition}'"); |
|
227 | - } |
|
224 | + $name = self::wordShift($data); |
|
225 | + if (empty($name)) { |
|
226 | + throw new Exception("Missing property name: '{$definition}'"); |
|
227 | + } |
|
228 | 228 | |
229 | - $readOnly = null; |
|
230 | - $required = false; |
|
231 | - $propertySuffix = substr($command, -1); |
|
232 | - if ($propertySuffix === '!') { |
|
233 | - $readOnly = true; |
|
234 | - } else if ($propertySuffix !== '?') { |
|
235 | - $required = true; |
|
236 | - } |
|
229 | + $readOnly = null; |
|
230 | + $required = false; |
|
231 | + $propertySuffix = substr($command, -1); |
|
232 | + if ($propertySuffix === '!') { |
|
233 | + $readOnly = true; |
|
234 | + } else if ($propertySuffix !== '?') { |
|
235 | + $required = true; |
|
236 | + } |
|
237 | 237 | |
238 | - if (($name === $this->discriminator) && !$required) { |
|
239 | - throw new Exception("Discriminator must be a required property, " |
|
240 | - . "property '{$name}' is not required"); |
|
241 | - } |
|
238 | + if (($name === $this->discriminator) && !$required) { |
|
239 | + throw new Exception("Discriminator must be a required property, " |
|
240 | + . "property '{$name}' is not required"); |
|
241 | + } |
|
242 | 242 | |
243 | - unset($this->required[$name]); |
|
244 | - if ($required) { |
|
245 | - $this->required[$name] = true; |
|
246 | - } |
|
243 | + unset($this->required[$name]); |
|
244 | + if ($required) { |
|
245 | + $this->required[$name] = true; |
|
246 | + } |
|
247 | 247 | |
248 | - $this->mostRecentProperty = new Property($this, $definition, $data, $readOnly); |
|
249 | - $this->properties[$name] = $this->mostRecentProperty; |
|
250 | - return $this; |
|
248 | + $this->mostRecentProperty = new Property($this, $definition, $data, $readOnly); |
|
249 | + $this->properties[$name] = $this->mostRecentProperty; |
|
250 | + return $this; |
|
251 | 251 | |
252 | - case 'min': |
|
253 | - $this->minProperties = (int)$data; |
|
254 | - if ($this->minProperties < 0) { |
|
255 | - throw new Exception("Minimum less than zero: '{$data}'"); |
|
256 | - } |
|
257 | - if ($this->maxProperties !== null && $this->minProperties > $this->maxProperties) { |
|
258 | - throw new Exception("Minimum greater than maximum: '{$data}'"); |
|
259 | - } |
|
260 | - $this->minProperties = (int)$data; |
|
261 | - return $this; |
|
252 | + case 'min': |
|
253 | + $this->minProperties = (int)$data; |
|
254 | + if ($this->minProperties < 0) { |
|
255 | + throw new Exception("Minimum less than zero: '{$data}'"); |
|
256 | + } |
|
257 | + if ($this->maxProperties !== null && $this->minProperties > $this->maxProperties) { |
|
258 | + throw new Exception("Minimum greater than maximum: '{$data}'"); |
|
259 | + } |
|
260 | + $this->minProperties = (int)$data; |
|
261 | + return $this; |
|
262 | 262 | |
263 | - case 'max': |
|
264 | - $this->maxProperties = (int)$data; |
|
265 | - if ($this->minProperties !== null && $this->minProperties > $this->maxProperties) { |
|
266 | - throw new Exception("Maximum less than minimum: '{$data}'"); |
|
267 | - } |
|
268 | - if ($this->maxProperties < 0) { |
|
269 | - throw new Exception("Maximum less than zero: '{$data}'"); |
|
270 | - } |
|
271 | - return $this; |
|
272 | - } |
|
263 | + case 'max': |
|
264 | + $this->maxProperties = (int)$data; |
|
265 | + if ($this->minProperties !== null && $this->minProperties > $this->maxProperties) { |
|
266 | + throw new Exception("Maximum less than minimum: '{$data}'"); |
|
267 | + } |
|
268 | + if ($this->maxProperties < 0) { |
|
269 | + throw new Exception("Maximum less than zero: '{$data}'"); |
|
270 | + } |
|
271 | + return $this; |
|
272 | + } |
|
273 | 273 | |
274 | - // Pass through to most recent Property |
|
275 | - if ($this->mostRecentProperty && $this->mostRecentProperty->handleCommand($command, $data)) { |
|
276 | - return $this; |
|
277 | - } |
|
274 | + // Pass through to most recent Property |
|
275 | + if ($this->mostRecentProperty && $this->mostRecentProperty->handleCommand($command, $data)) { |
|
276 | + return $this; |
|
277 | + } |
|
278 | 278 | |
279 | - return parent::handleCommand($command, $data); |
|
280 | - } |
|
279 | + return parent::handleCommand($command, $data); |
|
280 | + } |
|
281 | 281 | |
282 | - public function toArray(): array |
|
283 | - { |
|
284 | - return self::arrayFilterNull(array_merge(array( |
|
285 | - 'type' => 'object', |
|
286 | - 'required' => array_keys($this->required), |
|
287 | - 'properties' => self::objectsToArray($this->properties), |
|
288 | - 'minProperties' => $this->minProperties, |
|
289 | - 'maxProperties' => $this->maxProperties, |
|
290 | - 'discriminator' => $this->discriminator, |
|
291 | - 'additionalProperties' => $this->additionalProperties instanceof AbstractType ? $this->additionalProperties->toArray() : $this->additionalProperties, |
|
292 | - ), parent::toArray())); |
|
293 | - } |
|
282 | + public function toArray(): array |
|
283 | + { |
|
284 | + return self::arrayFilterNull(array_merge(array( |
|
285 | + 'type' => 'object', |
|
286 | + 'required' => array_keys($this->required), |
|
287 | + 'properties' => self::objectsToArray($this->properties), |
|
288 | + 'minProperties' => $this->minProperties, |
|
289 | + 'maxProperties' => $this->maxProperties, |
|
290 | + 'discriminator' => $this->discriminator, |
|
291 | + 'additionalProperties' => $this->additionalProperties instanceof AbstractType ? $this->additionalProperties->toArray() : $this->additionalProperties, |
|
292 | + ), parent::toArray())); |
|
293 | + } |
|
294 | 294 | |
295 | - public function __toString() |
|
296 | - { |
|
297 | - return __CLASS__; |
|
298 | - } |
|
295 | + public function __toString() |
|
296 | + { |
|
297 | + return __CLASS__; |
|
298 | + } |
|
299 | 299 | |
300 | 300 | } |
@@ -143,8 +143,8 @@ discard block |
||
143 | 143 | } |
144 | 144 | |
145 | 145 | $exclusiveMinimum = $match[3] == '<'; |
146 | - $this->minProperties = $match[4] === '' ? null : (int)$match[4]; |
|
147 | - $this->maxProperties = $match[5] === '' ? null : (int)$match[5]; |
|
146 | + $this->minProperties = $match[4] === '' ? null : (int) $match[4]; |
|
147 | + $this->maxProperties = $match[5] === '' ? null : (int) $match[5]; |
|
148 | 148 | $exclusiveMaximum = isset($match[6]) ? ($match[6] == '>') : null; |
149 | 149 | if ($this->minProperties && $this->maxProperties && $this->minProperties > $this->maxProperties) { |
150 | 150 | self::swap($this->minProperties, $this->maxProperties); |
@@ -250,18 +250,18 @@ discard block |
||
250 | 250 | return $this; |
251 | 251 | |
252 | 252 | case 'min': |
253 | - $this->minProperties = (int)$data; |
|
253 | + $this->minProperties = (int) $data; |
|
254 | 254 | if ($this->minProperties < 0) { |
255 | 255 | throw new Exception("Minimum less than zero: '{$data}'"); |
256 | 256 | } |
257 | 257 | if ($this->maxProperties !== null && $this->minProperties > $this->maxProperties) { |
258 | 258 | throw new Exception("Minimum greater than maximum: '{$data}'"); |
259 | 259 | } |
260 | - $this->minProperties = (int)$data; |
|
260 | + $this->minProperties = (int) $data; |
|
261 | 261 | return $this; |
262 | 262 | |
263 | 263 | case 'max': |
264 | - $this->maxProperties = (int)$data; |
|
264 | + $this->maxProperties = (int) $data; |
|
265 | 265 | if ($this->minProperties !== null && $this->minProperties > $this->maxProperties) { |
266 | 266 | throw new Exception("Maximum less than minimum: '{$data}'"); |
267 | 267 | } |
@@ -15,61 +15,61 @@ |
||
15 | 15 | class BooleanType extends AbstractType |
16 | 16 | { |
17 | 17 | |
18 | - /** @noinspection PhpRegExpUnsupportedModifierInspection */ |
|
19 | - const REGEX_DEFAULT = '(?:=(true|false|1|0))?'; |
|
18 | + /** @noinspection PhpRegExpUnsupportedModifierInspection */ |
|
19 | + const REGEX_DEFAULT = '(?:=(true|false|1|0))?'; |
|
20 | 20 | |
21 | - private $default = null; |
|
21 | + private $default = null; |
|
22 | 22 | |
23 | - /** |
|
24 | - * @throws Exception |
|
25 | - */ |
|
26 | - protected function parseDefinition($definition) |
|
27 | - { |
|
28 | - $match = array(); |
|
29 | - if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
30 | - throw new Exception("Unparseable boolean definition: '{$definition}'"); |
|
31 | - } |
|
23 | + /** |
|
24 | + * @throws Exception |
|
25 | + */ |
|
26 | + protected function parseDefinition($definition) |
|
27 | + { |
|
28 | + $match = array(); |
|
29 | + if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
30 | + throw new Exception("Unparseable boolean definition: '{$definition}'"); |
|
31 | + } |
|
32 | 32 | |
33 | - if (strtolower($match[1]) !== 'boolean') { |
|
34 | - throw new Exception("Not a boolean: '{$definition}'"); |
|
35 | - } |
|
33 | + if (strtolower($match[1]) !== 'boolean') { |
|
34 | + throw new Exception("Not a boolean: '{$definition}'"); |
|
35 | + } |
|
36 | 36 | |
37 | - if (isset($match[2])) { |
|
38 | - $this->default = ($match[2] === '1') || (strtolower($match[2]) === 'true'); |
|
39 | - } |
|
40 | - } |
|
37 | + if (isset($match[2])) { |
|
38 | + $this->default = ($match[2] === '1') || (strtolower($match[2]) === 'true'); |
|
39 | + } |
|
40 | + } |
|
41 | 41 | |
42 | - /** |
|
43 | - * @param string $command The comment command |
|
44 | - * @param string $data Any data added after the command |
|
45 | - * @return AbstractType|boolean |
|
46 | - * @throws Exception |
|
47 | - * @throws Exception |
|
48 | - */ |
|
49 | - public function handleCommand($command, $data = null) |
|
50 | - { |
|
51 | - if (strtolower($command) === 'default') { |
|
52 | - if (!in_array($data, array('0', '1', 'true', 'false'))) { |
|
53 | - throw new Exception("Invalid boolean default: '{$data}'"); |
|
54 | - } |
|
55 | - $this->default = ($data == '1') || (strtolower($data) === 'true'); |
|
56 | - return $this; |
|
57 | - } |
|
42 | + /** |
|
43 | + * @param string $command The comment command |
|
44 | + * @param string $data Any data added after the command |
|
45 | + * @return AbstractType|boolean |
|
46 | + * @throws Exception |
|
47 | + * @throws Exception |
|
48 | + */ |
|
49 | + public function handleCommand($command, $data = null) |
|
50 | + { |
|
51 | + if (strtolower($command) === 'default') { |
|
52 | + if (!in_array($data, array('0', '1', 'true', 'false'))) { |
|
53 | + throw new Exception("Invalid boolean default: '{$data}'"); |
|
54 | + } |
|
55 | + $this->default = ($data == '1') || (strtolower($data) === 'true'); |
|
56 | + return $this; |
|
57 | + } |
|
58 | 58 | |
59 | - return parent::handleCommand($command, $data); |
|
60 | - } |
|
59 | + return parent::handleCommand($command, $data); |
|
60 | + } |
|
61 | 61 | |
62 | - public function toArray(): array |
|
63 | - { |
|
64 | - return self::arrayFilterNull(array_merge(array( |
|
65 | - 'type' => 'boolean', |
|
66 | - 'default' => $this->default, |
|
67 | - ), parent::toArray())); |
|
68 | - } |
|
62 | + public function toArray(): array |
|
63 | + { |
|
64 | + return self::arrayFilterNull(array_merge(array( |
|
65 | + 'type' => 'boolean', |
|
66 | + 'default' => $this->default, |
|
67 | + ), parent::toArray())); |
|
68 | + } |
|
69 | 69 | |
70 | - public function __toString() |
|
71 | - { |
|
72 | - return __CLASS__; |
|
73 | - } |
|
70 | + public function __toString() |
|
71 | + { |
|
72 | + return __CLASS__; |
|
73 | + } |
|
74 | 74 | |
75 | 75 | } |
@@ -16,195 +16,195 @@ |
||
16 | 16 | abstract class AbstractType extends AbstractObject |
17 | 17 | { |
18 | 18 | |
19 | - const REGEX_START = '/^'; |
|
20 | - const REGEX_FORMAT = '([a-z][a-z0-9]*)'; |
|
21 | - const REGEX_CONTENT = '(?:\((.*)\))?'; |
|
22 | - const REGEX_RANGE = '(?:([[<])(\\d*),(\\d*)([\\]>]))?'; |
|
23 | - const REGEX_DEFAULT = '(?:=(.+))?'; |
|
24 | - const REGEX_END = '$/i'; |
|
25 | - |
|
26 | - private static $classTypes = array( |
|
27 | - 'integer' => 'Integer', |
|
28 | - 'int' => 'Integer', |
|
29 | - 'int32' => 'Integer', |
|
30 | - 'int64' => 'Integer', |
|
31 | - 'long' => 'Integer', |
|
32 | - 'float' => 'Number', |
|
33 | - 'double' => 'Number', |
|
34 | - 'string' => 'String', |
|
35 | - 'uuid' => 'StringUuid', |
|
36 | - 'byte' => 'String', |
|
37 | - 'binary' => 'String', |
|
38 | - 'password' => 'String', |
|
39 | - 'enum' => 'String', |
|
40 | - 'boolean' => 'Boolean', |
|
41 | - 'bool' => 'Boolean', |
|
42 | - 'array' => 'Array', |
|
43 | - 'csv' => 'Array', |
|
44 | - 'ssv' => 'Array', |
|
45 | - 'tsv' => 'Array', |
|
46 | - 'pipes' => 'Array', |
|
47 | - 'date' => 'Date', |
|
48 | - 'datetime' => 'Date', |
|
49 | - 'date-time' => 'Date', |
|
50 | - 'object' => 'Object', |
|
51 | - 'refobject' => 'ReferenceObject', |
|
52 | - 'allof' => 'AllOf', |
|
53 | - ); |
|
54 | - |
|
55 | - private $example = null; |
|
56 | - |
|
57 | - /** |
|
58 | - * Swap values of two variables. |
|
59 | - * Used for sorting. |
|
60 | - * @param mixed $a |
|
61 | - * @param mixed $b |
|
62 | - */ |
|
63 | - protected static function swap(&$a, &$b) |
|
64 | - { |
|
65 | - $tmp = $a; |
|
66 | - $a = $b; |
|
67 | - $b = $tmp; |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * @param string $list |
|
72 | - * @return array |
|
73 | - */ |
|
74 | - protected static function parseList($list) |
|
75 | - { |
|
76 | - $ret = array(); |
|
77 | - while ($item = self::parseListItem($list)) { |
|
78 | - $ret[] = $item; |
|
79 | - } |
|
80 | - return $ret; |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * Extract an item from a comma-separated list of items. |
|
85 | - * |
|
86 | - * i.e. `a(x(x,x)),b(x)` returns `a(x(x,x))` and changes `$list` into `b(x)`. |
|
87 | - * Note: brace nesting is not checked, e.g. `a{b(})` is a valid list item. |
|
88 | - * |
|
89 | - * @param string $list the list to parse |
|
90 | - * @return string the extracted item |
|
91 | - */ |
|
92 | - protected static function parseListItem(&$list) |
|
93 | - { |
|
94 | - $item = ''; |
|
95 | - |
|
96 | - $depth = 0; |
|
97 | - $index = 0; |
|
98 | - while ($index < strlen($list)) { |
|
99 | - $c = $list[$index++]; |
|
100 | - |
|
101 | - if (str_contains('{([<', $c)) { |
|
102 | - ++$depth; |
|
103 | - } elseif (str_contains('})]>', $c)) { |
|
104 | - --$depth; |
|
105 | - } elseif ($c === ',' && $depth === 0) { |
|
106 | - break; |
|
107 | - } |
|
108 | - |
|
109 | - $item .= $c; |
|
110 | - } |
|
111 | - $list = substr($list, $index); |
|
112 | - |
|
113 | - return $item; |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * @param AbstractObject $parent |
|
118 | - * @param $definition |
|
119 | - * @constructor |
|
120 | - */ |
|
121 | - public function __construct(AbstractObject $parent, $definition) |
|
122 | - { |
|
123 | - parent::__construct($parent); |
|
124 | - |
|
125 | - $this->parseDefinition($definition); |
|
126 | - } |
|
127 | - |
|
128 | - abstract protected function parseDefinition($definition); |
|
129 | - |
|
130 | - /** |
|
131 | - * Overwrites default AbstractObject parser, since Types should not handle |
|
132 | - * extensions themselves. |
|
133 | - * |
|
134 | - * @param string $command |
|
135 | - * @param string $data |
|
136 | - * @return AbstractType|boolean |
|
137 | - * @throws Exception |
|
138 | - */ |
|
139 | - public function handleCommand($command, $data = null) |
|
140 | - { |
|
141 | - if (strtolower($command) === 'example') { |
|
142 | - if ($data === '') { |
|
143 | - throw new Exception("Missing content for type example"); |
|
144 | - } |
|
145 | - $json = preg_replace_callback('/([^{}:,]+)/', static function ($match) { |
|
146 | - json_decode($match[1]); |
|
147 | - return json_last_error() === JSON_ERROR_NONE ? $match[1] : json_encode($match[1]); |
|
148 | - }, trim($data)); |
|
149 | - $this->example = json_decode($json, true); |
|
150 | - |
|
151 | - // In case input contains special chars, the above preg_replace would fail |
|
152 | - // Input could be a well-formed json already though |
|
153 | - if ($this->example === null) { |
|
154 | - $this->example = json_decode($data, true); |
|
155 | - } |
|
156 | - // If all fails, use input as-is |
|
157 | - if ($this->example === null) { |
|
158 | - $this->example = $data; |
|
159 | - } |
|
160 | - |
|
161 | - return $this; |
|
162 | - } |
|
163 | - |
|
164 | - return false; |
|
165 | - } |
|
166 | - |
|
167 | - public function toArray(): array |
|
168 | - { |
|
169 | - return self::arrayFilterNull(array_merge(array( |
|
170 | - 'example' => $this->example, |
|
171 | - ), parent::toArray())); |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * @param AbstractObject $parent |
|
176 | - * @param string $definition |
|
177 | - * @param string $error |
|
178 | - * @return self |
|
179 | - * @throws Exception |
|
180 | - */ |
|
181 | - public static function typeFactory($parent, $definition, $error = "Unparseable schema type definition: '%s'") |
|
182 | - { |
|
183 | - // Parse regex |
|
184 | - $match = array(); |
|
185 | - if (preg_match('/^([a-z]+)/i', $definition, $match) === 1) { |
|
186 | - $format = strtolower($match[1]); |
|
187 | - } elseif (preg_match('/^(\[)(?:.*?)\]$/', $definition, $match) === 1) { |
|
188 | - $format = 'array'; |
|
189 | - } elseif (preg_match('/^(\{)(?:.*?)\}$/', $definition, $match) === 1) { |
|
190 | - $format = 'object'; |
|
191 | - } else { |
|
192 | - throw new Exception(sprintf($error, $definition)); |
|
193 | - } |
|
194 | - |
|
195 | - // Internal type if type known and not overwritten by definition |
|
196 | - if ($parent->getTypeRegistry()->has($format)) { |
|
197 | - $class = $parent->getTypeRegistry()->get($format); |
|
198 | - return new $class($parent, $definition); |
|
199 | - } |
|
200 | - |
|
201 | - if (isset(self::$classTypes[$format])) { |
|
202 | - $type = self::$classTypes[$format]; |
|
203 | - $class = "\\SwaggerGen\\Swagger\\Type\\{$type}Type"; |
|
204 | - return new $class($parent, $definition); |
|
205 | - } |
|
206 | - |
|
207 | - return new ReferenceObjectType($parent, $definition); |
|
208 | - } |
|
19 | + const REGEX_START = '/^'; |
|
20 | + const REGEX_FORMAT = '([a-z][a-z0-9]*)'; |
|
21 | + const REGEX_CONTENT = '(?:\((.*)\))?'; |
|
22 | + const REGEX_RANGE = '(?:([[<])(\\d*),(\\d*)([\\]>]))?'; |
|
23 | + const REGEX_DEFAULT = '(?:=(.+))?'; |
|
24 | + const REGEX_END = '$/i'; |
|
25 | + |
|
26 | + private static $classTypes = array( |
|
27 | + 'integer' => 'Integer', |
|
28 | + 'int' => 'Integer', |
|
29 | + 'int32' => 'Integer', |
|
30 | + 'int64' => 'Integer', |
|
31 | + 'long' => 'Integer', |
|
32 | + 'float' => 'Number', |
|
33 | + 'double' => 'Number', |
|
34 | + 'string' => 'String', |
|
35 | + 'uuid' => 'StringUuid', |
|
36 | + 'byte' => 'String', |
|
37 | + 'binary' => 'String', |
|
38 | + 'password' => 'String', |
|
39 | + 'enum' => 'String', |
|
40 | + 'boolean' => 'Boolean', |
|
41 | + 'bool' => 'Boolean', |
|
42 | + 'array' => 'Array', |
|
43 | + 'csv' => 'Array', |
|
44 | + 'ssv' => 'Array', |
|
45 | + 'tsv' => 'Array', |
|
46 | + 'pipes' => 'Array', |
|
47 | + 'date' => 'Date', |
|
48 | + 'datetime' => 'Date', |
|
49 | + 'date-time' => 'Date', |
|
50 | + 'object' => 'Object', |
|
51 | + 'refobject' => 'ReferenceObject', |
|
52 | + 'allof' => 'AllOf', |
|
53 | + ); |
|
54 | + |
|
55 | + private $example = null; |
|
56 | + |
|
57 | + /** |
|
58 | + * Swap values of two variables. |
|
59 | + * Used for sorting. |
|
60 | + * @param mixed $a |
|
61 | + * @param mixed $b |
|
62 | + */ |
|
63 | + protected static function swap(&$a, &$b) |
|
64 | + { |
|
65 | + $tmp = $a; |
|
66 | + $a = $b; |
|
67 | + $b = $tmp; |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * @param string $list |
|
72 | + * @return array |
|
73 | + */ |
|
74 | + protected static function parseList($list) |
|
75 | + { |
|
76 | + $ret = array(); |
|
77 | + while ($item = self::parseListItem($list)) { |
|
78 | + $ret[] = $item; |
|
79 | + } |
|
80 | + return $ret; |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * Extract an item from a comma-separated list of items. |
|
85 | + * |
|
86 | + * i.e. `a(x(x,x)),b(x)` returns `a(x(x,x))` and changes `$list` into `b(x)`. |
|
87 | + * Note: brace nesting is not checked, e.g. `a{b(})` is a valid list item. |
|
88 | + * |
|
89 | + * @param string $list the list to parse |
|
90 | + * @return string the extracted item |
|
91 | + */ |
|
92 | + protected static function parseListItem(&$list) |
|
93 | + { |
|
94 | + $item = ''; |
|
95 | + |
|
96 | + $depth = 0; |
|
97 | + $index = 0; |
|
98 | + while ($index < strlen($list)) { |
|
99 | + $c = $list[$index++]; |
|
100 | + |
|
101 | + if (str_contains('{([<', $c)) { |
|
102 | + ++$depth; |
|
103 | + } elseif (str_contains('})]>', $c)) { |
|
104 | + --$depth; |
|
105 | + } elseif ($c === ',' && $depth === 0) { |
|
106 | + break; |
|
107 | + } |
|
108 | + |
|
109 | + $item .= $c; |
|
110 | + } |
|
111 | + $list = substr($list, $index); |
|
112 | + |
|
113 | + return $item; |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * @param AbstractObject $parent |
|
118 | + * @param $definition |
|
119 | + * @constructor |
|
120 | + */ |
|
121 | + public function __construct(AbstractObject $parent, $definition) |
|
122 | + { |
|
123 | + parent::__construct($parent); |
|
124 | + |
|
125 | + $this->parseDefinition($definition); |
|
126 | + } |
|
127 | + |
|
128 | + abstract protected function parseDefinition($definition); |
|
129 | + |
|
130 | + /** |
|
131 | + * Overwrites default AbstractObject parser, since Types should not handle |
|
132 | + * extensions themselves. |
|
133 | + * |
|
134 | + * @param string $command |
|
135 | + * @param string $data |
|
136 | + * @return AbstractType|boolean |
|
137 | + * @throws Exception |
|
138 | + */ |
|
139 | + public function handleCommand($command, $data = null) |
|
140 | + { |
|
141 | + if (strtolower($command) === 'example') { |
|
142 | + if ($data === '') { |
|
143 | + throw new Exception("Missing content for type example"); |
|
144 | + } |
|
145 | + $json = preg_replace_callback('/([^{}:,]+)/', static function ($match) { |
|
146 | + json_decode($match[1]); |
|
147 | + return json_last_error() === JSON_ERROR_NONE ? $match[1] : json_encode($match[1]); |
|
148 | + }, trim($data)); |
|
149 | + $this->example = json_decode($json, true); |
|
150 | + |
|
151 | + // In case input contains special chars, the above preg_replace would fail |
|
152 | + // Input could be a well-formed json already though |
|
153 | + if ($this->example === null) { |
|
154 | + $this->example = json_decode($data, true); |
|
155 | + } |
|
156 | + // If all fails, use input as-is |
|
157 | + if ($this->example === null) { |
|
158 | + $this->example = $data; |
|
159 | + } |
|
160 | + |
|
161 | + return $this; |
|
162 | + } |
|
163 | + |
|
164 | + return false; |
|
165 | + } |
|
166 | + |
|
167 | + public function toArray(): array |
|
168 | + { |
|
169 | + return self::arrayFilterNull(array_merge(array( |
|
170 | + 'example' => $this->example, |
|
171 | + ), parent::toArray())); |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * @param AbstractObject $parent |
|
176 | + * @param string $definition |
|
177 | + * @param string $error |
|
178 | + * @return self |
|
179 | + * @throws Exception |
|
180 | + */ |
|
181 | + public static function typeFactory($parent, $definition, $error = "Unparseable schema type definition: '%s'") |
|
182 | + { |
|
183 | + // Parse regex |
|
184 | + $match = array(); |
|
185 | + if (preg_match('/^([a-z]+)/i', $definition, $match) === 1) { |
|
186 | + $format = strtolower($match[1]); |
|
187 | + } elseif (preg_match('/^(\[)(?:.*?)\]$/', $definition, $match) === 1) { |
|
188 | + $format = 'array'; |
|
189 | + } elseif (preg_match('/^(\{)(?:.*?)\}$/', $definition, $match) === 1) { |
|
190 | + $format = 'object'; |
|
191 | + } else { |
|
192 | + throw new Exception(sprintf($error, $definition)); |
|
193 | + } |
|
194 | + |
|
195 | + // Internal type if type known and not overwritten by definition |
|
196 | + if ($parent->getTypeRegistry()->has($format)) { |
|
197 | + $class = $parent->getTypeRegistry()->get($format); |
|
198 | + return new $class($parent, $definition); |
|
199 | + } |
|
200 | + |
|
201 | + if (isset(self::$classTypes[$format])) { |
|
202 | + $type = self::$classTypes[$format]; |
|
203 | + $class = "\\SwaggerGen\\Swagger\\Type\\{$type}Type"; |
|
204 | + return new $class($parent, $definition); |
|
205 | + } |
|
206 | + |
|
207 | + return new ReferenceObjectType($parent, $definition); |
|
208 | + } |
|
209 | 209 | |
210 | 210 | } |
@@ -142,7 +142,7 @@ |
||
142 | 142 | if ($data === '') { |
143 | 143 | throw new Exception("Missing content for type example"); |
144 | 144 | } |
145 | - $json = preg_replace_callback('/([^{}:,]+)/', static function ($match) { |
|
145 | + $json = preg_replace_callback('/([^{}:,]+)/', static function($match) { |
|
146 | 146 | json_decode($match[1]); |
147 | 147 | return json_last_error() === JSON_ERROR_NONE ? $match[1] : json_encode($match[1]); |
148 | 148 | }, trim($data)); |
@@ -200,7 +200,7 @@ |
||
200 | 200 | |
201 | 201 | if (isset(self::$classTypes[$format])) { |
202 | 202 | $type = self::$classTypes[$format]; |
203 | - $class = "\\SwaggerGen\\Swagger\\Type\\{$type}Type"; |
|
203 | + $class = "\\SwaggerGen\\Swagger\\Type\\{$type}type"; |
|
204 | 204 | return new $class($parent, $definition); |
205 | 205 | } |
206 | 206 |
@@ -15,157 +15,157 @@ |
||
15 | 15 | class NumberType extends AbstractType |
16 | 16 | { |
17 | 17 | |
18 | - /** @noinspection PhpRegExpUnsupportedModifierInspection */ |
|
19 | - const REGEX_RANGE = '(?:([[<])(-?(?:\\d*\\.?\\d+|\\d+\\.\\d*))?,(-?(?:\\d*\\.?\\d+|\\d+\\.\\d*))?([\\]>]))?'; |
|
20 | - /** @noinspection PhpRegExpUnsupportedModifierInspection */ |
|
21 | - const REGEX_DEFAULT = '(?:=(-?(?:\\d*\\.?\\d+|\\d+\\.\\d*)))?'; |
|
22 | - |
|
23 | - private static $formats = array( |
|
24 | - 'float' => 'float', |
|
25 | - 'double' => 'double', |
|
26 | - ); |
|
27 | - private $format; |
|
28 | - private $default = null; |
|
29 | - private $maximum = null; |
|
30 | - private $exclusiveMaximum = null; |
|
31 | - private $minimum = null; |
|
32 | - private $exclusiveMinimum = null; |
|
33 | - private $enum = array(); |
|
34 | - private $multipleOf = null; |
|
35 | - |
|
36 | - /** |
|
37 | - * @throws Exception |
|
38 | - */ |
|
39 | - protected function parseDefinition($definition) |
|
40 | - { |
|
41 | - $match = array(); |
|
42 | - if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
43 | - throw new Exception("Unparseable number definition: '{$definition}'"); |
|
44 | - } |
|
45 | - |
|
46 | - $this->parseFormat($definition, $match); |
|
47 | - $this->parseRange($definition, $match); |
|
48 | - $this->parseDefault($definition, $match); |
|
49 | - } |
|
50 | - |
|
51 | - /** |
|
52 | - * @param string[] $match |
|
53 | - * @throws Exception |
|
54 | - */ |
|
55 | - private function parseFormat($definition, $match) |
|
56 | - { |
|
57 | - if (!isset(self::$formats[strtolower($match[1])])) { |
|
58 | - throw new Exception("Not a number: '{$definition}'"); |
|
59 | - } |
|
60 | - $this->format = self::$formats[strtolower($match[1])]; |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * @param string[] $match |
|
65 | - * @throws Exception |
|
66 | - */ |
|
67 | - private function parseRange($definition, $match) |
|
68 | - { |
|
69 | - if (!empty($match[2])) { |
|
70 | - if ($match[3] === '' && $match[4] === '') { |
|
71 | - throw new Exception("Empty number range: '{$definition}'"); |
|
72 | - } |
|
73 | - |
|
74 | - $this->exclusiveMinimum = $match[2] == '<'; |
|
75 | - $this->minimum = $match[3] === '' ? null : (float)$match[3]; |
|
76 | - $this->maximum = $match[4] === '' ? null : (float)$match[4]; |
|
77 | - $this->exclusiveMaximum = isset($match[5]) ? ($match[5] == '>') : null; |
|
78 | - if ($this->minimum && $this->maximum && $this->minimum > $this->maximum) { |
|
79 | - self::swap($this->minimum, $this->maximum); |
|
80 | - self::swap($this->exclusiveMinimum, $this->exclusiveMaximum); |
|
81 | - } |
|
82 | - } |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * @param string[] $match |
|
87 | - * @throws Exception |
|
88 | - */ |
|
89 | - private function parseDefault($definition, $match) |
|
90 | - { |
|
91 | - $this->default = isset($match[6]) && $match[6] !== '' ? $this->validateDefault($match[6]) : null; |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * @param string $command The comment command |
|
96 | - * @param string $data Any data added after the command |
|
97 | - * @return AbstractType|boolean |
|
98 | - * @throws Exception |
|
99 | - * @throws Exception |
|
100 | - * @throws Exception |
|
101 | - */ |
|
102 | - public function handleCommand($command, $data = null) |
|
103 | - { |
|
104 | - switch (strtolower($command)) { |
|
105 | - case 'default': |
|
106 | - $this->default = $this->validateDefault($data); |
|
107 | - return $this; |
|
108 | - |
|
109 | - case 'enum': |
|
110 | - $words = self::wordSplit($data); |
|
111 | - foreach ($words as &$word) { |
|
112 | - $word = $this->validateDefault($word); |
|
113 | - } |
|
114 | - unset($word); |
|
115 | - $this->enum = array_merge($this->enum, $words); |
|
116 | - return $this; |
|
117 | - |
|
118 | - case 'step': |
|
119 | - if (($step = (float)$data) > 0) { |
|
120 | - $this->multipleOf = $step; |
|
121 | - } |
|
122 | - return $this; |
|
123 | - } |
|
124 | - |
|
125 | - return parent::handleCommand($command, $data); |
|
126 | - } |
|
127 | - |
|
128 | - public function toArray(): array |
|
129 | - { |
|
130 | - return self::arrayFilterNull(array_merge(array( |
|
131 | - 'type' => 'number', |
|
132 | - 'format' => $this->format, |
|
133 | - 'default' => $this->default, |
|
134 | - 'minimum' => $this->minimum, |
|
135 | - 'exclusiveMinimum' => ($this->exclusiveMinimum && !is_null($this->minimum)) ? true : null, |
|
136 | - 'maximum' => $this->maximum, |
|
137 | - 'exclusiveMaximum' => ($this->exclusiveMaximum && !is_null($this->maximum)) ? true : null, |
|
138 | - 'enum' => $this->enum, |
|
139 | - 'multipleOf' => $this->multipleOf, |
|
140 | - ), parent::toArray())); |
|
141 | - } |
|
142 | - |
|
143 | - public function __toString() |
|
144 | - { |
|
145 | - return __CLASS__; |
|
146 | - } |
|
147 | - |
|
148 | - /** |
|
149 | - * @throws Exception |
|
150 | - */ |
|
151 | - private function validateDefault($value) |
|
152 | - { |
|
153 | - if (preg_match('~^-?(?:\\d*\\.?\\d+|\\d+\\.\\d*)$~', $value) !== 1) { |
|
154 | - throw new Exception("Invalid number default: '{$value}'"); |
|
155 | - } |
|
156 | - |
|
157 | - if ($this->maximum) { |
|
158 | - if (($value > $this->maximum) || ($this->exclusiveMaximum && $value == $this->maximum)) { |
|
159 | - throw new Exception("Default number beyond maximum: '{$value}'"); |
|
160 | - } |
|
161 | - } |
|
162 | - if ($this->minimum) { |
|
163 | - if (($value < $this->minimum) || ($this->exclusiveMinimum && $value == $this->minimum)) { |
|
164 | - throw new Exception("Default number beyond minimum: '{$value}'"); |
|
165 | - } |
|
166 | - } |
|
167 | - |
|
168 | - return (float)$value; |
|
169 | - } |
|
18 | + /** @noinspection PhpRegExpUnsupportedModifierInspection */ |
|
19 | + const REGEX_RANGE = '(?:([[<])(-?(?:\\d*\\.?\\d+|\\d+\\.\\d*))?,(-?(?:\\d*\\.?\\d+|\\d+\\.\\d*))?([\\]>]))?'; |
|
20 | + /** @noinspection PhpRegExpUnsupportedModifierInspection */ |
|
21 | + const REGEX_DEFAULT = '(?:=(-?(?:\\d*\\.?\\d+|\\d+\\.\\d*)))?'; |
|
22 | + |
|
23 | + private static $formats = array( |
|
24 | + 'float' => 'float', |
|
25 | + 'double' => 'double', |
|
26 | + ); |
|
27 | + private $format; |
|
28 | + private $default = null; |
|
29 | + private $maximum = null; |
|
30 | + private $exclusiveMaximum = null; |
|
31 | + private $minimum = null; |
|
32 | + private $exclusiveMinimum = null; |
|
33 | + private $enum = array(); |
|
34 | + private $multipleOf = null; |
|
35 | + |
|
36 | + /** |
|
37 | + * @throws Exception |
|
38 | + */ |
|
39 | + protected function parseDefinition($definition) |
|
40 | + { |
|
41 | + $match = array(); |
|
42 | + if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
43 | + throw new Exception("Unparseable number definition: '{$definition}'"); |
|
44 | + } |
|
45 | + |
|
46 | + $this->parseFormat($definition, $match); |
|
47 | + $this->parseRange($definition, $match); |
|
48 | + $this->parseDefault($definition, $match); |
|
49 | + } |
|
50 | + |
|
51 | + /** |
|
52 | + * @param string[] $match |
|
53 | + * @throws Exception |
|
54 | + */ |
|
55 | + private function parseFormat($definition, $match) |
|
56 | + { |
|
57 | + if (!isset(self::$formats[strtolower($match[1])])) { |
|
58 | + throw new Exception("Not a number: '{$definition}'"); |
|
59 | + } |
|
60 | + $this->format = self::$formats[strtolower($match[1])]; |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * @param string[] $match |
|
65 | + * @throws Exception |
|
66 | + */ |
|
67 | + private function parseRange($definition, $match) |
|
68 | + { |
|
69 | + if (!empty($match[2])) { |
|
70 | + if ($match[3] === '' && $match[4] === '') { |
|
71 | + throw new Exception("Empty number range: '{$definition}'"); |
|
72 | + } |
|
73 | + |
|
74 | + $this->exclusiveMinimum = $match[2] == '<'; |
|
75 | + $this->minimum = $match[3] === '' ? null : (float)$match[3]; |
|
76 | + $this->maximum = $match[4] === '' ? null : (float)$match[4]; |
|
77 | + $this->exclusiveMaximum = isset($match[5]) ? ($match[5] == '>') : null; |
|
78 | + if ($this->minimum && $this->maximum && $this->minimum > $this->maximum) { |
|
79 | + self::swap($this->minimum, $this->maximum); |
|
80 | + self::swap($this->exclusiveMinimum, $this->exclusiveMaximum); |
|
81 | + } |
|
82 | + } |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * @param string[] $match |
|
87 | + * @throws Exception |
|
88 | + */ |
|
89 | + private function parseDefault($definition, $match) |
|
90 | + { |
|
91 | + $this->default = isset($match[6]) && $match[6] !== '' ? $this->validateDefault($match[6]) : null; |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * @param string $command The comment command |
|
96 | + * @param string $data Any data added after the command |
|
97 | + * @return AbstractType|boolean |
|
98 | + * @throws Exception |
|
99 | + * @throws Exception |
|
100 | + * @throws Exception |
|
101 | + */ |
|
102 | + public function handleCommand($command, $data = null) |
|
103 | + { |
|
104 | + switch (strtolower($command)) { |
|
105 | + case 'default': |
|
106 | + $this->default = $this->validateDefault($data); |
|
107 | + return $this; |
|
108 | + |
|
109 | + case 'enum': |
|
110 | + $words = self::wordSplit($data); |
|
111 | + foreach ($words as &$word) { |
|
112 | + $word = $this->validateDefault($word); |
|
113 | + } |
|
114 | + unset($word); |
|
115 | + $this->enum = array_merge($this->enum, $words); |
|
116 | + return $this; |
|
117 | + |
|
118 | + case 'step': |
|
119 | + if (($step = (float)$data) > 0) { |
|
120 | + $this->multipleOf = $step; |
|
121 | + } |
|
122 | + return $this; |
|
123 | + } |
|
124 | + |
|
125 | + return parent::handleCommand($command, $data); |
|
126 | + } |
|
127 | + |
|
128 | + public function toArray(): array |
|
129 | + { |
|
130 | + return self::arrayFilterNull(array_merge(array( |
|
131 | + 'type' => 'number', |
|
132 | + 'format' => $this->format, |
|
133 | + 'default' => $this->default, |
|
134 | + 'minimum' => $this->minimum, |
|
135 | + 'exclusiveMinimum' => ($this->exclusiveMinimum && !is_null($this->minimum)) ? true : null, |
|
136 | + 'maximum' => $this->maximum, |
|
137 | + 'exclusiveMaximum' => ($this->exclusiveMaximum && !is_null($this->maximum)) ? true : null, |
|
138 | + 'enum' => $this->enum, |
|
139 | + 'multipleOf' => $this->multipleOf, |
|
140 | + ), parent::toArray())); |
|
141 | + } |
|
142 | + |
|
143 | + public function __toString() |
|
144 | + { |
|
145 | + return __CLASS__; |
|
146 | + } |
|
147 | + |
|
148 | + /** |
|
149 | + * @throws Exception |
|
150 | + */ |
|
151 | + private function validateDefault($value) |
|
152 | + { |
|
153 | + if (preg_match('~^-?(?:\\d*\\.?\\d+|\\d+\\.\\d*)$~', $value) !== 1) { |
|
154 | + throw new Exception("Invalid number default: '{$value}'"); |
|
155 | + } |
|
156 | + |
|
157 | + if ($this->maximum) { |
|
158 | + if (($value > $this->maximum) || ($this->exclusiveMaximum && $value == $this->maximum)) { |
|
159 | + throw new Exception("Default number beyond maximum: '{$value}'"); |
|
160 | + } |
|
161 | + } |
|
162 | + if ($this->minimum) { |
|
163 | + if (($value < $this->minimum) || ($this->exclusiveMinimum && $value == $this->minimum)) { |
|
164 | + throw new Exception("Default number beyond minimum: '{$value}'"); |
|
165 | + } |
|
166 | + } |
|
167 | + |
|
168 | + return (float)$value; |
|
169 | + } |
|
170 | 170 | |
171 | 171 | } |
@@ -72,8 +72,8 @@ discard block |
||
72 | 72 | } |
73 | 73 | |
74 | 74 | $this->exclusiveMinimum = $match[2] == '<'; |
75 | - $this->minimum = $match[3] === '' ? null : (float)$match[3]; |
|
76 | - $this->maximum = $match[4] === '' ? null : (float)$match[4]; |
|
75 | + $this->minimum = $match[3] === '' ? null : (float) $match[3]; |
|
76 | + $this->maximum = $match[4] === '' ? null : (float) $match[4]; |
|
77 | 77 | $this->exclusiveMaximum = isset($match[5]) ? ($match[5] == '>') : null; |
78 | 78 | if ($this->minimum && $this->maximum && $this->minimum > $this->maximum) { |
79 | 79 | self::swap($this->minimum, $this->maximum); |
@@ -116,7 +116,7 @@ discard block |
||
116 | 116 | return $this; |
117 | 117 | |
118 | 118 | case 'step': |
119 | - if (($step = (float)$data) > 0) { |
|
119 | + if (($step = (float) $data) > 0) { |
|
120 | 120 | $this->multipleOf = $step; |
121 | 121 | } |
122 | 122 | return $this; |
@@ -165,7 +165,7 @@ discard block |
||
165 | 165 | } |
166 | 166 | } |
167 | 167 | |
168 | - return (float)$value; |
|
168 | + return (float) $value; |
|
169 | 169 | } |
170 | 170 | |
171 | 171 | } |
@@ -16,82 +16,82 @@ |
||
16 | 16 | class Property extends AbstractObject |
17 | 17 | { |
18 | 18 | |
19 | - /** |
|
20 | - * Description of this property |
|
21 | - * @var string |
|
22 | - */ |
|
23 | - private $description; |
|
19 | + /** |
|
20 | + * Description of this property |
|
21 | + * @var string |
|
22 | + */ |
|
23 | + private $description; |
|
24 | 24 | |
25 | - /** |
|
26 | - * Whether property is read only |
|
27 | - * @var bool |
|
28 | - */ |
|
29 | - private $readOnly; |
|
25 | + /** |
|
26 | + * Whether property is read only |
|
27 | + * @var bool |
|
28 | + */ |
|
29 | + private $readOnly; |
|
30 | 30 | |
31 | - /** |
|
32 | - * Type definition of this property |
|
33 | - * @var AbstractType |
|
34 | - */ |
|
35 | - private $Type; |
|
31 | + /** |
|
32 | + * Type definition of this property |
|
33 | + * @var AbstractType |
|
34 | + */ |
|
35 | + private $Type; |
|
36 | 36 | |
37 | - /** |
|
38 | - * Create a new property |
|
39 | - * @param AbstractObject $parent |
|
40 | - * @param string $definition Either a built-in type or a definition name |
|
41 | - * @param string $description description of the property |
|
42 | - * @param bool $readOnly Whether the property is read only |
|
43 | - * @throws Exception |
|
44 | - */ |
|
45 | - public function __construct(AbstractObject $parent, $definition, $description = null, $readOnly = null) |
|
46 | - { |
|
47 | - parent::__construct($parent); |
|
48 | - $this->Type = AbstractType::typeFactory($this, $definition, "Not a property: '%s'"); |
|
49 | - $this->description = $description; |
|
50 | - $this->readOnly = $readOnly; |
|
51 | - } |
|
37 | + /** |
|
38 | + * Create a new property |
|
39 | + * @param AbstractObject $parent |
|
40 | + * @param string $definition Either a built-in type or a definition name |
|
41 | + * @param string $description description of the property |
|
42 | + * @param bool $readOnly Whether the property is read only |
|
43 | + * @throws Exception |
|
44 | + */ |
|
45 | + public function __construct(AbstractObject $parent, $definition, $description = null, $readOnly = null) |
|
46 | + { |
|
47 | + parent::__construct($parent); |
|
48 | + $this->Type = AbstractType::typeFactory($this, $definition, "Not a property: '%s'"); |
|
49 | + $this->description = $description; |
|
50 | + $this->readOnly = $readOnly; |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * @param string $command The comment command |
|
55 | - * @param string $data Any data added after the command |
|
56 | - * @return self|boolean |
|
57 | - * @throws Exception |
|
58 | - */ |
|
59 | - public function handleCommand($command, $data = null) |
|
60 | - { |
|
61 | - // Pass through to Type |
|
62 | - if ($this->Type && $this->Type->handleCommand($command, $data)) { |
|
63 | - return $this; |
|
64 | - } |
|
53 | + /** |
|
54 | + * @param string $command The comment command |
|
55 | + * @param string $data Any data added after the command |
|
56 | + * @return self|boolean |
|
57 | + * @throws Exception |
|
58 | + */ |
|
59 | + public function handleCommand($command, $data = null) |
|
60 | + { |
|
61 | + // Pass through to Type |
|
62 | + if ($this->Type && $this->Type->handleCommand($command, $data)) { |
|
63 | + return $this; |
|
64 | + } |
|
65 | 65 | |
66 | - return parent::handleCommand($command, $data); |
|
67 | - } |
|
66 | + return parent::handleCommand($command, $data); |
|
67 | + } |
|
68 | 68 | |
69 | - public function toArray(): array |
|
70 | - { |
|
71 | - // Reference + readonly/description result in allOf construct |
|
72 | - // as it's semantically the same and that's what swagger tools |
|
73 | - // like swagger-ui can understand |
|
74 | - $requiresWrap = |
|
75 | - $this->Type instanceof ReferenceObjectType |
|
76 | - && (!empty($this->description) || !is_null($this->readOnly)); |
|
69 | + public function toArray(): array |
|
70 | + { |
|
71 | + // Reference + readonly/description result in allOf construct |
|
72 | + // as it's semantically the same and that's what swagger tools |
|
73 | + // like swagger-ui can understand |
|
74 | + $requiresWrap = |
|
75 | + $this->Type instanceof ReferenceObjectType |
|
76 | + && (!empty($this->description) || !is_null($this->readOnly)); |
|
77 | 77 | |
78 | - $valueType = $this->Type->toArray(); |
|
78 | + $valueType = $this->Type->toArray(); |
|
79 | 79 | |
80 | - if ($requiresWrap) { |
|
81 | - $valueType = array( |
|
82 | - 'allOf' => array($valueType), |
|
83 | - ); |
|
84 | - } |
|
80 | + if ($requiresWrap) { |
|
81 | + $valueType = array( |
|
82 | + 'allOf' => array($valueType), |
|
83 | + ); |
|
84 | + } |
|
85 | 85 | |
86 | - return self::arrayFilterNull(array_merge($valueType, array( |
|
87 | - 'description' => empty($this->description) ? null : $this->description, |
|
88 | - 'readOnly' => $this->readOnly |
|
89 | - ), parent::toArray())); |
|
90 | - } |
|
86 | + return self::arrayFilterNull(array_merge($valueType, array( |
|
87 | + 'description' => empty($this->description) ? null : $this->description, |
|
88 | + 'readOnly' => $this->readOnly |
|
89 | + ), parent::toArray())); |
|
90 | + } |
|
91 | 91 | |
92 | - public function __toString() |
|
93 | - { |
|
94 | - return __CLASS__; |
|
95 | - } |
|
92 | + public function __toString() |
|
93 | + { |
|
94 | + return __CLASS__; |
|
95 | + } |
|
96 | 96 | |
97 | 97 | } |
@@ -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 = null; |
|
32 | - protected $default = null; |
|
33 | - protected $maxLength = null; |
|
34 | - protected $minLength = null; |
|
35 | - protected $enum = array(); |
|
36 | - |
|
37 | - /** |
|
38 | - * @throws Exception |
|
39 | - */ |
|
40 | - protected function parseDefinition($definition) |
|
41 | - { |
|
42 | - $definition = self::trim($definition); |
|
43 | - |
|
44 | - $match = array(); |
|
45 | - if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_CONTENT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
46 | - throw new Exception("Unparseable string definition: '{$definition}'"); |
|
47 | - } |
|
48 | - |
|
49 | - $this->parseFormat($definition, $match); |
|
50 | - $this->parseContent($definition, $match); |
|
51 | - $this->parseRange($definition, $match); |
|
52 | - $this->parseDefault($definition, $match); |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * @param string $definition |
|
57 | - * @param string[] $match |
|
58 | - * @throws Exception |
|
59 | - */ |
|
60 | - private function parseFormat($definition, $match) |
|
61 | - { |
|
62 | - $type = strtolower($match[1]); |
|
63 | - if (!isset(self::$formats[$type])) { |
|
64 | - throw new Exception("Not a string: '{$definition}'"); |
|
65 | - } |
|
66 | - $this->format = self::$formats[$type]; |
|
67 | - } |
|
68 | - |
|
69 | - /** |
|
70 | - * @param string $definition |
|
71 | - * @param string[] $match |
|
72 | - */ |
|
73 | - private function parseContent($definition, $match) |
|
74 | - { |
|
75 | - if (strtolower($match[1]) === 'enum') { |
|
76 | - $this->enum = explode(',', $match[2]); |
|
77 | - } else { |
|
78 | - $this->pattern = empty($match[2]) ? null : $match[2]; |
|
79 | - } |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * @param string $definition |
|
84 | - * @param string[] $match |
|
85 | - * @throws Exception |
|
86 | - * @throws Exception |
|
87 | - */ |
|
88 | - private function parseRange($definition, $match) |
|
89 | - { |
|
90 | - |
|
91 | - if (!empty($match[3])) { |
|
92 | - if ($match[1] === 'enum') { |
|
93 | - throw new Exception("Range not allowed in enumeration definition: '{$definition}'"); |
|
94 | - } |
|
95 | - if ($match[4] === '' && $match[5] === '') { |
|
96 | - throw new Exception("Empty string range: '{$definition}'"); |
|
97 | - } |
|
98 | - $exclusiveMinimum = $match[3] == '<'; |
|
99 | - $this->minLength = $match[4] === '' ? null : $match[4]; |
|
100 | - $this->maxLength = $match[5] === '' ? null : $match[5]; |
|
101 | - $exclusiveMaximum = isset($match[6]) ? ($match[6] == '>') : null; |
|
102 | - if ($this->minLength !== null && $this->maxLength !== null && $this->minLength > $this->maxLength) { |
|
103 | - self::swap($this->minLength, $this->maxLength); |
|
104 | - self::swap($exclusiveMinimum, $exclusiveMaximum); |
|
105 | - } |
|
106 | - $this->minLength = $this->minLength === null ? null : max(0, $exclusiveMinimum ? $this->minLength + 1 : $this->minLength); |
|
107 | - $this->maxLength = $this->maxLength === null ? null : max(0, $exclusiveMaximum ? $this->maxLength - 1 : $this->maxLength); |
|
108 | - } |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * @param string $definition |
|
113 | - * @param string[] $match |
|
114 | - * @throws Exception |
|
115 | - */ |
|
116 | - private function parseDefault($definition, $match) |
|
117 | - { |
|
118 | - $this->default = isset($match[7]) && $match[7] !== '' ? $this->validateDefault($match[7]) : null; |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * @param string $command The comment command |
|
123 | - * @param string $data Any data added after the command |
|
124 | - * @return AbstractType|boolean |
|
125 | - * @throws Exception |
|
126 | - * @throws Exception |
|
127 | - * @throws Exception |
|
128 | - */ |
|
129 | - public function handleCommand($command, $data = null) |
|
130 | - { |
|
131 | - switch (strtolower($command)) { |
|
132 | - case 'default': |
|
133 | - $this->default = $this->validateDefault($data); |
|
134 | - return $this; |
|
135 | - |
|
136 | - case 'pattern': |
|
137 | - $this->pattern = $data; |
|
138 | - return $this; |
|
139 | - |
|
140 | - case 'enum': |
|
141 | - if ($this->minLength !== null || $this->maxLength !== null) { |
|
142 | - throw new Exception("Enumeration not allowed in ranged string: '{$data}'"); |
|
143 | - } |
|
144 | - $words = self::wordSplit($data); |
|
145 | - $this->enum = is_array($this->enum) ? array_merge($this->enum, $words) : $words; |
|
146 | - return $this; |
|
147 | - } |
|
148 | - |
|
149 | - return parent::handleCommand($command, $data); |
|
150 | - } |
|
151 | - |
|
152 | - public function toArray(): array |
|
153 | - { |
|
154 | - return self::arrayFilterNull(array_merge(array( |
|
155 | - 'type' => 'string', |
|
156 | - 'format' => empty($this->format) ? null : $this->format, |
|
157 | - 'pattern' => $this->pattern, |
|
158 | - 'default' => $this->default, |
|
159 | - 'minLength' => $this->minLength ? (int)$this->minLength : null, |
|
160 | - 'maxLength' => $this->maxLength ? (int)$this->maxLength : null, |
|
161 | - 'enum' => $this->enum, |
|
162 | - ), parent::toArray())); |
|
163 | - } |
|
164 | - |
|
165 | - /** |
|
166 | - * Validate a default string value, depending on subtype |
|
167 | - * |
|
168 | - * @param string $value the value to validate |
|
169 | - * @return string the value after validation (might become trimmed) |
|
170 | - * @throws Exception |
|
171 | - */ |
|
172 | - protected function validateDefault($value) |
|
173 | - { |
|
174 | - if (empty($value)) { |
|
175 | - if ($this->format) { |
|
176 | - $type = $this->format; |
|
177 | - } else { |
|
178 | - $type = $this->enum ? 'enum' : 'string'; |
|
179 | - } |
|
180 | - throw new Exception("Empty {$type} default"); |
|
181 | - } |
|
182 | - |
|
183 | - if (!empty($this->enum) && !in_array($value, $this->enum)) { |
|
184 | - throw new Exception("Invalid enum default: '{$value}'"); |
|
185 | - } |
|
186 | - |
|
187 | - if ($this->maxLength !== null && mb_strlen($value) > $this->maxLength) { |
|
188 | - if ($this->format) { |
|
189 | - $type = $this->format; |
|
190 | - } else { |
|
191 | - $type = $this->enum ? 'enum' : 'string'; |
|
192 | - } |
|
193 | - throw new Exception("Default {$type} length beyond maximum: '{$value}'"); |
|
194 | - } |
|
195 | - |
|
196 | - if ($this->minLength !== null && mb_strlen($value) < $this->minLength) { |
|
197 | - if ($this->format) { |
|
198 | - $type = $this->format; |
|
199 | - } else { |
|
200 | - $type = $this->enum ? 'enum' : 'string'; |
|
201 | - } |
|
202 | - throw new Exception("Default {$type} length beyond minimum: '{$value}'"); |
|
203 | - } |
|
204 | - |
|
205 | - return $value; |
|
206 | - } |
|
207 | - |
|
208 | - public function __toString() |
|
209 | - { |
|
210 | - return __CLASS__; |
|
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 = null; |
|
32 | + protected $default = null; |
|
33 | + protected $maxLength = null; |
|
34 | + protected $minLength = null; |
|
35 | + protected $enum = array(); |
|
36 | + |
|
37 | + /** |
|
38 | + * @throws Exception |
|
39 | + */ |
|
40 | + protected function parseDefinition($definition) |
|
41 | + { |
|
42 | + $definition = self::trim($definition); |
|
43 | + |
|
44 | + $match = array(); |
|
45 | + if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_CONTENT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
46 | + throw new Exception("Unparseable string definition: '{$definition}'"); |
|
47 | + } |
|
48 | + |
|
49 | + $this->parseFormat($definition, $match); |
|
50 | + $this->parseContent($definition, $match); |
|
51 | + $this->parseRange($definition, $match); |
|
52 | + $this->parseDefault($definition, $match); |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * @param string $definition |
|
57 | + * @param string[] $match |
|
58 | + * @throws Exception |
|
59 | + */ |
|
60 | + private function parseFormat($definition, $match) |
|
61 | + { |
|
62 | + $type = strtolower($match[1]); |
|
63 | + if (!isset(self::$formats[$type])) { |
|
64 | + throw new Exception("Not a string: '{$definition}'"); |
|
65 | + } |
|
66 | + $this->format = self::$formats[$type]; |
|
67 | + } |
|
68 | + |
|
69 | + /** |
|
70 | + * @param string $definition |
|
71 | + * @param string[] $match |
|
72 | + */ |
|
73 | + private function parseContent($definition, $match) |
|
74 | + { |
|
75 | + if (strtolower($match[1]) === 'enum') { |
|
76 | + $this->enum = explode(',', $match[2]); |
|
77 | + } else { |
|
78 | + $this->pattern = empty($match[2]) ? null : $match[2]; |
|
79 | + } |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * @param string $definition |
|
84 | + * @param string[] $match |
|
85 | + * @throws Exception |
|
86 | + * @throws Exception |
|
87 | + */ |
|
88 | + private function parseRange($definition, $match) |
|
89 | + { |
|
90 | + |
|
91 | + if (!empty($match[3])) { |
|
92 | + if ($match[1] === 'enum') { |
|
93 | + throw new Exception("Range not allowed in enumeration definition: '{$definition}'"); |
|
94 | + } |
|
95 | + if ($match[4] === '' && $match[5] === '') { |
|
96 | + throw new Exception("Empty string range: '{$definition}'"); |
|
97 | + } |
|
98 | + $exclusiveMinimum = $match[3] == '<'; |
|
99 | + $this->minLength = $match[4] === '' ? null : $match[4]; |
|
100 | + $this->maxLength = $match[5] === '' ? null : $match[5]; |
|
101 | + $exclusiveMaximum = isset($match[6]) ? ($match[6] == '>') : null; |
|
102 | + if ($this->minLength !== null && $this->maxLength !== null && $this->minLength > $this->maxLength) { |
|
103 | + self::swap($this->minLength, $this->maxLength); |
|
104 | + self::swap($exclusiveMinimum, $exclusiveMaximum); |
|
105 | + } |
|
106 | + $this->minLength = $this->minLength === null ? null : max(0, $exclusiveMinimum ? $this->minLength + 1 : $this->minLength); |
|
107 | + $this->maxLength = $this->maxLength === null ? null : max(0, $exclusiveMaximum ? $this->maxLength - 1 : $this->maxLength); |
|
108 | + } |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * @param string $definition |
|
113 | + * @param string[] $match |
|
114 | + * @throws Exception |
|
115 | + */ |
|
116 | + private function parseDefault($definition, $match) |
|
117 | + { |
|
118 | + $this->default = isset($match[7]) && $match[7] !== '' ? $this->validateDefault($match[7]) : null; |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * @param string $command The comment command |
|
123 | + * @param string $data Any data added after the command |
|
124 | + * @return AbstractType|boolean |
|
125 | + * @throws Exception |
|
126 | + * @throws Exception |
|
127 | + * @throws Exception |
|
128 | + */ |
|
129 | + public function handleCommand($command, $data = null) |
|
130 | + { |
|
131 | + switch (strtolower($command)) { |
|
132 | + case 'default': |
|
133 | + $this->default = $this->validateDefault($data); |
|
134 | + return $this; |
|
135 | + |
|
136 | + case 'pattern': |
|
137 | + $this->pattern = $data; |
|
138 | + return $this; |
|
139 | + |
|
140 | + case 'enum': |
|
141 | + if ($this->minLength !== null || $this->maxLength !== null) { |
|
142 | + throw new Exception("Enumeration not allowed in ranged string: '{$data}'"); |
|
143 | + } |
|
144 | + $words = self::wordSplit($data); |
|
145 | + $this->enum = is_array($this->enum) ? array_merge($this->enum, $words) : $words; |
|
146 | + return $this; |
|
147 | + } |
|
148 | + |
|
149 | + return parent::handleCommand($command, $data); |
|
150 | + } |
|
151 | + |
|
152 | + public function toArray(): array |
|
153 | + { |
|
154 | + return self::arrayFilterNull(array_merge(array( |
|
155 | + 'type' => 'string', |
|
156 | + 'format' => empty($this->format) ? null : $this->format, |
|
157 | + 'pattern' => $this->pattern, |
|
158 | + 'default' => $this->default, |
|
159 | + 'minLength' => $this->minLength ? (int)$this->minLength : null, |
|
160 | + 'maxLength' => $this->maxLength ? (int)$this->maxLength : null, |
|
161 | + 'enum' => $this->enum, |
|
162 | + ), parent::toArray())); |
|
163 | + } |
|
164 | + |
|
165 | + /** |
|
166 | + * Validate a default string value, depending on subtype |
|
167 | + * |
|
168 | + * @param string $value the value to validate |
|
169 | + * @return string the value after validation (might become trimmed) |
|
170 | + * @throws Exception |
|
171 | + */ |
|
172 | + protected function validateDefault($value) |
|
173 | + { |
|
174 | + if (empty($value)) { |
|
175 | + if ($this->format) { |
|
176 | + $type = $this->format; |
|
177 | + } else { |
|
178 | + $type = $this->enum ? 'enum' : 'string'; |
|
179 | + } |
|
180 | + throw new Exception("Empty {$type} default"); |
|
181 | + } |
|
182 | + |
|
183 | + if (!empty($this->enum) && !in_array($value, $this->enum)) { |
|
184 | + throw new Exception("Invalid enum default: '{$value}'"); |
|
185 | + } |
|
186 | + |
|
187 | + if ($this->maxLength !== null && mb_strlen($value) > $this->maxLength) { |
|
188 | + if ($this->format) { |
|
189 | + $type = $this->format; |
|
190 | + } else { |
|
191 | + $type = $this->enum ? 'enum' : 'string'; |
|
192 | + } |
|
193 | + throw new Exception("Default {$type} length beyond maximum: '{$value}'"); |
|
194 | + } |
|
195 | + |
|
196 | + if ($this->minLength !== null && mb_strlen($value) < $this->minLength) { |
|
197 | + if ($this->format) { |
|
198 | + $type = $this->format; |
|
199 | + } else { |
|
200 | + $type = $this->enum ? 'enum' : 'string'; |
|
201 | + } |
|
202 | + throw new Exception("Default {$type} length beyond minimum: '{$value}'"); |
|
203 | + } |
|
204 | + |
|
205 | + return $value; |
|
206 | + } |
|
207 | + |
|
208 | + public function __toString() |
|
209 | + { |
|
210 | + return __CLASS__; |
|
211 | + } |
|
212 | 212 | |
213 | 213 | } |
@@ -156,8 +156,8 @@ |
||
156 | 156 | 'format' => empty($this->format) ? null : $this->format, |
157 | 157 | 'pattern' => $this->pattern, |
158 | 158 | 'default' => $this->default, |
159 | - 'minLength' => $this->minLength ? (int)$this->minLength : null, |
|
160 | - 'maxLength' => $this->maxLength ? (int)$this->maxLength : null, |
|
159 | + 'minLength' => $this->minLength ? (int) $this->minLength : null, |
|
160 | + 'maxLength' => $this->maxLength ? (int) $this->maxLength : null, |
|
161 | 161 | 'enum' => $this->enum, |
162 | 162 | ), parent::toArray())); |
163 | 163 | } |
@@ -177,7 +177,7 @@ discard block |
||
177 | 177 | } else { |
178 | 178 | $type = $this->enum ? 'enum' : 'string'; |
179 | 179 | } |
180 | - throw new Exception("Empty {$type} default"); |
|
180 | + throw new Exception("empty {$type} default"); |
|
181 | 181 | } |
182 | 182 | |
183 | 183 | if (!empty($this->enum) && !in_array($value, $this->enum)) { |
@@ -190,7 +190,7 @@ discard block |
||
190 | 190 | } else { |
191 | 191 | $type = $this->enum ? 'enum' : 'string'; |
192 | 192 | } |
193 | - throw new Exception("Default {$type} length beyond maximum: '{$value}'"); |
|
193 | + throw new Exception("default {$type} length beyond maximum: '{$value}'"); |
|
194 | 194 | } |
195 | 195 | |
196 | 196 | if ($this->minLength !== null && mb_strlen($value) < $this->minLength) { |
@@ -199,7 +199,7 @@ discard block |
||
199 | 199 | } else { |
200 | 200 | $type = $this->enum ? 'enum' : 'string'; |
201 | 201 | } |
202 | - throw new Exception("Default {$type} length beyond minimum: '{$value}'"); |
|
202 | + throw new Exception("default {$type} length beyond minimum: '{$value}'"); |
|
203 | 203 | } |
204 | 204 | |
205 | 205 | return $value; |
@@ -15,48 +15,48 @@ |
||
15 | 15 | class ReferenceObjectType extends AbstractType |
16 | 16 | { |
17 | 17 | |
18 | - private $reference = null; |
|
19 | - |
|
20 | - /** |
|
21 | - * @throws Exception |
|
22 | - */ |
|
23 | - protected function parseDefinition($definition) |
|
24 | - { |
|
25 | - $definition = self::trim($definition); |
|
26 | - |
|
27 | - $match = array(); |
|
28 | - if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_CONTENT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
29 | - throw new Exception('Unparseable string definition: \'' . $definition . '\''); |
|
30 | - } |
|
31 | - |
|
32 | - $type = strtolower($match[1]); |
|
33 | - |
|
34 | - $reference = null; |
|
35 | - if ($type === 'refobject') { |
|
36 | - if (isset($match[2])) { |
|
37 | - $reference = $match[2]; |
|
38 | - } |
|
39 | - } else { |
|
40 | - $reference = $match[1]; |
|
41 | - } |
|
42 | - |
|
43 | - if (empty($reference)) { |
|
44 | - throw new Exception('Referenced object name missing: \'' . $definition . '\''); |
|
45 | - } |
|
46 | - |
|
47 | - $this->reference = $reference; |
|
48 | - } |
|
49 | - |
|
50 | - public function toArray(): array |
|
51 | - { |
|
52 | - return self::arrayFilterNull(array_merge(array( |
|
53 | - '$ref' => '#/definitions/' . $this->reference, |
|
54 | - ), parent::toArray())); |
|
55 | - } |
|
56 | - |
|
57 | - public function __toString() |
|
58 | - { |
|
59 | - return __CLASS__ . ' ' . $this->reference; |
|
60 | - } |
|
18 | + private $reference = null; |
|
19 | + |
|
20 | + /** |
|
21 | + * @throws Exception |
|
22 | + */ |
|
23 | + protected function parseDefinition($definition) |
|
24 | + { |
|
25 | + $definition = self::trim($definition); |
|
26 | + |
|
27 | + $match = array(); |
|
28 | + if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_CONTENT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
29 | + throw new Exception('Unparseable string definition: \'' . $definition . '\''); |
|
30 | + } |
|
31 | + |
|
32 | + $type = strtolower($match[1]); |
|
33 | + |
|
34 | + $reference = null; |
|
35 | + if ($type === 'refobject') { |
|
36 | + if (isset($match[2])) { |
|
37 | + $reference = $match[2]; |
|
38 | + } |
|
39 | + } else { |
|
40 | + $reference = $match[1]; |
|
41 | + } |
|
42 | + |
|
43 | + if (empty($reference)) { |
|
44 | + throw new Exception('Referenced object name missing: \'' . $definition . '\''); |
|
45 | + } |
|
46 | + |
|
47 | + $this->reference = $reference; |
|
48 | + } |
|
49 | + |
|
50 | + public function toArray(): array |
|
51 | + { |
|
52 | + return self::arrayFilterNull(array_merge(array( |
|
53 | + '$ref' => '#/definitions/' . $this->reference, |
|
54 | + ), parent::toArray())); |
|
55 | + } |
|
56 | + |
|
57 | + public function __toString() |
|
58 | + { |
|
59 | + return __CLASS__ . ' ' . $this->reference; |
|
60 | + } |
|
61 | 61 | |
62 | 62 | } |
@@ -13,47 +13,47 @@ |
||
13 | 13 | class Tag extends AbstractDocumentableObject |
14 | 14 | { |
15 | 15 | |
16 | - private $name; |
|
17 | - private $description; |
|
18 | - |
|
19 | - public function __construct(AbstractObject $parent, $name, $description = null) |
|
20 | - { |
|
21 | - parent::__construct($parent); |
|
22 | - $this->name = $name; |
|
23 | - $this->description = $description; |
|
24 | - } |
|
25 | - |
|
26 | - /** |
|
27 | - * @param string $command |
|
28 | - * @param string $data |
|
29 | - * @return AbstractObject|boolean |
|
30 | - */ |
|
31 | - public function handleCommand($command, $data = null) |
|
32 | - { |
|
33 | - if (strtolower($command) === 'description') { |
|
34 | - $this->description = $data; |
|
35 | - return $this; |
|
36 | - } |
|
37 | - |
|
38 | - return parent::handleCommand($command, $data); |
|
39 | - } |
|
40 | - |
|
41 | - public function toArray(): array |
|
42 | - { |
|
43 | - return self::arrayFilterNull(array_merge(array( |
|
44 | - 'name' => $this->name, |
|
45 | - 'description' => empty($this->description) ? null : $this->description, |
|
46 | - ), parent::toArray())); |
|
47 | - } |
|
48 | - |
|
49 | - public function getName() |
|
50 | - { |
|
51 | - return $this->name; |
|
52 | - } |
|
53 | - |
|
54 | - public function __toString() |
|
55 | - { |
|
56 | - return __CLASS__ . ' ' . $this->name; |
|
57 | - } |
|
16 | + private $name; |
|
17 | + private $description; |
|
18 | + |
|
19 | + public function __construct(AbstractObject $parent, $name, $description = null) |
|
20 | + { |
|
21 | + parent::__construct($parent); |
|
22 | + $this->name = $name; |
|
23 | + $this->description = $description; |
|
24 | + } |
|
25 | + |
|
26 | + /** |
|
27 | + * @param string $command |
|
28 | + * @param string $data |
|
29 | + * @return AbstractObject|boolean |
|
30 | + */ |
|
31 | + public function handleCommand($command, $data = null) |
|
32 | + { |
|
33 | + if (strtolower($command) === 'description') { |
|
34 | + $this->description = $data; |
|
35 | + return $this; |
|
36 | + } |
|
37 | + |
|
38 | + return parent::handleCommand($command, $data); |
|
39 | + } |
|
40 | + |
|
41 | + public function toArray(): array |
|
42 | + { |
|
43 | + return self::arrayFilterNull(array_merge(array( |
|
44 | + 'name' => $this->name, |
|
45 | + 'description' => empty($this->description) ? null : $this->description, |
|
46 | + ), parent::toArray())); |
|
47 | + } |
|
48 | + |
|
49 | + public function getName() |
|
50 | + { |
|
51 | + return $this->name; |
|
52 | + } |
|
53 | + |
|
54 | + public function __toString() |
|
55 | + { |
|
56 | + return __CLASS__ . ' ' . $this->name; |
|
57 | + } |
|
58 | 58 | |
59 | 59 | } |
@@ -16,162 +16,162 @@ |
||
16 | 16 | abstract class AbstractObject |
17 | 17 | { |
18 | 18 | |
19 | - private static $mime_types = array( |
|
20 | - 'fileform' => 'multipart/form-data', |
|
21 | - 'form' => 'application/x-www-form-urlencoded', |
|
22 | - 'json' => 'application/json', |
|
23 | - 'text' => 'text/plain', |
|
24 | - 'utf8' => 'text/plain; charset=utf-8', |
|
25 | - 'yml' => 'application/x-yaml', |
|
26 | - 'yaml' => 'application/x-yaml', |
|
27 | - 'php' => 'text/x-php', |
|
28 | - 'xml' => 'text/xml', |
|
29 | - ); |
|
30 | - |
|
31 | - /** |
|
32 | - * @var AbstractObject |
|
33 | - */ |
|
34 | - private $parent; |
|
35 | - |
|
36 | - /** |
|
37 | - * Map of extensions and their (trimmed) values |
|
38 | - * @var string[] |
|
39 | - */ |
|
40 | - private $extensions = array(); |
|
41 | - |
|
42 | - public function __construct(AbstractObject $parent = null) |
|
43 | - { |
|
44 | - $this->parent = $parent; |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * @return AbstractObject|null |
|
49 | - */ |
|
50 | - protected function getParent(): ?AbstractObject |
|
51 | - { |
|
52 | - return $this->parent; |
|
53 | - } |
|
54 | - |
|
55 | - protected function getParentClass($classname): AbstractObject |
|
56 | - { |
|
57 | - if (is_a($this, $classname)) { |
|
58 | - return $this; |
|
59 | - } |
|
60 | - return $this->parent->getParentClass($classname); |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * @return Swagger |
|
65 | - */ |
|
66 | - protected function getSwagger(): Swagger |
|
67 | - { |
|
68 | - return $this->parent->getSwagger(); |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * @return TypeRegistry |
|
73 | - */ |
|
74 | - protected function getTypeRegistry(): TypeRegistry |
|
75 | - { |
|
76 | - return $this->parent->getTypeRegistry(); |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * @param string $command |
|
81 | - * @param string $data |
|
82 | - * @return AbstractObject|boolean |
|
83 | - */ |
|
84 | - public function handleCommand($command, $data = null) |
|
85 | - { |
|
86 | - if (stripos($command, 'x-') === 0) { |
|
87 | - $this->extensions[$command] = empty($data) ? $data : trim($data); |
|
88 | - return $this; |
|
89 | - } |
|
90 | - |
|
91 | - return false; |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * @return array |
|
96 | - */ |
|
97 | - public function toArray(): array |
|
98 | - { |
|
99 | - return $this->extensions; |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * Translate consumes from shortcuts |
|
104 | - * @param String[] $mimeTypes |
|
105 | - * @return String[] |
|
106 | - */ |
|
107 | - protected static function translateMimeTypes($mimeTypes): array |
|
108 | - { |
|
109 | - foreach ($mimeTypes as &$mimeType) { |
|
110 | - if (isset(self::$mime_types[strtolower($mimeType)])) { |
|
111 | - $mimeType = self::$mime_types[strtolower($mimeType)]; |
|
112 | - } |
|
113 | - } |
|
114 | - |
|
115 | - return $mimeTypes; |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * Trim whitespace from a multibyte string |
|
120 | - * @param string $string |
|
121 | - * @return string |
|
122 | - */ |
|
123 | - public static function trim($string): string |
|
124 | - { |
|
125 | - return mb_ereg_replace('^\s*([\s\S]*?)\s*$', '\1', $string); |
|
126 | - } |
|
127 | - |
|
128 | - /** |
|
129 | - * Filter all items from an array where the value is either null or an |
|
130 | - * empty array. |
|
131 | - * @param array $array |
|
132 | - * @return array |
|
133 | - */ |
|
134 | - public static function arrayFilterNull($array): array |
|
135 | - { |
|
136 | - return array_filter($array, static function ($value) { |
|
137 | - return $value !== null && $value !== array(); |
|
138 | - }); |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * Recursively call toArray() on all objects to return an array |
|
143 | - * @param array $array |
|
144 | - * @return array |
|
145 | - */ |
|
146 | - public static function objectsToArray($array): array |
|
147 | - { |
|
148 | - return array_map(static function (AbstractObject $item) { |
|
149 | - return $item->toArray(); |
|
150 | - }, $array); |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * Shifts the first word off a text line and returns it |
|
155 | - * @param string $data |
|
156 | - * @return string|bool Either the first word or false if no more words available |
|
157 | - */ |
|
158 | - public static function wordShift(&$data) |
|
159 | - { |
|
160 | - if (preg_match('~^\s*(\S+)\s*(.*)$~s', $data, $matches) === 1) { |
|
161 | - $data = $matches[2]; |
|
162 | - return $matches[1]; |
|
163 | - } |
|
164 | - return false; |
|
165 | - } |
|
166 | - |
|
167 | - /** |
|
168 | - * Splits a text line in all it's words |
|
169 | - * @param string $data |
|
170 | - * @return string[] |
|
171 | - */ |
|
172 | - public static function wordSplit($data): array |
|
173 | - { |
|
174 | - return array_values(preg_grep('~\S~', preg_split('~\s+~', $data))); |
|
175 | - } |
|
19 | + private static $mime_types = array( |
|
20 | + 'fileform' => 'multipart/form-data', |
|
21 | + 'form' => 'application/x-www-form-urlencoded', |
|
22 | + 'json' => 'application/json', |
|
23 | + 'text' => 'text/plain', |
|
24 | + 'utf8' => 'text/plain; charset=utf-8', |
|
25 | + 'yml' => 'application/x-yaml', |
|
26 | + 'yaml' => 'application/x-yaml', |
|
27 | + 'php' => 'text/x-php', |
|
28 | + 'xml' => 'text/xml', |
|
29 | + ); |
|
30 | + |
|
31 | + /** |
|
32 | + * @var AbstractObject |
|
33 | + */ |
|
34 | + private $parent; |
|
35 | + |
|
36 | + /** |
|
37 | + * Map of extensions and their (trimmed) values |
|
38 | + * @var string[] |
|
39 | + */ |
|
40 | + private $extensions = array(); |
|
41 | + |
|
42 | + public function __construct(AbstractObject $parent = null) |
|
43 | + { |
|
44 | + $this->parent = $parent; |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * @return AbstractObject|null |
|
49 | + */ |
|
50 | + protected function getParent(): ?AbstractObject |
|
51 | + { |
|
52 | + return $this->parent; |
|
53 | + } |
|
54 | + |
|
55 | + protected function getParentClass($classname): AbstractObject |
|
56 | + { |
|
57 | + if (is_a($this, $classname)) { |
|
58 | + return $this; |
|
59 | + } |
|
60 | + return $this->parent->getParentClass($classname); |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * @return Swagger |
|
65 | + */ |
|
66 | + protected function getSwagger(): Swagger |
|
67 | + { |
|
68 | + return $this->parent->getSwagger(); |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * @return TypeRegistry |
|
73 | + */ |
|
74 | + protected function getTypeRegistry(): TypeRegistry |
|
75 | + { |
|
76 | + return $this->parent->getTypeRegistry(); |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * @param string $command |
|
81 | + * @param string $data |
|
82 | + * @return AbstractObject|boolean |
|
83 | + */ |
|
84 | + public function handleCommand($command, $data = null) |
|
85 | + { |
|
86 | + if (stripos($command, 'x-') === 0) { |
|
87 | + $this->extensions[$command] = empty($data) ? $data : trim($data); |
|
88 | + return $this; |
|
89 | + } |
|
90 | + |
|
91 | + return false; |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * @return array |
|
96 | + */ |
|
97 | + public function toArray(): array |
|
98 | + { |
|
99 | + return $this->extensions; |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * Translate consumes from shortcuts |
|
104 | + * @param String[] $mimeTypes |
|
105 | + * @return String[] |
|
106 | + */ |
|
107 | + protected static function translateMimeTypes($mimeTypes): array |
|
108 | + { |
|
109 | + foreach ($mimeTypes as &$mimeType) { |
|
110 | + if (isset(self::$mime_types[strtolower($mimeType)])) { |
|
111 | + $mimeType = self::$mime_types[strtolower($mimeType)]; |
|
112 | + } |
|
113 | + } |
|
114 | + |
|
115 | + return $mimeTypes; |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * Trim whitespace from a multibyte string |
|
120 | + * @param string $string |
|
121 | + * @return string |
|
122 | + */ |
|
123 | + public static function trim($string): string |
|
124 | + { |
|
125 | + return mb_ereg_replace('^\s*([\s\S]*?)\s*$', '\1', $string); |
|
126 | + } |
|
127 | + |
|
128 | + /** |
|
129 | + * Filter all items from an array where the value is either null or an |
|
130 | + * empty array. |
|
131 | + * @param array $array |
|
132 | + * @return array |
|
133 | + */ |
|
134 | + public static function arrayFilterNull($array): array |
|
135 | + { |
|
136 | + return array_filter($array, static function ($value) { |
|
137 | + return $value !== null && $value !== array(); |
|
138 | + }); |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * Recursively call toArray() on all objects to return an array |
|
143 | + * @param array $array |
|
144 | + * @return array |
|
145 | + */ |
|
146 | + public static function objectsToArray($array): array |
|
147 | + { |
|
148 | + return array_map(static function (AbstractObject $item) { |
|
149 | + return $item->toArray(); |
|
150 | + }, $array); |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * Shifts the first word off a text line and returns it |
|
155 | + * @param string $data |
|
156 | + * @return string|bool Either the first word or false if no more words available |
|
157 | + */ |
|
158 | + public static function wordShift(&$data) |
|
159 | + { |
|
160 | + if (preg_match('~^\s*(\S+)\s*(.*)$~s', $data, $matches) === 1) { |
|
161 | + $data = $matches[2]; |
|
162 | + return $matches[1]; |
|
163 | + } |
|
164 | + return false; |
|
165 | + } |
|
166 | + |
|
167 | + /** |
|
168 | + * Splits a text line in all it's words |
|
169 | + * @param string $data |
|
170 | + * @return string[] |
|
171 | + */ |
|
172 | + public static function wordSplit($data): array |
|
173 | + { |
|
174 | + return array_values(preg_grep('~\S~', preg_split('~\s+~', $data))); |
|
175 | + } |
|
176 | 176 | |
177 | 177 | } |
@@ -133,7 +133,7 @@ discard block |
||
133 | 133 | */ |
134 | 134 | public static function arrayFilterNull($array): array |
135 | 135 | { |
136 | - return array_filter($array, static function ($value) { |
|
136 | + return array_filter($array, static function($value) { |
|
137 | 137 | return $value !== null && $value !== array(); |
138 | 138 | }); |
139 | 139 | } |
@@ -145,7 +145,7 @@ discard block |
||
145 | 145 | */ |
146 | 146 | public static function objectsToArray($array): array |
147 | 147 | { |
148 | - return array_map(static function (AbstractObject $item) { |
|
148 | + return array_map(static function(AbstractObject $item) { |
|
149 | 149 | return $item->toArray(); |
150 | 150 | }, $array); |
151 | 151 | } |