@@ -14,44 +14,44 @@ |
||
14 | 14 | class ExternalDocumentation extends AbstractObject |
15 | 15 | { |
16 | 16 | |
17 | - private $url; |
|
18 | - private $description; |
|
19 | - |
|
20 | - public function __construct(AbstractObject $parent, $url, $description = null) |
|
21 | - { |
|
22 | - parent::__construct($parent); |
|
23 | - $this->url = $url; |
|
24 | - $this->description = $description; |
|
25 | - } |
|
26 | - |
|
27 | - /** |
|
28 | - * @param string $command |
|
29 | - * @param string $data |
|
30 | - * @return AbstractObject|boolean |
|
31 | - */ |
|
32 | - public function handleCommand($command, $data = null) |
|
33 | - { |
|
34 | - switch (strtolower($command)) { |
|
35 | - case 'url': |
|
36 | - case 'description': |
|
37 | - $this->$command = $data; |
|
38 | - return $this; |
|
39 | - } |
|
40 | - |
|
41 | - return parent::handleCommand($command, $data); |
|
42 | - } |
|
43 | - |
|
44 | - public function toArray(): array |
|
45 | - { |
|
46 | - return self::arrayFilterNull(array_merge(array( |
|
47 | - 'url' => $this->url, |
|
48 | - 'description' => empty($this->description) ? null : $this->description, |
|
49 | - ), parent::toArray())); |
|
50 | - } |
|
51 | - |
|
52 | - public function __toString() |
|
53 | - { |
|
54 | - return __CLASS__ . ' ' . $this->url; |
|
55 | - } |
|
17 | + private $url; |
|
18 | + private $description; |
|
19 | + |
|
20 | + public function __construct(AbstractObject $parent, $url, $description = null) |
|
21 | + { |
|
22 | + parent::__construct($parent); |
|
23 | + $this->url = $url; |
|
24 | + $this->description = $description; |
|
25 | + } |
|
26 | + |
|
27 | + /** |
|
28 | + * @param string $command |
|
29 | + * @param string $data |
|
30 | + * @return AbstractObject|boolean |
|
31 | + */ |
|
32 | + public function handleCommand($command, $data = null) |
|
33 | + { |
|
34 | + switch (strtolower($command)) { |
|
35 | + case 'url': |
|
36 | + case 'description': |
|
37 | + $this->$command = $data; |
|
38 | + return $this; |
|
39 | + } |
|
40 | + |
|
41 | + return parent::handleCommand($command, $data); |
|
42 | + } |
|
43 | + |
|
44 | + public function toArray(): array |
|
45 | + { |
|
46 | + return self::arrayFilterNull(array_merge(array( |
|
47 | + 'url' => $this->url, |
|
48 | + 'description' => empty($this->description) ? null : $this->description, |
|
49 | + ), parent::toArray())); |
|
50 | + } |
|
51 | + |
|
52 | + public function __toString() |
|
53 | + { |
|
54 | + return __CLASS__ . ' ' . $this->url; |
|
55 | + } |
|
56 | 56 | |
57 | 57 | } |
@@ -15,166 +15,166 @@ |
||
15 | 15 | class IntegerType extends AbstractType |
16 | 16 | { |
17 | 17 | |
18 | - /** @noinspection PhpRegExpUnsupportedModifierInspection */ |
|
19 | - const REGEX_RANGE = '(?:([[<])(-?\d*)?,(-?\d*)?([\\]>]))?'; |
|
20 | - /** @noinspection PhpRegExpUnsupportedModifierInspection */ |
|
21 | - const REGEX_DEFAULT = '(?:=(-?\d+))?'; |
|
22 | - |
|
23 | - private static $formats = array( |
|
24 | - 'int32' => 'int32', |
|
25 | - 'integer' => 'int32', |
|
26 | - 'int' => 'int32', |
|
27 | - 'int64' => 'int64', |
|
28 | - 'long' => 'int64', |
|
29 | - ); |
|
30 | - private $format; |
|
31 | - private $default = null; |
|
32 | - private $maximum = null; |
|
33 | - private $exclusiveMaximum = null; |
|
34 | - private $minimum = null; |
|
35 | - private $exclusiveMinimum = null; |
|
36 | - private $enum = array(); |
|
37 | - private $multipleOf = null; |
|
38 | - |
|
39 | - /** |
|
40 | - * @throws Exception |
|
41 | - */ |
|
42 | - protected function parseDefinition($definition) |
|
43 | - { |
|
44 | - $definition = self::trim($definition); |
|
45 | - |
|
46 | - $match = array(); |
|
47 | - if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
48 | - throw new Exception("Unparseable integer definition: '{$definition}'"); |
|
49 | - } |
|
50 | - |
|
51 | - $this->parseFormat($definition, $match); |
|
52 | - $this->parseRange($definition, $match); |
|
53 | - $this->parseDefault($definition, $match); |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * @param string $definition |
|
58 | - * @param string[] $match |
|
59 | - * @throws Exception |
|
60 | - */ |
|
61 | - private function parseFormat($definition, $match) |
|
62 | - { |
|
63 | - $type = strtolower($match[1]); |
|
64 | - if (!isset(self::$formats[$type])) { |
|
65 | - throw new Exception("Not an integer: '{$definition}'"); |
|
66 | - } |
|
67 | - $this->format = self::$formats[$type]; |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * @param string $definition |
|
72 | - * @param string[] $match |
|
73 | - * @throws Exception |
|
74 | - */ |
|
75 | - private function parseRange($definition, $match) |
|
76 | - { |
|
77 | - if (!empty($match[2])) { |
|
78 | - if ($match[3] === '' && $match[4] === '') { |
|
79 | - throw new Exception("Empty integer range: '{$definition}'"); |
|
80 | - } |
|
81 | - |
|
82 | - $this->exclusiveMinimum = $match[2] == '<'; |
|
83 | - $this->minimum = $match[3] === '' ? null : (int)$match[3]; |
|
84 | - $this->maximum = $match[4] === '' ? null : (int)$match[4]; |
|
85 | - $this->exclusiveMaximum = isset($match[5]) ? ($match[5] == '>') : null; |
|
86 | - if ($this->minimum && $this->maximum && $this->minimum > $this->maximum) { |
|
87 | - self::swap($this->minimum, $this->maximum); |
|
88 | - self::swap($this->exclusiveMinimum, $this->exclusiveMaximum); |
|
89 | - } |
|
90 | - } |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * @param string $definition |
|
95 | - * @param string[] $match |
|
96 | - * @throws Exception |
|
97 | - */ |
|
98 | - private function parseDefault($definition, $match) |
|
99 | - { |
|
100 | - $this->default = isset($match[6]) && $match[6] !== '' ? $this->validateDefault($match[6]) : null; |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * @param string $command The comment command |
|
105 | - * @param string $data Any data added after the command |
|
106 | - * @return AbstractType|boolean |
|
107 | - * @throws Exception |
|
108 | - * @throws Exception |
|
109 | - * @throws Exception |
|
110 | - */ |
|
111 | - public function handleCommand($command, $data = null) |
|
112 | - { |
|
113 | - switch (strtolower($command)) { |
|
114 | - case 'default': |
|
115 | - $this->default = $this->validateDefault($data); |
|
116 | - return $this; |
|
117 | - |
|
118 | - case 'enum': |
|
119 | - $words = self::wordSplit($data); |
|
120 | - foreach ($words as &$word) { |
|
121 | - $word = $this->validateDefault($word); |
|
122 | - } |
|
123 | - unset($word); |
|
124 | - $this->enum = array_merge($this->enum, $words); |
|
125 | - return $this; |
|
126 | - |
|
127 | - case 'step': |
|
128 | - if (($step = (int)$data) > 0) { |
|
129 | - $this->multipleOf = $step; |
|
130 | - } |
|
131 | - return $this; |
|
132 | - } |
|
133 | - |
|
134 | - return parent::handleCommand($command, $data); |
|
135 | - } |
|
136 | - |
|
137 | - public function toArray(): array |
|
138 | - { |
|
139 | - return self::arrayFilterNull(array_merge(array( |
|
140 | - 'type' => 'integer', |
|
141 | - 'format' => $this->format, |
|
142 | - 'default' => $this->default, |
|
143 | - 'minimum' => $this->minimum, |
|
144 | - 'exclusiveMinimum' => ($this->exclusiveMinimum && !is_null($this->minimum)) ? true : null, |
|
145 | - 'maximum' => $this->maximum, |
|
146 | - 'exclusiveMaximum' => ($this->exclusiveMaximum && !is_null($this->maximum)) ? true : null, |
|
147 | - 'enum' => $this->enum, |
|
148 | - 'multipleOf' => $this->multipleOf, |
|
149 | - ), parent::toArray())); |
|
150 | - } |
|
151 | - |
|
152 | - public function __toString() |
|
153 | - { |
|
154 | - return __CLASS__; |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * @throws Exception |
|
159 | - */ |
|
160 | - private function validateDefault($value) |
|
161 | - { |
|
162 | - if (preg_match('~^-?\d+$~', $value) !== 1) { |
|
163 | - throw new Exception("Invalid integer default: '{$value}'"); |
|
164 | - } |
|
165 | - |
|
166 | - if ($this->maximum) { |
|
167 | - if (($value > $this->maximum) || ($this->exclusiveMaximum && $value == $this->maximum)) { |
|
168 | - throw new Exception("Default integer beyond maximum: '{$value}'"); |
|
169 | - } |
|
170 | - } |
|
171 | - if ($this->minimum) { |
|
172 | - if (($value < $this->minimum) || ($this->exclusiveMinimum && $value == $this->minimum)) { |
|
173 | - throw new Exception("Default integer beyond minimum: '{$value}'"); |
|
174 | - } |
|
175 | - } |
|
176 | - |
|
177 | - return (int)$value; |
|
178 | - } |
|
18 | + /** @noinspection PhpRegExpUnsupportedModifierInspection */ |
|
19 | + const REGEX_RANGE = '(?:([[<])(-?\d*)?,(-?\d*)?([\\]>]))?'; |
|
20 | + /** @noinspection PhpRegExpUnsupportedModifierInspection */ |
|
21 | + const REGEX_DEFAULT = '(?:=(-?\d+))?'; |
|
22 | + |
|
23 | + private static $formats = array( |
|
24 | + 'int32' => 'int32', |
|
25 | + 'integer' => 'int32', |
|
26 | + 'int' => 'int32', |
|
27 | + 'int64' => 'int64', |
|
28 | + 'long' => 'int64', |
|
29 | + ); |
|
30 | + private $format; |
|
31 | + private $default = null; |
|
32 | + private $maximum = null; |
|
33 | + private $exclusiveMaximum = null; |
|
34 | + private $minimum = null; |
|
35 | + private $exclusiveMinimum = null; |
|
36 | + private $enum = array(); |
|
37 | + private $multipleOf = null; |
|
38 | + |
|
39 | + /** |
|
40 | + * @throws Exception |
|
41 | + */ |
|
42 | + protected function parseDefinition($definition) |
|
43 | + { |
|
44 | + $definition = self::trim($definition); |
|
45 | + |
|
46 | + $match = array(); |
|
47 | + if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
48 | + throw new Exception("Unparseable integer definition: '{$definition}'"); |
|
49 | + } |
|
50 | + |
|
51 | + $this->parseFormat($definition, $match); |
|
52 | + $this->parseRange($definition, $match); |
|
53 | + $this->parseDefault($definition, $match); |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * @param string $definition |
|
58 | + * @param string[] $match |
|
59 | + * @throws Exception |
|
60 | + */ |
|
61 | + private function parseFormat($definition, $match) |
|
62 | + { |
|
63 | + $type = strtolower($match[1]); |
|
64 | + if (!isset(self::$formats[$type])) { |
|
65 | + throw new Exception("Not an integer: '{$definition}'"); |
|
66 | + } |
|
67 | + $this->format = self::$formats[$type]; |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * @param string $definition |
|
72 | + * @param string[] $match |
|
73 | + * @throws Exception |
|
74 | + */ |
|
75 | + private function parseRange($definition, $match) |
|
76 | + { |
|
77 | + if (!empty($match[2])) { |
|
78 | + if ($match[3] === '' && $match[4] === '') { |
|
79 | + throw new Exception("Empty integer range: '{$definition}'"); |
|
80 | + } |
|
81 | + |
|
82 | + $this->exclusiveMinimum = $match[2] == '<'; |
|
83 | + $this->minimum = $match[3] === '' ? null : (int)$match[3]; |
|
84 | + $this->maximum = $match[4] === '' ? null : (int)$match[4]; |
|
85 | + $this->exclusiveMaximum = isset($match[5]) ? ($match[5] == '>') : null; |
|
86 | + if ($this->minimum && $this->maximum && $this->minimum > $this->maximum) { |
|
87 | + self::swap($this->minimum, $this->maximum); |
|
88 | + self::swap($this->exclusiveMinimum, $this->exclusiveMaximum); |
|
89 | + } |
|
90 | + } |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * @param string $definition |
|
95 | + * @param string[] $match |
|
96 | + * @throws Exception |
|
97 | + */ |
|
98 | + private function parseDefault($definition, $match) |
|
99 | + { |
|
100 | + $this->default = isset($match[6]) && $match[6] !== '' ? $this->validateDefault($match[6]) : null; |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * @param string $command The comment command |
|
105 | + * @param string $data Any data added after the command |
|
106 | + * @return AbstractType|boolean |
|
107 | + * @throws Exception |
|
108 | + * @throws Exception |
|
109 | + * @throws Exception |
|
110 | + */ |
|
111 | + public function handleCommand($command, $data = null) |
|
112 | + { |
|
113 | + switch (strtolower($command)) { |
|
114 | + case 'default': |
|
115 | + $this->default = $this->validateDefault($data); |
|
116 | + return $this; |
|
117 | + |
|
118 | + case 'enum': |
|
119 | + $words = self::wordSplit($data); |
|
120 | + foreach ($words as &$word) { |
|
121 | + $word = $this->validateDefault($word); |
|
122 | + } |
|
123 | + unset($word); |
|
124 | + $this->enum = array_merge($this->enum, $words); |
|
125 | + return $this; |
|
126 | + |
|
127 | + case 'step': |
|
128 | + if (($step = (int)$data) > 0) { |
|
129 | + $this->multipleOf = $step; |
|
130 | + } |
|
131 | + return $this; |
|
132 | + } |
|
133 | + |
|
134 | + return parent::handleCommand($command, $data); |
|
135 | + } |
|
136 | + |
|
137 | + public function toArray(): array |
|
138 | + { |
|
139 | + return self::arrayFilterNull(array_merge(array( |
|
140 | + 'type' => 'integer', |
|
141 | + 'format' => $this->format, |
|
142 | + 'default' => $this->default, |
|
143 | + 'minimum' => $this->minimum, |
|
144 | + 'exclusiveMinimum' => ($this->exclusiveMinimum && !is_null($this->minimum)) ? true : null, |
|
145 | + 'maximum' => $this->maximum, |
|
146 | + 'exclusiveMaximum' => ($this->exclusiveMaximum && !is_null($this->maximum)) ? true : null, |
|
147 | + 'enum' => $this->enum, |
|
148 | + 'multipleOf' => $this->multipleOf, |
|
149 | + ), parent::toArray())); |
|
150 | + } |
|
151 | + |
|
152 | + public function __toString() |
|
153 | + { |
|
154 | + return __CLASS__; |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * @throws Exception |
|
159 | + */ |
|
160 | + private function validateDefault($value) |
|
161 | + { |
|
162 | + if (preg_match('~^-?\d+$~', $value) !== 1) { |
|
163 | + throw new Exception("Invalid integer default: '{$value}'"); |
|
164 | + } |
|
165 | + |
|
166 | + if ($this->maximum) { |
|
167 | + if (($value > $this->maximum) || ($this->exclusiveMaximum && $value == $this->maximum)) { |
|
168 | + throw new Exception("Default integer beyond maximum: '{$value}'"); |
|
169 | + } |
|
170 | + } |
|
171 | + if ($this->minimum) { |
|
172 | + if (($value < $this->minimum) || ($this->exclusiveMinimum && $value == $this->minimum)) { |
|
173 | + throw new Exception("Default integer beyond minimum: '{$value}'"); |
|
174 | + } |
|
175 | + } |
|
176 | + |
|
177 | + return (int)$value; |
|
178 | + } |
|
179 | 179 | |
180 | 180 | } |
@@ -80,8 +80,8 @@ discard block |
||
80 | 80 | } |
81 | 81 | |
82 | 82 | $this->exclusiveMinimum = $match[2] == '<'; |
83 | - $this->minimum = $match[3] === '' ? null : (int)$match[3]; |
|
84 | - $this->maximum = $match[4] === '' ? null : (int)$match[4]; |
|
83 | + $this->minimum = $match[3] === '' ? null : (int) $match[3]; |
|
84 | + $this->maximum = $match[4] === '' ? null : (int) $match[4]; |
|
85 | 85 | $this->exclusiveMaximum = isset($match[5]) ? ($match[5] == '>') : null; |
86 | 86 | if ($this->minimum && $this->maximum && $this->minimum > $this->maximum) { |
87 | 87 | self::swap($this->minimum, $this->maximum); |
@@ -125,7 +125,7 @@ discard block |
||
125 | 125 | return $this; |
126 | 126 | |
127 | 127 | case 'step': |
128 | - if (($step = (int)$data) > 0) { |
|
128 | + if (($step = (int) $data) > 0) { |
|
129 | 129 | $this->multipleOf = $step; |
130 | 130 | } |
131 | 131 | return $this; |
@@ -174,7 +174,7 @@ discard block |
||
174 | 174 | } |
175 | 175 | } |
176 | 176 | |
177 | - return (int)$value; |
|
177 | + return (int) $value; |
|
178 | 178 | } |
179 | 179 | |
180 | 180 | } |
@@ -14,52 +14,52 @@ |
||
14 | 14 | */ |
15 | 15 | class AllOfType extends AbstractType |
16 | 16 | { |
17 | - private $allOfItems = array(); |
|
18 | - private $mostRecentItem; |
|
17 | + private $allOfItems = array(); |
|
18 | + private $mostRecentItem; |
|
19 | 19 | |
20 | - /** |
|
21 | - * @throws Exception |
|
22 | - */ |
|
23 | - protected function parseDefinition($definition) |
|
24 | - { |
|
25 | - $pattern = self::REGEX_START . 'allof' . self::REGEX_CONTENT . self::REGEX_END; |
|
26 | - $inlineDef = ''; |
|
27 | - if (preg_match($pattern, $definition, $matches) |
|
28 | - && isset($matches[1])) { |
|
29 | - $inlineDef = $matches[1]; |
|
30 | - } |
|
31 | - if ($inlineDef) { |
|
32 | - foreach (self::parseList($inlineDef) as $item) { |
|
33 | - $this->handleCommand('item', $item); |
|
34 | - } |
|
35 | - } |
|
36 | - } |
|
20 | + /** |
|
21 | + * @throws Exception |
|
22 | + */ |
|
23 | + protected function parseDefinition($definition) |
|
24 | + { |
|
25 | + $pattern = self::REGEX_START . 'allof' . self::REGEX_CONTENT . self::REGEX_END; |
|
26 | + $inlineDef = ''; |
|
27 | + if (preg_match($pattern, $definition, $matches) |
|
28 | + && isset($matches[1])) { |
|
29 | + $inlineDef = $matches[1]; |
|
30 | + } |
|
31 | + if ($inlineDef) { |
|
32 | + foreach (self::parseList($inlineDef) as $item) { |
|
33 | + $this->handleCommand('item', $item); |
|
34 | + } |
|
35 | + } |
|
36 | + } |
|
37 | 37 | |
38 | - /** |
|
39 | - * @throws Exception |
|
40 | - */ |
|
41 | - public function handleCommand($command, $data = null) |
|
42 | - { |
|
43 | - if (strtolower($command) === 'item') { |
|
44 | - $this->mostRecentItem = self::typeFactory($this, $data); |
|
45 | - $this->allOfItems[] = $this->mostRecentItem; |
|
46 | - return $this; |
|
47 | - } |
|
48 | - if (isset($this->mostRecentItem) |
|
49 | - && $this->mostRecentItem->handleCommand($command, $data)) { |
|
50 | - return $this; |
|
51 | - } |
|
52 | - return parent::handleCommand($command, $data); |
|
53 | - } |
|
38 | + /** |
|
39 | + * @throws Exception |
|
40 | + */ |
|
41 | + public function handleCommand($command, $data = null) |
|
42 | + { |
|
43 | + if (strtolower($command) === 'item') { |
|
44 | + $this->mostRecentItem = self::typeFactory($this, $data); |
|
45 | + $this->allOfItems[] = $this->mostRecentItem; |
|
46 | + return $this; |
|
47 | + } |
|
48 | + if (isset($this->mostRecentItem) |
|
49 | + && $this->mostRecentItem->handleCommand($command, $data)) { |
|
50 | + return $this; |
|
51 | + } |
|
52 | + return parent::handleCommand($command, $data); |
|
53 | + } |
|
54 | 54 | |
55 | - public function toArray(): array |
|
56 | - { |
|
57 | - $allOf = array(); |
|
58 | - foreach ($this->allOfItems as $item) { |
|
59 | - $allOf[] = $item->toArray(); |
|
60 | - } |
|
61 | - return self::arrayFilterNull(array_merge(array( |
|
62 | - 'allOf' => $allOf, |
|
63 | - ), parent::toArray())); |
|
64 | - } |
|
55 | + public function toArray(): array |
|
56 | + { |
|
57 | + $allOf = array(); |
|
58 | + foreach ($this->allOfItems as $item) { |
|
59 | + $allOf[] = $item->toArray(); |
|
60 | + } |
|
61 | + return self::arrayFilterNull(array_merge(array( |
|
62 | + 'allOf' => $allOf, |
|
63 | + ), parent::toArray())); |
|
64 | + } |
|
65 | 65 | } |
@@ -16,61 +16,61 @@ |
||
16 | 16 | class AbstractRegexType extends StringType |
17 | 17 | { |
18 | 18 | |
19 | - public const REGEX_DEFAULT_START = '(?:=('; |
|
20 | - public const REGEX_DEFAULT_END = '))?'; |
|
19 | + public const REGEX_DEFAULT_START = '(?:=('; |
|
20 | + public const REGEX_DEFAULT_END = '))?'; |
|
21 | 21 | |
22 | - /** |
|
23 | - * The raw regular expression to use. |
|
24 | - * Exclude start (`^`) and end (`$`) anchors. |
|
25 | - * @var string |
|
26 | - */ |
|
27 | - private $regex; |
|
22 | + /** |
|
23 | + * The raw regular expression to use. |
|
24 | + * Exclude start (`^`) and end (`$`) anchors. |
|
25 | + * @var string |
|
26 | + */ |
|
27 | + private $regex; |
|
28 | 28 | |
29 | - /** |
|
30 | - * Construct and set up the regular expression for this type |
|
31 | - * |
|
32 | - * @param AbstractObject $parent |
|
33 | - * @param string $definition |
|
34 | - * @param string $format Name of the string format |
|
35 | - * @param string $regex Regular expression to use as the format and for default validation |
|
36 | - */ |
|
37 | - public function __construct(AbstractObject $parent, $definition, $format, $regex) |
|
38 | - { |
|
39 | - $this->format = $format; |
|
40 | - $this->regex = $regex; |
|
29 | + /** |
|
30 | + * Construct and set up the regular expression for this type |
|
31 | + * |
|
32 | + * @param AbstractObject $parent |
|
33 | + * @param string $definition |
|
34 | + * @param string $format Name of the string format |
|
35 | + * @param string $regex Regular expression to use as the format and for default validation |
|
36 | + */ |
|
37 | + public function __construct(AbstractObject $parent, $definition, $format, $regex) |
|
38 | + { |
|
39 | + $this->format = $format; |
|
40 | + $this->regex = $regex; |
|
41 | 41 | |
42 | - parent::__construct($parent, $definition); |
|
43 | - } |
|
42 | + parent::__construct($parent, $definition); |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * @throws Exception |
|
47 | - */ |
|
48 | - protected function parseDefinition($definition) |
|
49 | - { |
|
50 | - $definition = self::trim($definition); |
|
45 | + /** |
|
46 | + * @throws Exception |
|
47 | + */ |
|
48 | + protected function parseDefinition($definition) |
|
49 | + { |
|
50 | + $definition = self::trim($definition); |
|
51 | 51 | |
52 | - $match = array(); |
|
53 | - if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_DEFAULT_START . $this->regex . self::REGEX_DEFAULT_END . self::REGEX_END, $definition, $match) !== 1) { |
|
54 | - throw new Exception('Unparseable ' . $this->format . ' definition: \'' . $definition . '\''); |
|
55 | - } |
|
52 | + $match = array(); |
|
53 | + if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_DEFAULT_START . $this->regex . self::REGEX_DEFAULT_END . self::REGEX_END, $definition, $match) !== 1) { |
|
54 | + throw new Exception('Unparseable ' . $this->format . ' definition: \'' . $definition . '\''); |
|
55 | + } |
|
56 | 56 | |
57 | - if (strtolower($match[1]) !== $this->format) { |
|
58 | - throw new Exception('Not a ' . $this->format . ': \'' . $definition . '\''); |
|
59 | - } |
|
57 | + if (strtolower($match[1]) !== $this->format) { |
|
58 | + throw new Exception('Not a ' . $this->format . ': \'' . $definition . '\''); |
|
59 | + } |
|
60 | 60 | |
61 | - $this->pattern = '^' . $this->regex . '$'; |
|
62 | - $this->default = isset($match[2]) && $match[2] !== '' ? $this->validateDefault($match[2]) : null; |
|
63 | - } |
|
61 | + $this->pattern = '^' . $this->regex . '$'; |
|
62 | + $this->default = isset($match[2]) && $match[2] !== '' ? $this->validateDefault($match[2]) : null; |
|
63 | + } |
|
64 | 64 | |
65 | - protected function validateDefault($value) |
|
66 | - { |
|
67 | - $value = parent::validateDefault($value); |
|
65 | + protected function validateDefault($value) |
|
66 | + { |
|
67 | + $value = parent::validateDefault($value); |
|
68 | 68 | |
69 | - if (preg_match('/' . $this->pattern . '/', $value) !== 1) { |
|
70 | - throw new Exception('Invalid ' . $this->format . ' default value'); |
|
71 | - } |
|
69 | + if (preg_match('/' . $this->pattern . '/', $value) !== 1) { |
|
70 | + throw new Exception('Invalid ' . $this->format . ' default value'); |
|
71 | + } |
|
72 | 72 | |
73 | - return $value; |
|
74 | - } |
|
73 | + return $value; |
|
74 | + } |
|
75 | 75 | |
76 | 76 | } |
@@ -17,98 +17,98 @@ |
||
17 | 17 | class DateType extends AbstractType |
18 | 18 | { |
19 | 19 | |
20 | - /** @noinspection PhpRegExpUnsupportedModifierInspection */ |
|
21 | - const REGEX_DEFAULT = '(?:=(\S+))?'; |
|
22 | - |
|
23 | - /** |
|
24 | - * Map of recognized format names to Swagger formats |
|
25 | - * @var array |
|
26 | - */ |
|
27 | - private static $formats = array( |
|
28 | - 'date' => 'date', |
|
29 | - 'date-time' => 'date-time', |
|
30 | - 'datetime' => 'date-time', |
|
31 | - ); |
|
32 | - private static $datetime_formats = array( |
|
33 | - 'date' => 'Y-m-d', |
|
34 | - 'date-time' => DateTimeInterface::RFC3339, |
|
35 | - ); |
|
36 | - |
|
37 | - /** |
|
38 | - * @var String |
|
39 | - */ |
|
40 | - private $format; |
|
41 | - |
|
42 | - /** |
|
43 | - * @var DateTime |
|
44 | - */ |
|
45 | - private $default = null; |
|
46 | - |
|
47 | - /** |
|
48 | - * @throws Exception |
|
49 | - */ |
|
50 | - protected function parseDefinition($definition) |
|
51 | - { |
|
52 | - $match = array(); |
|
53 | - if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
54 | - throw new Exception("Unparseable date definition: '{$definition}'"); |
|
55 | - } |
|
56 | - |
|
57 | - $type = strtolower($match[1]); |
|
58 | - |
|
59 | - if (!isset(self::$formats[$type])) { |
|
60 | - throw new Exception("Not a date: '{$definition}'"); |
|
61 | - } |
|
62 | - $this->format = self::$formats[$type]; |
|
63 | - |
|
64 | - $this->default = isset($match[2]) && $match[2] !== '' ? $this->validateDefault($match[2]) : null; |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * @param string $command The comment command |
|
69 | - * @param string $data Any data added after the command |
|
70 | - * @return AbstractType|boolean |
|
71 | - * @throws Exception |
|
72 | - * @throws Exception |
|
73 | - */ |
|
74 | - public function handleCommand($command, $data = null) |
|
75 | - { |
|
76 | - if (strtolower($command) === 'default') { |
|
77 | - $this->default = $this->validateDefault($data); |
|
78 | - return $this; |
|
79 | - } |
|
80 | - |
|
81 | - return parent::handleCommand($command, $data); |
|
82 | - } |
|
83 | - |
|
84 | - public function toArray(): array |
|
85 | - { |
|
86 | - return self::arrayFilterNull(array_merge(array( |
|
87 | - 'type' => 'string', |
|
88 | - 'format' => $this->format, |
|
89 | - 'default' => $this->default ? $this->default->format(self::$datetime_formats[$this->format]) : null, |
|
90 | - ), parent::toArray())); |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * @throws Exception |
|
95 | - */ |
|
96 | - private function validateDefault($value) |
|
97 | - { |
|
98 | - if (empty($value)) { |
|
99 | - throw new Exception("Empty date default"); |
|
100 | - } |
|
101 | - |
|
102 | - if (($DateTime = date_create($value)) !== false) { |
|
103 | - return $DateTime; |
|
104 | - } |
|
105 | - |
|
106 | - throw new Exception("Invalid '{$this->format}' default: '{$value}'"); |
|
107 | - } |
|
108 | - |
|
109 | - public function __toString() |
|
110 | - { |
|
111 | - return __CLASS__; |
|
112 | - } |
|
20 | + /** @noinspection PhpRegExpUnsupportedModifierInspection */ |
|
21 | + const REGEX_DEFAULT = '(?:=(\S+))?'; |
|
22 | + |
|
23 | + /** |
|
24 | + * Map of recognized format names to Swagger formats |
|
25 | + * @var array |
|
26 | + */ |
|
27 | + private static $formats = array( |
|
28 | + 'date' => 'date', |
|
29 | + 'date-time' => 'date-time', |
|
30 | + 'datetime' => 'date-time', |
|
31 | + ); |
|
32 | + private static $datetime_formats = array( |
|
33 | + 'date' => 'Y-m-d', |
|
34 | + 'date-time' => DateTimeInterface::RFC3339, |
|
35 | + ); |
|
36 | + |
|
37 | + /** |
|
38 | + * @var String |
|
39 | + */ |
|
40 | + private $format; |
|
41 | + |
|
42 | + /** |
|
43 | + * @var DateTime |
|
44 | + */ |
|
45 | + private $default = null; |
|
46 | + |
|
47 | + /** |
|
48 | + * @throws Exception |
|
49 | + */ |
|
50 | + protected function parseDefinition($definition) |
|
51 | + { |
|
52 | + $match = array(); |
|
53 | + if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
54 | + throw new Exception("Unparseable date definition: '{$definition}'"); |
|
55 | + } |
|
56 | + |
|
57 | + $type = strtolower($match[1]); |
|
58 | + |
|
59 | + if (!isset(self::$formats[$type])) { |
|
60 | + throw new Exception("Not a date: '{$definition}'"); |
|
61 | + } |
|
62 | + $this->format = self::$formats[$type]; |
|
63 | + |
|
64 | + $this->default = isset($match[2]) && $match[2] !== '' ? $this->validateDefault($match[2]) : null; |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * @param string $command The comment command |
|
69 | + * @param string $data Any data added after the command |
|
70 | + * @return AbstractType|boolean |
|
71 | + * @throws Exception |
|
72 | + * @throws Exception |
|
73 | + */ |
|
74 | + public function handleCommand($command, $data = null) |
|
75 | + { |
|
76 | + if (strtolower($command) === 'default') { |
|
77 | + $this->default = $this->validateDefault($data); |
|
78 | + return $this; |
|
79 | + } |
|
80 | + |
|
81 | + return parent::handleCommand($command, $data); |
|
82 | + } |
|
83 | + |
|
84 | + public function toArray(): array |
|
85 | + { |
|
86 | + return self::arrayFilterNull(array_merge(array( |
|
87 | + 'type' => 'string', |
|
88 | + 'format' => $this->format, |
|
89 | + 'default' => $this->default ? $this->default->format(self::$datetime_formats[$this->format]) : null, |
|
90 | + ), parent::toArray())); |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * @throws Exception |
|
95 | + */ |
|
96 | + private function validateDefault($value) |
|
97 | + { |
|
98 | + if (empty($value)) { |
|
99 | + throw new Exception("Empty date default"); |
|
100 | + } |
|
101 | + |
|
102 | + if (($DateTime = date_create($value)) !== false) { |
|
103 | + return $DateTime; |
|
104 | + } |
|
105 | + |
|
106 | + throw new Exception("Invalid '{$this->format}' default: '{$value}'"); |
|
107 | + } |
|
108 | + |
|
109 | + public function __toString() |
|
110 | + { |
|
111 | + return __CLASS__; |
|
112 | + } |
|
113 | 113 | |
114 | 114 | } |
@@ -17,45 +17,45 @@ |
||
17 | 17 | class FileType extends AbstractType |
18 | 18 | { |
19 | 19 | |
20 | - /** |
|
21 | - * @throws Exception |
|
22 | - */ |
|
23 | - protected function parseDefinition($definition) |
|
24 | - { |
|
25 | - $type = strtolower($definition); |
|
26 | - |
|
27 | - if ($type !== 'file') { |
|
28 | - throw new Exception("Not a file: '{$definition}'"); |
|
29 | - } |
|
30 | - |
|
31 | - $parent = $this->getParent(); |
|
32 | - if (!($parent instanceof Parameter) || !$parent->isForm()) { |
|
33 | - throw new Exception("File type '{$definition}' only allowed on form parameter"); |
|
34 | - } |
|
35 | - |
|
36 | - /** @var Operation $parentOperation */ |
|
37 | - $parentOperation = $this->getParentClass(Operation::class); |
|
38 | - $consumes = $parentOperation->getConsumes(); |
|
39 | - if (empty($consumes)) { |
|
40 | - $consumes = $this->getSwagger()->getConsumes(); |
|
41 | - } |
|
42 | - |
|
43 | - $valid_consumes = ((int)in_array('multipart/form-data', $consumes)) + ((int)in_array('application/x-www-form-urlencoded', $consumes)); |
|
44 | - if (empty($consumes) || $valid_consumes !== count($consumes)) { |
|
45 | - throw new Exception("File type '{$definition}' without valid consume"); |
|
46 | - } |
|
47 | - } |
|
48 | - |
|
49 | - public function toArray(): array |
|
50 | - { |
|
51 | - return self::arrayFilterNull(array_merge(array( |
|
52 | - 'type' => 'file', |
|
53 | - ), parent::toArray())); |
|
54 | - } |
|
55 | - |
|
56 | - public function __toString() |
|
57 | - { |
|
58 | - return __CLASS__; |
|
59 | - } |
|
20 | + /** |
|
21 | + * @throws Exception |
|
22 | + */ |
|
23 | + protected function parseDefinition($definition) |
|
24 | + { |
|
25 | + $type = strtolower($definition); |
|
26 | + |
|
27 | + if ($type !== 'file') { |
|
28 | + throw new Exception("Not a file: '{$definition}'"); |
|
29 | + } |
|
30 | + |
|
31 | + $parent = $this->getParent(); |
|
32 | + if (!($parent instanceof Parameter) || !$parent->isForm()) { |
|
33 | + throw new Exception("File type '{$definition}' only allowed on form parameter"); |
|
34 | + } |
|
35 | + |
|
36 | + /** @var Operation $parentOperation */ |
|
37 | + $parentOperation = $this->getParentClass(Operation::class); |
|
38 | + $consumes = $parentOperation->getConsumes(); |
|
39 | + if (empty($consumes)) { |
|
40 | + $consumes = $this->getSwagger()->getConsumes(); |
|
41 | + } |
|
42 | + |
|
43 | + $valid_consumes = ((int)in_array('multipart/form-data', $consumes)) + ((int)in_array('application/x-www-form-urlencoded', $consumes)); |
|
44 | + if (empty($consumes) || $valid_consumes !== count($consumes)) { |
|
45 | + throw new Exception("File type '{$definition}' without valid consume"); |
|
46 | + } |
|
47 | + } |
|
48 | + |
|
49 | + public function toArray(): array |
|
50 | + { |
|
51 | + return self::arrayFilterNull(array_merge(array( |
|
52 | + 'type' => 'file', |
|
53 | + ), parent::toArray())); |
|
54 | + } |
|
55 | + |
|
56 | + public function __toString() |
|
57 | + { |
|
58 | + return __CLASS__; |
|
59 | + } |
|
60 | 60 | |
61 | 61 | } |
@@ -16,182 +16,182 @@ |
||
16 | 16 | class ArrayType extends AbstractType |
17 | 17 | { |
18 | 18 | |
19 | - /** @noinspection PhpRegExpUnsupportedModifierInspection */ |
|
20 | - const REGEX_ARRAY_CONTENT = '(?:(\[)(.*)\])?'; |
|
21 | - |
|
22 | - private static $collectionFormats = array( |
|
23 | - 'array' => 'csv', |
|
24 | - 'csv' => 'csv', |
|
25 | - 'ssv' => 'ssv', |
|
26 | - 'tsv' => 'tsv', |
|
27 | - 'pipes' => 'pipes', |
|
28 | - 'multi' => 'multi', |
|
29 | - ); |
|
30 | - |
|
31 | - /** |
|
32 | - * @var AbstractType |
|
33 | - */ |
|
34 | - private $Items = null; |
|
35 | - private $minItems = null; |
|
36 | - private $maxItems = null; |
|
37 | - private $collectionFormat = null; |
|
38 | - |
|
39 | - /** |
|
40 | - * @throws Exception |
|
41 | - */ |
|
42 | - protected function parseDefinition($definition) |
|
43 | - { |
|
44 | - $definition = self::trim($definition); |
|
45 | - $match = array(); |
|
46 | - if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_CONTENT . self::REGEX_RANGE . self::REGEX_END, $definition, $match) === 1) { |
|
47 | - $match[1] = strtolower($match[1]); |
|
48 | - } elseif (preg_match(self::REGEX_START . self::REGEX_ARRAY_CONTENT . self::REGEX_RANGE . self::REGEX_END, $definition, $match) === 1) { |
|
49 | - $match[1] = 'array'; |
|
50 | - } else { |
|
51 | - throw new Exception('Unparseable array definition: \'' . $definition . '\''); |
|
52 | - } |
|
53 | - |
|
54 | - $this->parseFormat($definition, $match); |
|
55 | - $this->parseItems($definition, $match); |
|
56 | - $this->parseRange($definition, $match); |
|
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * @param string $definition |
|
61 | - * @param string[] $match |
|
62 | - * @throws Exception |
|
63 | - * @throws Exception |
|
64 | - */ |
|
65 | - private function parseFormat($definition, $match) |
|
66 | - { |
|
67 | - $type = strtolower($match[1]); |
|
68 | - if (!isset(self::$collectionFormats[$type])) { |
|
69 | - throw new Exception("Not an array: '{$definition}'"); |
|
70 | - } |
|
71 | - |
|
72 | - if ($type === 'multi') { |
|
73 | - $parent = $this->getParent(); |
|
74 | - if (!($parent instanceof Parameter) || !$parent->isMulti()) { |
|
75 | - throw new Exception("Multi array only allowed on query or form parameter: '{$definition}'"); |
|
76 | - } |
|
77 | - } |
|
78 | - |
|
79 | - $this->collectionFormat = self::$collectionFormats[$type]; |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * @param string $definition |
|
84 | - * @param string[] $match |
|
85 | - * @throws Exception |
|
86 | - */ |
|
87 | - private function parseItems($definition, $match) |
|
88 | - { |
|
89 | - if (!empty($match[2])) { |
|
90 | - $this->Items = $this->validateItems($match[2]); |
|
91 | - } |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * @param string $definition |
|
96 | - * @param string[] $match |
|
97 | - * @throws Exception |
|
98 | - */ |
|
99 | - private function parseRange($definition, $match) |
|
100 | - { |
|
101 | - if (!empty($match[3])) { |
|
102 | - if ($match[4] === '' && $match[5] === '') { |
|
103 | - throw new Exception("Empty array range: '{$definition}'"); |
|
104 | - } |
|
105 | - |
|
106 | - $exclusiveMinimum = $match[3] == '<'; |
|
107 | - $this->minItems = $match[4] === '' ? null : (int)$match[4]; |
|
108 | - $this->maxItems = $match[5] === '' ? null : (int)$match[5]; |
|
109 | - $exclusiveMaximum = isset($match[6]) ? ($match[6] == '>') : null; |
|
110 | - if ($this->minItems && $this->maxItems && $this->minItems > $this->maxItems) { |
|
111 | - self::swap($this->minItems, $this->maxItems); |
|
112 | - self::swap($exclusiveMinimum, $exclusiveMaximum); |
|
113 | - } |
|
114 | - $this->minItems = $this->minItems === null ? null : max(0, $exclusiveMinimum ? $this->minItems + 1 : $this->minItems); |
|
115 | - $this->maxItems = $this->maxItems === null ? null : max(0, $exclusiveMaximum ? $this->maxItems - 1 : $this->maxItems); |
|
116 | - } |
|
117 | - } |
|
118 | - |
|
119 | - /** |
|
120 | - * @param string $command The comment command |
|
121 | - * @param string $data Any data added after the command |
|
122 | - * @return AbstractType|boolean |
|
123 | - * @throws Exception |
|
124 | - * @throws Exception |
|
125 | - * @throws Exception |
|
126 | - * @throws Exception |
|
127 | - * @throws Exception |
|
128 | - * @throws Exception |
|
129 | - * @throws Exception |
|
130 | - */ |
|
131 | - public function handleCommand($command, $data = null) |
|
132 | - { |
|
133 | - if ($this->Items) { |
|
134 | - $return = $this->Items->handleCommand($command, $data); |
|
135 | - if ($return) { |
|
136 | - return $return; |
|
137 | - } |
|
138 | - } |
|
139 | - |
|
140 | - switch (strtolower($command)) { |
|
141 | - case 'min': |
|
142 | - $this->minItems = (int)$data; |
|
143 | - if ($this->minItems < 0) { |
|
144 | - throw new Exception("Minimum less than zero: '{$data}'"); |
|
145 | - } |
|
146 | - if ($this->maxItems !== null && $this->minItems > $this->maxItems) { |
|
147 | - throw new Exception("Minimum greater than maximum: '{$data}'"); |
|
148 | - } |
|
149 | - return $this; |
|
150 | - |
|
151 | - case 'max': |
|
152 | - $this->maxItems = (int)$data; |
|
153 | - if ($this->minItems !== null && $this->minItems > $this->maxItems) { |
|
154 | - throw new Exception("Maximum less than minimum: '{$data}'"); |
|
155 | - } |
|
156 | - if ($this->maxItems < 0) { |
|
157 | - throw new Exception("Maximum less than zero: '{$data}'"); |
|
158 | - } |
|
159 | - return $this; |
|
160 | - |
|
161 | - case 'items': |
|
162 | - $this->Items = $this->validateItems($data); |
|
163 | - return $this->Items; |
|
164 | - } |
|
165 | - |
|
166 | - return parent::handleCommand($command, $data); |
|
167 | - } |
|
168 | - |
|
169 | - public function toArray(): array |
|
170 | - { |
|
171 | - return self::arrayFilterNull(array_merge(array( |
|
172 | - 'type' => 'array', |
|
173 | - 'items' => empty($this->Items) ? null : $this->Items->toArray(), |
|
174 | - 'collectionFormat' => $this->collectionFormat == 'csv' ? null : $this->collectionFormat, |
|
175 | - 'minItems' => $this->minItems, |
|
176 | - 'maxItems' => $this->maxItems, |
|
177 | - ), parent::toArray())); |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * @throws Exception |
|
182 | - */ |
|
183 | - private function validateItems($items) |
|
184 | - { |
|
185 | - if (empty($items)) { |
|
186 | - throw new Exception("Empty items definition: '{$items}'"); |
|
187 | - } |
|
188 | - |
|
189 | - return self::typeFactory($this, $items, "Unparseable items definition: '%s'"); |
|
190 | - } |
|
191 | - |
|
192 | - public function __toString() |
|
193 | - { |
|
194 | - return __CLASS__; |
|
195 | - } |
|
19 | + /** @noinspection PhpRegExpUnsupportedModifierInspection */ |
|
20 | + const REGEX_ARRAY_CONTENT = '(?:(\[)(.*)\])?'; |
|
21 | + |
|
22 | + private static $collectionFormats = array( |
|
23 | + 'array' => 'csv', |
|
24 | + 'csv' => 'csv', |
|
25 | + 'ssv' => 'ssv', |
|
26 | + 'tsv' => 'tsv', |
|
27 | + 'pipes' => 'pipes', |
|
28 | + 'multi' => 'multi', |
|
29 | + ); |
|
30 | + |
|
31 | + /** |
|
32 | + * @var AbstractType |
|
33 | + */ |
|
34 | + private $Items = null; |
|
35 | + private $minItems = null; |
|
36 | + private $maxItems = null; |
|
37 | + private $collectionFormat = null; |
|
38 | + |
|
39 | + /** |
|
40 | + * @throws Exception |
|
41 | + */ |
|
42 | + protected function parseDefinition($definition) |
|
43 | + { |
|
44 | + $definition = self::trim($definition); |
|
45 | + $match = array(); |
|
46 | + if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_CONTENT . self::REGEX_RANGE . self::REGEX_END, $definition, $match) === 1) { |
|
47 | + $match[1] = strtolower($match[1]); |
|
48 | + } elseif (preg_match(self::REGEX_START . self::REGEX_ARRAY_CONTENT . self::REGEX_RANGE . self::REGEX_END, $definition, $match) === 1) { |
|
49 | + $match[1] = 'array'; |
|
50 | + } else { |
|
51 | + throw new Exception('Unparseable array definition: \'' . $definition . '\''); |
|
52 | + } |
|
53 | + |
|
54 | + $this->parseFormat($definition, $match); |
|
55 | + $this->parseItems($definition, $match); |
|
56 | + $this->parseRange($definition, $match); |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * @param string $definition |
|
61 | + * @param string[] $match |
|
62 | + * @throws Exception |
|
63 | + * @throws Exception |
|
64 | + */ |
|
65 | + private function parseFormat($definition, $match) |
|
66 | + { |
|
67 | + $type = strtolower($match[1]); |
|
68 | + if (!isset(self::$collectionFormats[$type])) { |
|
69 | + throw new Exception("Not an array: '{$definition}'"); |
|
70 | + } |
|
71 | + |
|
72 | + if ($type === 'multi') { |
|
73 | + $parent = $this->getParent(); |
|
74 | + if (!($parent instanceof Parameter) || !$parent->isMulti()) { |
|
75 | + throw new Exception("Multi array only allowed on query or form parameter: '{$definition}'"); |
|
76 | + } |
|
77 | + } |
|
78 | + |
|
79 | + $this->collectionFormat = self::$collectionFormats[$type]; |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * @param string $definition |
|
84 | + * @param string[] $match |
|
85 | + * @throws Exception |
|
86 | + */ |
|
87 | + private function parseItems($definition, $match) |
|
88 | + { |
|
89 | + if (!empty($match[2])) { |
|
90 | + $this->Items = $this->validateItems($match[2]); |
|
91 | + } |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * @param string $definition |
|
96 | + * @param string[] $match |
|
97 | + * @throws Exception |
|
98 | + */ |
|
99 | + private function parseRange($definition, $match) |
|
100 | + { |
|
101 | + if (!empty($match[3])) { |
|
102 | + if ($match[4] === '' && $match[5] === '') { |
|
103 | + throw new Exception("Empty array range: '{$definition}'"); |
|
104 | + } |
|
105 | + |
|
106 | + $exclusiveMinimum = $match[3] == '<'; |
|
107 | + $this->minItems = $match[4] === '' ? null : (int)$match[4]; |
|
108 | + $this->maxItems = $match[5] === '' ? null : (int)$match[5]; |
|
109 | + $exclusiveMaximum = isset($match[6]) ? ($match[6] == '>') : null; |
|
110 | + if ($this->minItems && $this->maxItems && $this->minItems > $this->maxItems) { |
|
111 | + self::swap($this->minItems, $this->maxItems); |
|
112 | + self::swap($exclusiveMinimum, $exclusiveMaximum); |
|
113 | + } |
|
114 | + $this->minItems = $this->minItems === null ? null : max(0, $exclusiveMinimum ? $this->minItems + 1 : $this->minItems); |
|
115 | + $this->maxItems = $this->maxItems === null ? null : max(0, $exclusiveMaximum ? $this->maxItems - 1 : $this->maxItems); |
|
116 | + } |
|
117 | + } |
|
118 | + |
|
119 | + /** |
|
120 | + * @param string $command The comment command |
|
121 | + * @param string $data Any data added after the command |
|
122 | + * @return AbstractType|boolean |
|
123 | + * @throws Exception |
|
124 | + * @throws Exception |
|
125 | + * @throws Exception |
|
126 | + * @throws Exception |
|
127 | + * @throws Exception |
|
128 | + * @throws Exception |
|
129 | + * @throws Exception |
|
130 | + */ |
|
131 | + public function handleCommand($command, $data = null) |
|
132 | + { |
|
133 | + if ($this->Items) { |
|
134 | + $return = $this->Items->handleCommand($command, $data); |
|
135 | + if ($return) { |
|
136 | + return $return; |
|
137 | + } |
|
138 | + } |
|
139 | + |
|
140 | + switch (strtolower($command)) { |
|
141 | + case 'min': |
|
142 | + $this->minItems = (int)$data; |
|
143 | + if ($this->minItems < 0) { |
|
144 | + throw new Exception("Minimum less than zero: '{$data}'"); |
|
145 | + } |
|
146 | + if ($this->maxItems !== null && $this->minItems > $this->maxItems) { |
|
147 | + throw new Exception("Minimum greater than maximum: '{$data}'"); |
|
148 | + } |
|
149 | + return $this; |
|
150 | + |
|
151 | + case 'max': |
|
152 | + $this->maxItems = (int)$data; |
|
153 | + if ($this->minItems !== null && $this->minItems > $this->maxItems) { |
|
154 | + throw new Exception("Maximum less than minimum: '{$data}'"); |
|
155 | + } |
|
156 | + if ($this->maxItems < 0) { |
|
157 | + throw new Exception("Maximum less than zero: '{$data}'"); |
|
158 | + } |
|
159 | + return $this; |
|
160 | + |
|
161 | + case 'items': |
|
162 | + $this->Items = $this->validateItems($data); |
|
163 | + return $this->Items; |
|
164 | + } |
|
165 | + |
|
166 | + return parent::handleCommand($command, $data); |
|
167 | + } |
|
168 | + |
|
169 | + public function toArray(): array |
|
170 | + { |
|
171 | + return self::arrayFilterNull(array_merge(array( |
|
172 | + 'type' => 'array', |
|
173 | + 'items' => empty($this->Items) ? null : $this->Items->toArray(), |
|
174 | + 'collectionFormat' => $this->collectionFormat == 'csv' ? null : $this->collectionFormat, |
|
175 | + 'minItems' => $this->minItems, |
|
176 | + 'maxItems' => $this->maxItems, |
|
177 | + ), parent::toArray())); |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * @throws Exception |
|
182 | + */ |
|
183 | + private function validateItems($items) |
|
184 | + { |
|
185 | + if (empty($items)) { |
|
186 | + throw new Exception("Empty items definition: '{$items}'"); |
|
187 | + } |
|
188 | + |
|
189 | + return self::typeFactory($this, $items, "Unparseable items definition: '%s'"); |
|
190 | + } |
|
191 | + |
|
192 | + public function __toString() |
|
193 | + { |
|
194 | + return __CLASS__; |
|
195 | + } |
|
196 | 196 | |
197 | 197 | } |
@@ -104,8 +104,8 @@ discard block |
||
104 | 104 | } |
105 | 105 | |
106 | 106 | $exclusiveMinimum = $match[3] == '<'; |
107 | - $this->minItems = $match[4] === '' ? null : (int)$match[4]; |
|
108 | - $this->maxItems = $match[5] === '' ? null : (int)$match[5]; |
|
107 | + $this->minItems = $match[4] === '' ? null : (int) $match[4]; |
|
108 | + $this->maxItems = $match[5] === '' ? null : (int) $match[5]; |
|
109 | 109 | $exclusiveMaximum = isset($match[6]) ? ($match[6] == '>') : null; |
110 | 110 | if ($this->minItems && $this->maxItems && $this->minItems > $this->maxItems) { |
111 | 111 | self::swap($this->minItems, $this->maxItems); |
@@ -139,7 +139,7 @@ discard block |
||
139 | 139 | |
140 | 140 | switch (strtolower($command)) { |
141 | 141 | case 'min': |
142 | - $this->minItems = (int)$data; |
|
142 | + $this->minItems = (int) $data; |
|
143 | 143 | if ($this->minItems < 0) { |
144 | 144 | throw new Exception("Minimum less than zero: '{$data}'"); |
145 | 145 | } |
@@ -149,7 +149,7 @@ discard block |
||
149 | 149 | return $this; |
150 | 150 | |
151 | 151 | case 'max': |
152 | - $this->maxItems = (int)$data; |
|
152 | + $this->maxItems = (int) $data; |
|
153 | 153 | if ($this->minItems !== null && $this->minItems > $this->maxItems) { |
154 | 154 | throw new Exception("Maximum less than minimum: '{$data}'"); |
155 | 155 | } |
@@ -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 | } |