@@ -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 = []; |
|
37 | - private $multipleOf = null; |
|
38 | - |
|
39 | - /** |
|
40 | - * @param string $command The comment command |
|
41 | - * @param string $data Any data added after the command |
|
42 | - * @return AbstractType|boolean |
|
43 | - * @throws Exception |
|
44 | - * @throws Exception |
|
45 | - * @throws Exception |
|
46 | - */ |
|
47 | - public function handleCommand($command, $data = null) |
|
48 | - { |
|
49 | - switch (strtolower($command)) { |
|
50 | - case 'default': |
|
51 | - $this->default = $this->validateDefault($data); |
|
52 | - return $this; |
|
53 | - |
|
54 | - case 'enum': |
|
55 | - $words = self::wordSplit($data); |
|
56 | - foreach ($words as &$word) { |
|
57 | - $word = $this->validateDefault($word); |
|
58 | - } |
|
59 | - unset($word); |
|
60 | - $this->enum = array_merge($this->enum, $words); |
|
61 | - return $this; |
|
62 | - |
|
63 | - case 'step': |
|
64 | - if (($step = (int)$data) > 0) { |
|
65 | - $this->multipleOf = $step; |
|
66 | - } |
|
67 | - return $this; |
|
68 | - } |
|
69 | - |
|
70 | - return parent::handleCommand($command, $data); |
|
71 | - } |
|
72 | - |
|
73 | - /** |
|
74 | - * @throws Exception |
|
75 | - */ |
|
76 | - private function validateDefault($value) |
|
77 | - { |
|
78 | - if (preg_match('~^-?\d+$~', $value) !== 1) { |
|
79 | - throw new Exception("Invalid integer default: '{$value}'"); |
|
80 | - } |
|
81 | - |
|
82 | - if ($this->maximum) { |
|
83 | - if (($value > $this->maximum) || ($this->exclusiveMaximum && $value == $this->maximum)) { |
|
84 | - throw new Exception("Default integer beyond maximum: '{$value}'"); |
|
85 | - } |
|
86 | - } |
|
87 | - if ($this->minimum) { |
|
88 | - if (($value < $this->minimum) || ($this->exclusiveMinimum && $value == $this->minimum)) { |
|
89 | - throw new Exception("Default integer beyond minimum: '{$value}'"); |
|
90 | - } |
|
91 | - } |
|
92 | - |
|
93 | - return (int)$value; |
|
94 | - } |
|
95 | - |
|
96 | - public function toArray(): array |
|
97 | - { |
|
98 | - return self::arrayFilterNull(array_merge(array( |
|
99 | - 'type' => 'integer', |
|
100 | - 'format' => $this->format, |
|
101 | - 'default' => $this->default, |
|
102 | - 'minimum' => $this->minimum, |
|
103 | - 'exclusiveMinimum' => ($this->exclusiveMinimum && !is_null($this->minimum)) ? true : null, |
|
104 | - 'maximum' => $this->maximum, |
|
105 | - 'exclusiveMaximum' => ($this->exclusiveMaximum && !is_null($this->maximum)) ? true : null, |
|
106 | - 'enum' => $this->enum, |
|
107 | - 'multipleOf' => $this->multipleOf, |
|
108 | - ), parent::toArray())); |
|
109 | - } |
|
110 | - |
|
111 | - public function __toString() |
|
112 | - { |
|
113 | - return __CLASS__; |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * @throws Exception |
|
118 | - */ |
|
119 | - protected function parseDefinition($definition) |
|
120 | - { |
|
121 | - $definition = self::trim($definition); |
|
122 | - |
|
123 | - $match = []; |
|
124 | - if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
125 | - throw new Exception("Unparseable integer definition: '{$definition}'"); |
|
126 | - } |
|
127 | - |
|
128 | - $this->parseFormat($definition, $match); |
|
129 | - $this->parseRange($definition, $match); |
|
130 | - $this->parseDefault($definition, $match); |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * @param string $definition |
|
135 | - * @param string[] $match |
|
136 | - * @throws Exception |
|
137 | - */ |
|
138 | - private function parseFormat($definition, $match) |
|
139 | - { |
|
140 | - $type = strtolower($match[1]); |
|
141 | - if (!isset(self::$formats[$type])) { |
|
142 | - throw new Exception("Not an integer: '{$definition}'"); |
|
143 | - } |
|
144 | - $this->format = self::$formats[$type]; |
|
145 | - } |
|
146 | - |
|
147 | - /** |
|
148 | - * @param string $definition |
|
149 | - * @param string[] $match |
|
150 | - * @throws Exception |
|
151 | - */ |
|
152 | - private function parseRange($definition, $match) |
|
153 | - { |
|
154 | - if (!empty($match[2])) { |
|
155 | - if ($match[3] === '' && $match[4] === '') { |
|
156 | - throw new Exception("Empty integer range: '{$definition}'"); |
|
157 | - } |
|
158 | - |
|
159 | - $this->exclusiveMinimum = $match[2] == '<'; |
|
160 | - $this->minimum = $match[3] === '' ? null : (int)$match[3]; |
|
161 | - $this->maximum = $match[4] === '' ? null : (int)$match[4]; |
|
162 | - $this->exclusiveMaximum = isset($match[5]) ? ($match[5] == '>') : null; |
|
163 | - if ($this->minimum && $this->maximum && $this->minimum > $this->maximum) { |
|
164 | - self::swap($this->minimum, $this->maximum); |
|
165 | - self::swap($this->exclusiveMinimum, $this->exclusiveMaximum); |
|
166 | - } |
|
167 | - } |
|
168 | - } |
|
169 | - |
|
170 | - /** |
|
171 | - * @param string $definition |
|
172 | - * @param string[] $match |
|
173 | - * @throws Exception |
|
174 | - */ |
|
175 | - private function parseDefault($definition, $match) |
|
176 | - { |
|
177 | - $this->default = isset($match[6]) && $match[6] !== '' ? $this->validateDefault($match[6]) : null; |
|
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 = []; |
|
37 | + private $multipleOf = null; |
|
38 | + |
|
39 | + /** |
|
40 | + * @param string $command The comment command |
|
41 | + * @param string $data Any data added after the command |
|
42 | + * @return AbstractType|boolean |
|
43 | + * @throws Exception |
|
44 | + * @throws Exception |
|
45 | + * @throws Exception |
|
46 | + */ |
|
47 | + public function handleCommand($command, $data = null) |
|
48 | + { |
|
49 | + switch (strtolower($command)) { |
|
50 | + case 'default': |
|
51 | + $this->default = $this->validateDefault($data); |
|
52 | + return $this; |
|
53 | + |
|
54 | + case 'enum': |
|
55 | + $words = self::wordSplit($data); |
|
56 | + foreach ($words as &$word) { |
|
57 | + $word = $this->validateDefault($word); |
|
58 | + } |
|
59 | + unset($word); |
|
60 | + $this->enum = array_merge($this->enum, $words); |
|
61 | + return $this; |
|
62 | + |
|
63 | + case 'step': |
|
64 | + if (($step = (int)$data) > 0) { |
|
65 | + $this->multipleOf = $step; |
|
66 | + } |
|
67 | + return $this; |
|
68 | + } |
|
69 | + |
|
70 | + return parent::handleCommand($command, $data); |
|
71 | + } |
|
72 | + |
|
73 | + /** |
|
74 | + * @throws Exception |
|
75 | + */ |
|
76 | + private function validateDefault($value) |
|
77 | + { |
|
78 | + if (preg_match('~^-?\d+$~', $value) !== 1) { |
|
79 | + throw new Exception("Invalid integer default: '{$value}'"); |
|
80 | + } |
|
81 | + |
|
82 | + if ($this->maximum) { |
|
83 | + if (($value > $this->maximum) || ($this->exclusiveMaximum && $value == $this->maximum)) { |
|
84 | + throw new Exception("Default integer beyond maximum: '{$value}'"); |
|
85 | + } |
|
86 | + } |
|
87 | + if ($this->minimum) { |
|
88 | + if (($value < $this->minimum) || ($this->exclusiveMinimum && $value == $this->minimum)) { |
|
89 | + throw new Exception("Default integer beyond minimum: '{$value}'"); |
|
90 | + } |
|
91 | + } |
|
92 | + |
|
93 | + return (int)$value; |
|
94 | + } |
|
95 | + |
|
96 | + public function toArray(): array |
|
97 | + { |
|
98 | + return self::arrayFilterNull(array_merge(array( |
|
99 | + 'type' => 'integer', |
|
100 | + 'format' => $this->format, |
|
101 | + 'default' => $this->default, |
|
102 | + 'minimum' => $this->minimum, |
|
103 | + 'exclusiveMinimum' => ($this->exclusiveMinimum && !is_null($this->minimum)) ? true : null, |
|
104 | + 'maximum' => $this->maximum, |
|
105 | + 'exclusiveMaximum' => ($this->exclusiveMaximum && !is_null($this->maximum)) ? true : null, |
|
106 | + 'enum' => $this->enum, |
|
107 | + 'multipleOf' => $this->multipleOf, |
|
108 | + ), parent::toArray())); |
|
109 | + } |
|
110 | + |
|
111 | + public function __toString() |
|
112 | + { |
|
113 | + return __CLASS__; |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * @throws Exception |
|
118 | + */ |
|
119 | + protected function parseDefinition($definition) |
|
120 | + { |
|
121 | + $definition = self::trim($definition); |
|
122 | + |
|
123 | + $match = []; |
|
124 | + if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
125 | + throw new Exception("Unparseable integer definition: '{$definition}'"); |
|
126 | + } |
|
127 | + |
|
128 | + $this->parseFormat($definition, $match); |
|
129 | + $this->parseRange($definition, $match); |
|
130 | + $this->parseDefault($definition, $match); |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * @param string $definition |
|
135 | + * @param string[] $match |
|
136 | + * @throws Exception |
|
137 | + */ |
|
138 | + private function parseFormat($definition, $match) |
|
139 | + { |
|
140 | + $type = strtolower($match[1]); |
|
141 | + if (!isset(self::$formats[$type])) { |
|
142 | + throw new Exception("Not an integer: '{$definition}'"); |
|
143 | + } |
|
144 | + $this->format = self::$formats[$type]; |
|
145 | + } |
|
146 | + |
|
147 | + /** |
|
148 | + * @param string $definition |
|
149 | + * @param string[] $match |
|
150 | + * @throws Exception |
|
151 | + */ |
|
152 | + private function parseRange($definition, $match) |
|
153 | + { |
|
154 | + if (!empty($match[2])) { |
|
155 | + if ($match[3] === '' && $match[4] === '') { |
|
156 | + throw new Exception("Empty integer range: '{$definition}'"); |
|
157 | + } |
|
158 | + |
|
159 | + $this->exclusiveMinimum = $match[2] == '<'; |
|
160 | + $this->minimum = $match[3] === '' ? null : (int)$match[3]; |
|
161 | + $this->maximum = $match[4] === '' ? null : (int)$match[4]; |
|
162 | + $this->exclusiveMaximum = isset($match[5]) ? ($match[5] == '>') : null; |
|
163 | + if ($this->minimum && $this->maximum && $this->minimum > $this->maximum) { |
|
164 | + self::swap($this->minimum, $this->maximum); |
|
165 | + self::swap($this->exclusiveMinimum, $this->exclusiveMaximum); |
|
166 | + } |
|
167 | + } |
|
168 | + } |
|
169 | + |
|
170 | + /** |
|
171 | + * @param string $definition |
|
172 | + * @param string[] $match |
|
173 | + * @throws Exception |
|
174 | + */ |
|
175 | + private function parseDefault($definition, $match) |
|
176 | + { |
|
177 | + $this->default = isset($match[6]) && $match[6] !== '' ? $this->validateDefault($match[6]) : null; |
|
178 | + } |
|
179 | 179 | |
180 | 180 | } |
@@ -61,7 +61,7 @@ discard block |
||
61 | 61 | return $this; |
62 | 62 | |
63 | 63 | case 'step': |
64 | - if (($step = (int)$data) > 0) { |
|
64 | + if (($step = (int) $data) > 0) { |
|
65 | 65 | $this->multipleOf = $step; |
66 | 66 | } |
67 | 67 | return $this; |
@@ -90,7 +90,7 @@ discard block |
||
90 | 90 | } |
91 | 91 | } |
92 | 92 | |
93 | - return (int)$value; |
|
93 | + return (int) $value; |
|
94 | 94 | } |
95 | 95 | |
96 | 96 | public function toArray(): array |
@@ -157,8 +157,8 @@ discard block |
||
157 | 157 | } |
158 | 158 | |
159 | 159 | $this->exclusiveMinimum = $match[2] == '<'; |
160 | - $this->minimum = $match[3] === '' ? null : (int)$match[3]; |
|
161 | - $this->maximum = $match[4] === '' ? null : (int)$match[4]; |
|
160 | + $this->minimum = $match[3] === '' ? null : (int) $match[3]; |
|
161 | + $this->maximum = $match[4] === '' ? null : (int) $match[4]; |
|
162 | 162 | $this->exclusiveMaximum = isset($match[5]) ? ($match[5] == '>') : null; |
163 | 163 | if ($this->minimum && $this->maximum && $this->minimum > $this->maximum) { |
164 | 164 | self::swap($this->minimum, $this->maximum); |
@@ -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 = []; |
|
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 = []; |
|
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 | } |
@@ -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 = []; |
|
34 | - private $multipleOf = null; |
|
35 | - |
|
36 | - /** |
|
37 | - * @param string $command The comment command |
|
38 | - * @param string $data Any data added after the command |
|
39 | - * @return AbstractType|boolean |
|
40 | - * @throws Exception |
|
41 | - * @throws Exception |
|
42 | - * @throws Exception |
|
43 | - */ |
|
44 | - public function handleCommand($command, $data = null) |
|
45 | - { |
|
46 | - switch (strtolower($command)) { |
|
47 | - case 'default': |
|
48 | - $this->default = $this->validateDefault($data); |
|
49 | - return $this; |
|
50 | - |
|
51 | - case 'enum': |
|
52 | - $words = self::wordSplit($data); |
|
53 | - foreach ($words as &$word) { |
|
54 | - $word = $this->validateDefault($word); |
|
55 | - } |
|
56 | - unset($word); |
|
57 | - $this->enum = array_merge($this->enum, $words); |
|
58 | - return $this; |
|
59 | - |
|
60 | - case 'step': |
|
61 | - if (($step = (float)$data) > 0) { |
|
62 | - $this->multipleOf = $step; |
|
63 | - } |
|
64 | - return $this; |
|
65 | - } |
|
66 | - |
|
67 | - return parent::handleCommand($command, $data); |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * @throws Exception |
|
72 | - */ |
|
73 | - private function validateDefault($value) |
|
74 | - { |
|
75 | - if (preg_match('~^-?(?:\\d*\\.?\\d+|\\d+\\.\\d*)$~', $value) !== 1) { |
|
76 | - throw new Exception("Invalid number default: '{$value}'"); |
|
77 | - } |
|
78 | - |
|
79 | - if ($this->maximum) { |
|
80 | - if (($value > $this->maximum) || ($this->exclusiveMaximum && $value == $this->maximum)) { |
|
81 | - throw new Exception("Default number beyond maximum: '{$value}'"); |
|
82 | - } |
|
83 | - } |
|
84 | - if ($this->minimum) { |
|
85 | - if (($value < $this->minimum) || ($this->exclusiveMinimum && $value == $this->minimum)) { |
|
86 | - throw new Exception("Default number beyond minimum: '{$value}'"); |
|
87 | - } |
|
88 | - } |
|
89 | - |
|
90 | - return (float)$value; |
|
91 | - } |
|
92 | - |
|
93 | - public function toArray(): array |
|
94 | - { |
|
95 | - return self::arrayFilterNull(array_merge(array( |
|
96 | - 'type' => 'number', |
|
97 | - 'format' => $this->format, |
|
98 | - 'default' => $this->default, |
|
99 | - 'minimum' => $this->minimum, |
|
100 | - 'exclusiveMinimum' => ($this->exclusiveMinimum && !is_null($this->minimum)) ? true : null, |
|
101 | - 'maximum' => $this->maximum, |
|
102 | - 'exclusiveMaximum' => ($this->exclusiveMaximum && !is_null($this->maximum)) ? true : null, |
|
103 | - 'enum' => $this->enum, |
|
104 | - 'multipleOf' => $this->multipleOf, |
|
105 | - ), parent::toArray())); |
|
106 | - } |
|
107 | - |
|
108 | - public function __toString() |
|
109 | - { |
|
110 | - return __CLASS__; |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * @throws Exception |
|
115 | - */ |
|
116 | - protected function parseDefinition($definition) |
|
117 | - { |
|
118 | - $match = []; |
|
119 | - if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
120 | - throw new Exception("Unparseable number definition: '{$definition}'"); |
|
121 | - } |
|
122 | - |
|
123 | - $this->parseFormat($definition, $match); |
|
124 | - $this->parseRange($definition, $match); |
|
125 | - $this->parseDefault($definition, $match); |
|
126 | - } |
|
127 | - |
|
128 | - /** |
|
129 | - * @param string[] $match |
|
130 | - * @throws Exception |
|
131 | - */ |
|
132 | - private function parseFormat($definition, $match) |
|
133 | - { |
|
134 | - if (!isset(self::$formats[strtolower($match[1])])) { |
|
135 | - throw new Exception("Not a number: '{$definition}'"); |
|
136 | - } |
|
137 | - $this->format = self::$formats[strtolower($match[1])]; |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * @param string[] $match |
|
142 | - * @throws Exception |
|
143 | - */ |
|
144 | - private function parseRange($definition, $match) |
|
145 | - { |
|
146 | - if (!empty($match[2])) { |
|
147 | - if ($match[3] === '' && $match[4] === '') { |
|
148 | - throw new Exception("Empty number range: '{$definition}'"); |
|
149 | - } |
|
150 | - |
|
151 | - $this->exclusiveMinimum = $match[2] == '<'; |
|
152 | - $this->minimum = $match[3] === '' ? null : (float)$match[3]; |
|
153 | - $this->maximum = $match[4] === '' ? null : (float)$match[4]; |
|
154 | - $this->exclusiveMaximum = isset($match[5]) ? ($match[5] == '>') : null; |
|
155 | - if ($this->minimum && $this->maximum && $this->minimum > $this->maximum) { |
|
156 | - self::swap($this->minimum, $this->maximum); |
|
157 | - self::swap($this->exclusiveMinimum, $this->exclusiveMaximum); |
|
158 | - } |
|
159 | - } |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * @param string[] $match |
|
164 | - * @throws Exception |
|
165 | - */ |
|
166 | - private function parseDefault($definition, $match) |
|
167 | - { |
|
168 | - $this->default = isset($match[6]) && $match[6] !== '' ? $this->validateDefault($match[6]) : null; |
|
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 = []; |
|
34 | + private $multipleOf = null; |
|
35 | + |
|
36 | + /** |
|
37 | + * @param string $command The comment command |
|
38 | + * @param string $data Any data added after the command |
|
39 | + * @return AbstractType|boolean |
|
40 | + * @throws Exception |
|
41 | + * @throws Exception |
|
42 | + * @throws Exception |
|
43 | + */ |
|
44 | + public function handleCommand($command, $data = null) |
|
45 | + { |
|
46 | + switch (strtolower($command)) { |
|
47 | + case 'default': |
|
48 | + $this->default = $this->validateDefault($data); |
|
49 | + return $this; |
|
50 | + |
|
51 | + case 'enum': |
|
52 | + $words = self::wordSplit($data); |
|
53 | + foreach ($words as &$word) { |
|
54 | + $word = $this->validateDefault($word); |
|
55 | + } |
|
56 | + unset($word); |
|
57 | + $this->enum = array_merge($this->enum, $words); |
|
58 | + return $this; |
|
59 | + |
|
60 | + case 'step': |
|
61 | + if (($step = (float)$data) > 0) { |
|
62 | + $this->multipleOf = $step; |
|
63 | + } |
|
64 | + return $this; |
|
65 | + } |
|
66 | + |
|
67 | + return parent::handleCommand($command, $data); |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * @throws Exception |
|
72 | + */ |
|
73 | + private function validateDefault($value) |
|
74 | + { |
|
75 | + if (preg_match('~^-?(?:\\d*\\.?\\d+|\\d+\\.\\d*)$~', $value) !== 1) { |
|
76 | + throw new Exception("Invalid number default: '{$value}'"); |
|
77 | + } |
|
78 | + |
|
79 | + if ($this->maximum) { |
|
80 | + if (($value > $this->maximum) || ($this->exclusiveMaximum && $value == $this->maximum)) { |
|
81 | + throw new Exception("Default number beyond maximum: '{$value}'"); |
|
82 | + } |
|
83 | + } |
|
84 | + if ($this->minimum) { |
|
85 | + if (($value < $this->minimum) || ($this->exclusiveMinimum && $value == $this->minimum)) { |
|
86 | + throw new Exception("Default number beyond minimum: '{$value}'"); |
|
87 | + } |
|
88 | + } |
|
89 | + |
|
90 | + return (float)$value; |
|
91 | + } |
|
92 | + |
|
93 | + public function toArray(): array |
|
94 | + { |
|
95 | + return self::arrayFilterNull(array_merge(array( |
|
96 | + 'type' => 'number', |
|
97 | + 'format' => $this->format, |
|
98 | + 'default' => $this->default, |
|
99 | + 'minimum' => $this->minimum, |
|
100 | + 'exclusiveMinimum' => ($this->exclusiveMinimum && !is_null($this->minimum)) ? true : null, |
|
101 | + 'maximum' => $this->maximum, |
|
102 | + 'exclusiveMaximum' => ($this->exclusiveMaximum && !is_null($this->maximum)) ? true : null, |
|
103 | + 'enum' => $this->enum, |
|
104 | + 'multipleOf' => $this->multipleOf, |
|
105 | + ), parent::toArray())); |
|
106 | + } |
|
107 | + |
|
108 | + public function __toString() |
|
109 | + { |
|
110 | + return __CLASS__; |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * @throws Exception |
|
115 | + */ |
|
116 | + protected function parseDefinition($definition) |
|
117 | + { |
|
118 | + $match = []; |
|
119 | + if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
120 | + throw new Exception("Unparseable number definition: '{$definition}'"); |
|
121 | + } |
|
122 | + |
|
123 | + $this->parseFormat($definition, $match); |
|
124 | + $this->parseRange($definition, $match); |
|
125 | + $this->parseDefault($definition, $match); |
|
126 | + } |
|
127 | + |
|
128 | + /** |
|
129 | + * @param string[] $match |
|
130 | + * @throws Exception |
|
131 | + */ |
|
132 | + private function parseFormat($definition, $match) |
|
133 | + { |
|
134 | + if (!isset(self::$formats[strtolower($match[1])])) { |
|
135 | + throw new Exception("Not a number: '{$definition}'"); |
|
136 | + } |
|
137 | + $this->format = self::$formats[strtolower($match[1])]; |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * @param string[] $match |
|
142 | + * @throws Exception |
|
143 | + */ |
|
144 | + private function parseRange($definition, $match) |
|
145 | + { |
|
146 | + if (!empty($match[2])) { |
|
147 | + if ($match[3] === '' && $match[4] === '') { |
|
148 | + throw new Exception("Empty number range: '{$definition}'"); |
|
149 | + } |
|
150 | + |
|
151 | + $this->exclusiveMinimum = $match[2] == '<'; |
|
152 | + $this->minimum = $match[3] === '' ? null : (float)$match[3]; |
|
153 | + $this->maximum = $match[4] === '' ? null : (float)$match[4]; |
|
154 | + $this->exclusiveMaximum = isset($match[5]) ? ($match[5] == '>') : null; |
|
155 | + if ($this->minimum && $this->maximum && $this->minimum > $this->maximum) { |
|
156 | + self::swap($this->minimum, $this->maximum); |
|
157 | + self::swap($this->exclusiveMinimum, $this->exclusiveMaximum); |
|
158 | + } |
|
159 | + } |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * @param string[] $match |
|
164 | + * @throws Exception |
|
165 | + */ |
|
166 | + private function parseDefault($definition, $match) |
|
167 | + { |
|
168 | + $this->default = isset($match[6]) && $match[6] !== '' ? $this->validateDefault($match[6]) : null; |
|
169 | + } |
|
170 | 170 | |
171 | 171 | } |
@@ -58,7 +58,7 @@ discard block |
||
58 | 58 | return $this; |
59 | 59 | |
60 | 60 | case 'step': |
61 | - if (($step = (float)$data) > 0) { |
|
61 | + if (($step = (float) $data) > 0) { |
|
62 | 62 | $this->multipleOf = $step; |
63 | 63 | } |
64 | 64 | return $this; |
@@ -87,7 +87,7 @@ discard block |
||
87 | 87 | } |
88 | 88 | } |
89 | 89 | |
90 | - return (float)$value; |
|
90 | + return (float) $value; |
|
91 | 91 | } |
92 | 92 | |
93 | 93 | public function toArray(): array |
@@ -149,8 +149,8 @@ discard block |
||
149 | 149 | } |
150 | 150 | |
151 | 151 | $this->exclusiveMinimum = $match[2] == '<'; |
152 | - $this->minimum = $match[3] === '' ? null : (float)$match[3]; |
|
153 | - $this->maximum = $match[4] === '' ? null : (float)$match[4]; |
|
152 | + $this->minimum = $match[3] === '' ? null : (float) $match[3]; |
|
153 | + $this->maximum = $match[4] === '' ? null : (float) $match[4]; |
|
154 | 154 | $this->exclusiveMaximum = isset($match[5]) ? ($match[5] == '>') : null; |
155 | 155 | if ($this->minimum && $this->maximum && $this->minimum > $this->maximum) { |
156 | 156 | self::swap($this->minimum, $this->maximum); |
@@ -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 | - * @param string $command The comment command |
|
41 | - * @param string $data Any data added after the command |
|
42 | - * @return AbstractType|boolean |
|
43 | - * @throws Exception |
|
44 | - * @throws Exception |
|
45 | - * @throws Exception |
|
46 | - * @throws Exception |
|
47 | - * @throws Exception |
|
48 | - * @throws Exception |
|
49 | - * @throws Exception |
|
50 | - */ |
|
51 | - public function handleCommand($command, $data = null) |
|
52 | - { |
|
53 | - if ($this->Items) { |
|
54 | - $return = $this->Items->handleCommand($command, $data); |
|
55 | - if ($return) { |
|
56 | - return $return; |
|
57 | - } |
|
58 | - } |
|
59 | - |
|
60 | - switch (strtolower($command)) { |
|
61 | - case 'min': |
|
62 | - $this->minItems = (int)$data; |
|
63 | - if ($this->minItems < 0) { |
|
64 | - throw new Exception("Minimum less than zero: '{$data}'"); |
|
65 | - } |
|
66 | - if ($this->maxItems !== null && $this->minItems > $this->maxItems) { |
|
67 | - throw new Exception("Minimum greater than maximum: '{$data}'"); |
|
68 | - } |
|
69 | - return $this; |
|
70 | - |
|
71 | - case 'max': |
|
72 | - $this->maxItems = (int)$data; |
|
73 | - if ($this->minItems !== null && $this->minItems > $this->maxItems) { |
|
74 | - throw new Exception("Maximum less than minimum: '{$data}'"); |
|
75 | - } |
|
76 | - if ($this->maxItems < 0) { |
|
77 | - throw new Exception("Maximum less than zero: '{$data}'"); |
|
78 | - } |
|
79 | - return $this; |
|
80 | - |
|
81 | - case 'items': |
|
82 | - $this->Items = $this->validateItems($data); |
|
83 | - return $this->Items; |
|
84 | - } |
|
85 | - |
|
86 | - return parent::handleCommand($command, $data); |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * @throws Exception |
|
91 | - */ |
|
92 | - private function validateItems($items) |
|
93 | - { |
|
94 | - if (empty($items)) { |
|
95 | - throw new Exception("Empty items definition: '{$items}'"); |
|
96 | - } |
|
97 | - |
|
98 | - return self::typeFactory($this, $items, "Unparseable items definition: '%s'"); |
|
99 | - } |
|
100 | - |
|
101 | - public function toArray(): array |
|
102 | - { |
|
103 | - return self::arrayFilterNull(array_merge(array( |
|
104 | - 'type' => 'array', |
|
105 | - 'items' => empty($this->Items) ? null : $this->Items->toArray(), |
|
106 | - 'collectionFormat' => $this->collectionFormat == 'csv' ? null : $this->collectionFormat, |
|
107 | - 'minItems' => $this->minItems, |
|
108 | - 'maxItems' => $this->maxItems, |
|
109 | - ), parent::toArray())); |
|
110 | - } |
|
111 | - |
|
112 | - public function __toString() |
|
113 | - { |
|
114 | - return __CLASS__; |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * @throws Exception |
|
119 | - */ |
|
120 | - protected function parseDefinition($definition) |
|
121 | - { |
|
122 | - $definition = self::trim($definition); |
|
123 | - $match = []; |
|
124 | - if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_CONTENT . self::REGEX_RANGE . self::REGEX_END, $definition, $match) === 1) { |
|
125 | - $match[1] = strtolower($match[1]); |
|
126 | - } elseif (preg_match(self::REGEX_START . self::REGEX_ARRAY_CONTENT . self::REGEX_RANGE . self::REGEX_END, $definition, $match) === 1) { |
|
127 | - $match[1] = 'array'; |
|
128 | - } else { |
|
129 | - throw new Exception('Unparseable array definition: \'' . $definition . '\''); |
|
130 | - } |
|
131 | - |
|
132 | - $this->parseFormat($definition, $match); |
|
133 | - $this->parseItems($definition, $match); |
|
134 | - $this->parseRange($definition, $match); |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * @param string $definition |
|
139 | - * @param string[] $match |
|
140 | - * @throws Exception |
|
141 | - * @throws Exception |
|
142 | - */ |
|
143 | - private function parseFormat($definition, $match) |
|
144 | - { |
|
145 | - $type = strtolower($match[1]); |
|
146 | - if (!isset(self::$collectionFormats[$type])) { |
|
147 | - throw new Exception("Not an array: '{$definition}'"); |
|
148 | - } |
|
149 | - |
|
150 | - if ($type === 'multi') { |
|
151 | - $parent = $this->getParent(); |
|
152 | - if (!($parent instanceof Parameter) || !$parent->isMulti()) { |
|
153 | - throw new Exception("Multi array only allowed on query or form parameter: '{$definition}'"); |
|
154 | - } |
|
155 | - } |
|
156 | - |
|
157 | - $this->collectionFormat = self::$collectionFormats[$type]; |
|
158 | - } |
|
159 | - |
|
160 | - /** |
|
161 | - * @param string $definition |
|
162 | - * @param string[] $match |
|
163 | - * @throws Exception |
|
164 | - */ |
|
165 | - private function parseItems($definition, $match) |
|
166 | - { |
|
167 | - if (!empty($match[2])) { |
|
168 | - $this->Items = $this->validateItems($match[2]); |
|
169 | - } |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * @param string $definition |
|
174 | - * @param string[] $match |
|
175 | - * @throws Exception |
|
176 | - */ |
|
177 | - private function parseRange($definition, $match) |
|
178 | - { |
|
179 | - if (!empty($match[3])) { |
|
180 | - if ($match[4] === '' && $match[5] === '') { |
|
181 | - throw new Exception("Empty array range: '{$definition}'"); |
|
182 | - } |
|
183 | - |
|
184 | - $exclusiveMinimum = $match[3] == '<'; |
|
185 | - $this->minItems = $match[4] === '' ? null : (int)$match[4]; |
|
186 | - $this->maxItems = $match[5] === '' ? null : (int)$match[5]; |
|
187 | - $exclusiveMaximum = isset($match[6]) ? ($match[6] == '>') : null; |
|
188 | - if ($this->minItems && $this->maxItems && $this->minItems > $this->maxItems) { |
|
189 | - self::swap($this->minItems, $this->maxItems); |
|
190 | - self::swap($exclusiveMinimum, $exclusiveMaximum); |
|
191 | - } |
|
192 | - $this->minItems = $this->minItems === null ? null : max(0, $exclusiveMinimum ? $this->minItems + 1 : $this->minItems); |
|
193 | - $this->maxItems = $this->maxItems === null ? null : max(0, $exclusiveMaximum ? $this->maxItems - 1 : $this->maxItems); |
|
194 | - } |
|
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 | + * @param string $command The comment command |
|
41 | + * @param string $data Any data added after the command |
|
42 | + * @return AbstractType|boolean |
|
43 | + * @throws Exception |
|
44 | + * @throws Exception |
|
45 | + * @throws Exception |
|
46 | + * @throws Exception |
|
47 | + * @throws Exception |
|
48 | + * @throws Exception |
|
49 | + * @throws Exception |
|
50 | + */ |
|
51 | + public function handleCommand($command, $data = null) |
|
52 | + { |
|
53 | + if ($this->Items) { |
|
54 | + $return = $this->Items->handleCommand($command, $data); |
|
55 | + if ($return) { |
|
56 | + return $return; |
|
57 | + } |
|
58 | + } |
|
59 | + |
|
60 | + switch (strtolower($command)) { |
|
61 | + case 'min': |
|
62 | + $this->minItems = (int)$data; |
|
63 | + if ($this->minItems < 0) { |
|
64 | + throw new Exception("Minimum less than zero: '{$data}'"); |
|
65 | + } |
|
66 | + if ($this->maxItems !== null && $this->minItems > $this->maxItems) { |
|
67 | + throw new Exception("Minimum greater than maximum: '{$data}'"); |
|
68 | + } |
|
69 | + return $this; |
|
70 | + |
|
71 | + case 'max': |
|
72 | + $this->maxItems = (int)$data; |
|
73 | + if ($this->minItems !== null && $this->minItems > $this->maxItems) { |
|
74 | + throw new Exception("Maximum less than minimum: '{$data}'"); |
|
75 | + } |
|
76 | + if ($this->maxItems < 0) { |
|
77 | + throw new Exception("Maximum less than zero: '{$data}'"); |
|
78 | + } |
|
79 | + return $this; |
|
80 | + |
|
81 | + case 'items': |
|
82 | + $this->Items = $this->validateItems($data); |
|
83 | + return $this->Items; |
|
84 | + } |
|
85 | + |
|
86 | + return parent::handleCommand($command, $data); |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * @throws Exception |
|
91 | + */ |
|
92 | + private function validateItems($items) |
|
93 | + { |
|
94 | + if (empty($items)) { |
|
95 | + throw new Exception("Empty items definition: '{$items}'"); |
|
96 | + } |
|
97 | + |
|
98 | + return self::typeFactory($this, $items, "Unparseable items definition: '%s'"); |
|
99 | + } |
|
100 | + |
|
101 | + public function toArray(): array |
|
102 | + { |
|
103 | + return self::arrayFilterNull(array_merge(array( |
|
104 | + 'type' => 'array', |
|
105 | + 'items' => empty($this->Items) ? null : $this->Items->toArray(), |
|
106 | + 'collectionFormat' => $this->collectionFormat == 'csv' ? null : $this->collectionFormat, |
|
107 | + 'minItems' => $this->minItems, |
|
108 | + 'maxItems' => $this->maxItems, |
|
109 | + ), parent::toArray())); |
|
110 | + } |
|
111 | + |
|
112 | + public function __toString() |
|
113 | + { |
|
114 | + return __CLASS__; |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * @throws Exception |
|
119 | + */ |
|
120 | + protected function parseDefinition($definition) |
|
121 | + { |
|
122 | + $definition = self::trim($definition); |
|
123 | + $match = []; |
|
124 | + if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_CONTENT . self::REGEX_RANGE . self::REGEX_END, $definition, $match) === 1) { |
|
125 | + $match[1] = strtolower($match[1]); |
|
126 | + } elseif (preg_match(self::REGEX_START . self::REGEX_ARRAY_CONTENT . self::REGEX_RANGE . self::REGEX_END, $definition, $match) === 1) { |
|
127 | + $match[1] = 'array'; |
|
128 | + } else { |
|
129 | + throw new Exception('Unparseable array definition: \'' . $definition . '\''); |
|
130 | + } |
|
131 | + |
|
132 | + $this->parseFormat($definition, $match); |
|
133 | + $this->parseItems($definition, $match); |
|
134 | + $this->parseRange($definition, $match); |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * @param string $definition |
|
139 | + * @param string[] $match |
|
140 | + * @throws Exception |
|
141 | + * @throws Exception |
|
142 | + */ |
|
143 | + private function parseFormat($definition, $match) |
|
144 | + { |
|
145 | + $type = strtolower($match[1]); |
|
146 | + if (!isset(self::$collectionFormats[$type])) { |
|
147 | + throw new Exception("Not an array: '{$definition}'"); |
|
148 | + } |
|
149 | + |
|
150 | + if ($type === 'multi') { |
|
151 | + $parent = $this->getParent(); |
|
152 | + if (!($parent instanceof Parameter) || !$parent->isMulti()) { |
|
153 | + throw new Exception("Multi array only allowed on query or form parameter: '{$definition}'"); |
|
154 | + } |
|
155 | + } |
|
156 | + |
|
157 | + $this->collectionFormat = self::$collectionFormats[$type]; |
|
158 | + } |
|
159 | + |
|
160 | + /** |
|
161 | + * @param string $definition |
|
162 | + * @param string[] $match |
|
163 | + * @throws Exception |
|
164 | + */ |
|
165 | + private function parseItems($definition, $match) |
|
166 | + { |
|
167 | + if (!empty($match[2])) { |
|
168 | + $this->Items = $this->validateItems($match[2]); |
|
169 | + } |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * @param string $definition |
|
174 | + * @param string[] $match |
|
175 | + * @throws Exception |
|
176 | + */ |
|
177 | + private function parseRange($definition, $match) |
|
178 | + { |
|
179 | + if (!empty($match[3])) { |
|
180 | + if ($match[4] === '' && $match[5] === '') { |
|
181 | + throw new Exception("Empty array range: '{$definition}'"); |
|
182 | + } |
|
183 | + |
|
184 | + $exclusiveMinimum = $match[3] == '<'; |
|
185 | + $this->minItems = $match[4] === '' ? null : (int)$match[4]; |
|
186 | + $this->maxItems = $match[5] === '' ? null : (int)$match[5]; |
|
187 | + $exclusiveMaximum = isset($match[6]) ? ($match[6] == '>') : null; |
|
188 | + if ($this->minItems && $this->maxItems && $this->minItems > $this->maxItems) { |
|
189 | + self::swap($this->minItems, $this->maxItems); |
|
190 | + self::swap($exclusiveMinimum, $exclusiveMaximum); |
|
191 | + } |
|
192 | + $this->minItems = $this->minItems === null ? null : max(0, $exclusiveMinimum ? $this->minItems + 1 : $this->minItems); |
|
193 | + $this->maxItems = $this->maxItems === null ? null : max(0, $exclusiveMaximum ? $this->maxItems - 1 : $this->maxItems); |
|
194 | + } |
|
195 | + } |
|
196 | 196 | |
197 | 197 | } |
@@ -59,7 +59,7 @@ discard block |
||
59 | 59 | |
60 | 60 | switch (strtolower($command)) { |
61 | 61 | case 'min': |
62 | - $this->minItems = (int)$data; |
|
62 | + $this->minItems = (int) $data; |
|
63 | 63 | if ($this->minItems < 0) { |
64 | 64 | throw new Exception("Minimum less than zero: '{$data}'"); |
65 | 65 | } |
@@ -69,7 +69,7 @@ discard block |
||
69 | 69 | return $this; |
70 | 70 | |
71 | 71 | case 'max': |
72 | - $this->maxItems = (int)$data; |
|
72 | + $this->maxItems = (int) $data; |
|
73 | 73 | if ($this->minItems !== null && $this->minItems > $this->maxItems) { |
74 | 74 | throw new Exception("Maximum less than minimum: '{$data}'"); |
75 | 75 | } |
@@ -182,8 +182,8 @@ discard block |
||
182 | 182 | } |
183 | 183 | |
184 | 184 | $exclusiveMinimum = $match[3] == '<'; |
185 | - $this->minItems = $match[4] === '' ? null : (int)$match[4]; |
|
186 | - $this->maxItems = $match[5] === '' ? null : (int)$match[5]; |
|
185 | + $this->minItems = $match[4] === '' ? null : (int) $match[4]; |
|
186 | + $this->maxItems = $match[5] === '' ? null : (int) $match[5]; |
|
187 | 187 | $exclusiveMaximum = isset($match[6]) ? ($match[6] == '>') : null; |
188 | 188 | if ($this->minItems && $this->maxItems && $this->minItems > $this->maxItems) { |
189 | 189 | self::swap($this->minItems, $this->maxItems); |
@@ -14,106 +14,106 @@ |
||
14 | 14 | class Info extends AbstractObject |
15 | 15 | { |
16 | 16 | |
17 | - /** |
|
18 | - * @var string |
|
19 | - */ |
|
20 | - private $title = 'undefined'; |
|
21 | - |
|
22 | - /** |
|
23 | - * @var string |
|
24 | - */ |
|
25 | - private $description; |
|
26 | - |
|
27 | - /** |
|
28 | - * @var string |
|
29 | - */ |
|
30 | - private $termsofservice; |
|
31 | - |
|
32 | - /** |
|
33 | - * @var Contact |
|
34 | - */ |
|
35 | - private $contact; |
|
36 | - |
|
37 | - /** |
|
38 | - * @var License |
|
39 | - */ |
|
40 | - private $license; |
|
41 | - |
|
42 | - /** |
|
43 | - * @var string|integer|float |
|
44 | - */ |
|
45 | - private $version = 0; |
|
46 | - |
|
47 | - /** |
|
48 | - * @param string $command |
|
49 | - * @param string $data |
|
50 | - * @return AbstractObject|boolean |
|
51 | - */ |
|
52 | - public function handleCommand($command, $data = null) |
|
53 | - { |
|
54 | - switch (strtolower($command)) { |
|
55 | - case 'title': |
|
56 | - case 'description': |
|
57 | - case 'termsofservice': |
|
58 | - case 'version': |
|
59 | - $this->$command = $data; |
|
60 | - return $this; |
|
61 | - |
|
62 | - case 'terms': // alias |
|
63 | - case 'tos': // alias |
|
64 | - $this->termsofservice = $data; |
|
65 | - return $this; |
|
66 | - |
|
67 | - case 'contact': |
|
68 | - $name = []; |
|
69 | - $url = null; |
|
70 | - $email = null; |
|
71 | - foreach (self::wordSplit($data) as $word) { |
|
72 | - if (filter_var($word, FILTER_VALIDATE_URL)) { |
|
73 | - $url = $word; |
|
74 | - } elseif (filter_var($word, FILTER_VALIDATE_EMAIL)) { |
|
75 | - $email = $word; |
|
76 | - } else { |
|
77 | - $name[] = $word; |
|
78 | - } |
|
79 | - } |
|
80 | - $name = implode(' ', array_filter($name)); |
|
81 | - $this->contact = new Contact($this, $name, $url, $email); |
|
82 | - return $this->contact; |
|
83 | - |
|
84 | - case 'license': |
|
85 | - $name = []; |
|
86 | - $url = null; |
|
87 | - foreach (self::wordSplit($data) as $word) { |
|
88 | - if (filter_var($word, FILTER_VALIDATE_URL)) { |
|
89 | - $url = $word; |
|
90 | - } else { |
|
91 | - $name[] = $word; |
|
92 | - } |
|
93 | - } |
|
94 | - $name = implode(' ', array_filter($name)); |
|
95 | - $this->license = new License($this, $name, $url); |
|
96 | - return $this->license; |
|
97 | - } |
|
98 | - |
|
99 | - return parent::handleCommand($command, $data); |
|
100 | - } |
|
101 | - |
|
102 | - public function toArray(): array |
|
103 | - { |
|
104 | - return self::arrayFilterNull(array_merge(array( |
|
105 | - 'title' => $this->title, |
|
106 | - 'description' => $this->description, |
|
107 | - 'termsOfService' => $this->termsofservice, |
|
108 | - 'contact' => $this->contact ? $this->contact->toArray() : null, |
|
109 | - 'license' => $this->license ? $this->license->toArray() : null, |
|
110 | - 'version' => (string)$this->version, |
|
111 | - ), parent::toArray())); |
|
112 | - } |
|
113 | - |
|
114 | - public function __toString() |
|
115 | - { |
|
116 | - return __CLASS__ . ' \'' . $this->title . '\''; |
|
117 | - } |
|
17 | + /** |
|
18 | + * @var string |
|
19 | + */ |
|
20 | + private $title = 'undefined'; |
|
21 | + |
|
22 | + /** |
|
23 | + * @var string |
|
24 | + */ |
|
25 | + private $description; |
|
26 | + |
|
27 | + /** |
|
28 | + * @var string |
|
29 | + */ |
|
30 | + private $termsofservice; |
|
31 | + |
|
32 | + /** |
|
33 | + * @var Contact |
|
34 | + */ |
|
35 | + private $contact; |
|
36 | + |
|
37 | + /** |
|
38 | + * @var License |
|
39 | + */ |
|
40 | + private $license; |
|
41 | + |
|
42 | + /** |
|
43 | + * @var string|integer|float |
|
44 | + */ |
|
45 | + private $version = 0; |
|
46 | + |
|
47 | + /** |
|
48 | + * @param string $command |
|
49 | + * @param string $data |
|
50 | + * @return AbstractObject|boolean |
|
51 | + */ |
|
52 | + public function handleCommand($command, $data = null) |
|
53 | + { |
|
54 | + switch (strtolower($command)) { |
|
55 | + case 'title': |
|
56 | + case 'description': |
|
57 | + case 'termsofservice': |
|
58 | + case 'version': |
|
59 | + $this->$command = $data; |
|
60 | + return $this; |
|
61 | + |
|
62 | + case 'terms': // alias |
|
63 | + case 'tos': // alias |
|
64 | + $this->termsofservice = $data; |
|
65 | + return $this; |
|
66 | + |
|
67 | + case 'contact': |
|
68 | + $name = []; |
|
69 | + $url = null; |
|
70 | + $email = null; |
|
71 | + foreach (self::wordSplit($data) as $word) { |
|
72 | + if (filter_var($word, FILTER_VALIDATE_URL)) { |
|
73 | + $url = $word; |
|
74 | + } elseif (filter_var($word, FILTER_VALIDATE_EMAIL)) { |
|
75 | + $email = $word; |
|
76 | + } else { |
|
77 | + $name[] = $word; |
|
78 | + } |
|
79 | + } |
|
80 | + $name = implode(' ', array_filter($name)); |
|
81 | + $this->contact = new Contact($this, $name, $url, $email); |
|
82 | + return $this->contact; |
|
83 | + |
|
84 | + case 'license': |
|
85 | + $name = []; |
|
86 | + $url = null; |
|
87 | + foreach (self::wordSplit($data) as $word) { |
|
88 | + if (filter_var($word, FILTER_VALIDATE_URL)) { |
|
89 | + $url = $word; |
|
90 | + } else { |
|
91 | + $name[] = $word; |
|
92 | + } |
|
93 | + } |
|
94 | + $name = implode(' ', array_filter($name)); |
|
95 | + $this->license = new License($this, $name, $url); |
|
96 | + return $this->license; |
|
97 | + } |
|
98 | + |
|
99 | + return parent::handleCommand($command, $data); |
|
100 | + } |
|
101 | + |
|
102 | + public function toArray(): array |
|
103 | + { |
|
104 | + return self::arrayFilterNull(array_merge(array( |
|
105 | + 'title' => $this->title, |
|
106 | + 'description' => $this->description, |
|
107 | + 'termsOfService' => $this->termsofservice, |
|
108 | + 'contact' => $this->contact ? $this->contact->toArray() : null, |
|
109 | + 'license' => $this->license ? $this->license->toArray() : null, |
|
110 | + 'version' => (string)$this->version, |
|
111 | + ), parent::toArray())); |
|
112 | + } |
|
113 | + |
|
114 | + public function __toString() |
|
115 | + { |
|
116 | + return __CLASS__ . ' \'' . $this->title . '\''; |
|
117 | + } |
|
118 | 118 | |
119 | 119 | } |
@@ -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 = []; |
|
41 | - |
|
42 | - public function __construct(?AbstractObject $parent = null) |
|
43 | - { |
|
44 | - $this->parent = $parent; |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * Trim whitespace from a multibyte string |
|
49 | - * @param string $string |
|
50 | - * @return string |
|
51 | - */ |
|
52 | - public static function trim($string): string |
|
53 | - { |
|
54 | - return mb_ereg_replace('^\s*([\s\S]*?)\s*$', '\1', $string); |
|
55 | - } |
|
56 | - |
|
57 | - /** |
|
58 | - * Filter all items from an array where the value is either null or an |
|
59 | - * empty array. |
|
60 | - * @param array $array |
|
61 | - * @return array |
|
62 | - */ |
|
63 | - public static function arrayFilterNull($array): array |
|
64 | - { |
|
65 | - return array_filter($array, static function ($value) { |
|
66 | - return $value !== null && $value !== []; |
|
67 | - }); |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Recursively call toArray() on all objects to return an array |
|
72 | - * @param array $array |
|
73 | - * @return array |
|
74 | - */ |
|
75 | - public static function objectsToArray($array): array |
|
76 | - { |
|
77 | - return array_map(static function (AbstractObject $item) { |
|
78 | - return $item->toArray(); |
|
79 | - }, $array); |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * @return array |
|
84 | - */ |
|
85 | - public function toArray(): array |
|
86 | - { |
|
87 | - return $this->extensions; |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * Shifts the first word off a text line and returns it |
|
92 | - * @param string $data |
|
93 | - * @return string|bool Either the first word or false if no more words available |
|
94 | - */ |
|
95 | - public static function wordShift(&$data) |
|
96 | - { |
|
97 | - if (preg_match('~^\s*(\S+)\s*(.*)$~s', $data, $matches) === 1) { |
|
98 | - $data = $matches[2]; |
|
99 | - return $matches[1]; |
|
100 | - } |
|
101 | - return false; |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * Splits a text line in all it's words |
|
106 | - * @param string $data |
|
107 | - * @return string[] |
|
108 | - */ |
|
109 | - public static function wordSplit($data): array |
|
110 | - { |
|
111 | - return array_values(preg_grep('~\S~', preg_split('~\s+~', $data))); |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * Translate consumes from shortcuts |
|
116 | - * @param String[] $mimeTypes |
|
117 | - * @return String[] |
|
118 | - */ |
|
119 | - protected static function translateMimeTypes($mimeTypes): array |
|
120 | - { |
|
121 | - foreach ($mimeTypes as &$mimeType) { |
|
122 | - if (isset(self::$mime_types[strtolower($mimeType)])) { |
|
123 | - $mimeType = self::$mime_types[strtolower($mimeType)]; |
|
124 | - } |
|
125 | - } |
|
126 | - |
|
127 | - return $mimeTypes; |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * @param string $command |
|
132 | - * @param string $data |
|
133 | - * @return AbstractObject|boolean |
|
134 | - */ |
|
135 | - public function handleCommand($command, $data = null) |
|
136 | - { |
|
137 | - if (stripos($command, 'x-') === 0) { |
|
138 | - $this->extensions[$command] = empty($data) ? $data : trim($data); |
|
139 | - return $this; |
|
140 | - } |
|
141 | - |
|
142 | - return false; |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * @return AbstractObject|null |
|
147 | - */ |
|
148 | - protected function getParent(): ?AbstractObject |
|
149 | - { |
|
150 | - return $this->parent; |
|
151 | - } |
|
152 | - |
|
153 | - protected function getParentClass($classname): AbstractObject |
|
154 | - { |
|
155 | - if (is_a($this, $classname)) { |
|
156 | - return $this; |
|
157 | - } |
|
158 | - return $this->parent->getParentClass($classname); |
|
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * @return Swagger |
|
163 | - */ |
|
164 | - protected function getSwagger(): Swagger |
|
165 | - { |
|
166 | - return $this->parent->getSwagger(); |
|
167 | - } |
|
168 | - |
|
169 | - /** |
|
170 | - * @return TypeRegistry |
|
171 | - */ |
|
172 | - protected function getTypeRegistry(): TypeRegistry |
|
173 | - { |
|
174 | - return $this->parent->getTypeRegistry(); |
|
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 = []; |
|
41 | + |
|
42 | + public function __construct(?AbstractObject $parent = null) |
|
43 | + { |
|
44 | + $this->parent = $parent; |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * Trim whitespace from a multibyte string |
|
49 | + * @param string $string |
|
50 | + * @return string |
|
51 | + */ |
|
52 | + public static function trim($string): string |
|
53 | + { |
|
54 | + return mb_ereg_replace('^\s*([\s\S]*?)\s*$', '\1', $string); |
|
55 | + } |
|
56 | + |
|
57 | + /** |
|
58 | + * Filter all items from an array where the value is either null or an |
|
59 | + * empty array. |
|
60 | + * @param array $array |
|
61 | + * @return array |
|
62 | + */ |
|
63 | + public static function arrayFilterNull($array): array |
|
64 | + { |
|
65 | + return array_filter($array, static function ($value) { |
|
66 | + return $value !== null && $value !== []; |
|
67 | + }); |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Recursively call toArray() on all objects to return an array |
|
72 | + * @param array $array |
|
73 | + * @return array |
|
74 | + */ |
|
75 | + public static function objectsToArray($array): array |
|
76 | + { |
|
77 | + return array_map(static function (AbstractObject $item) { |
|
78 | + return $item->toArray(); |
|
79 | + }, $array); |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * @return array |
|
84 | + */ |
|
85 | + public function toArray(): array |
|
86 | + { |
|
87 | + return $this->extensions; |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * Shifts the first word off a text line and returns it |
|
92 | + * @param string $data |
|
93 | + * @return string|bool Either the first word or false if no more words available |
|
94 | + */ |
|
95 | + public static function wordShift(&$data) |
|
96 | + { |
|
97 | + if (preg_match('~^\s*(\S+)\s*(.*)$~s', $data, $matches) === 1) { |
|
98 | + $data = $matches[2]; |
|
99 | + return $matches[1]; |
|
100 | + } |
|
101 | + return false; |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * Splits a text line in all it's words |
|
106 | + * @param string $data |
|
107 | + * @return string[] |
|
108 | + */ |
|
109 | + public static function wordSplit($data): array |
|
110 | + { |
|
111 | + return array_values(preg_grep('~\S~', preg_split('~\s+~', $data))); |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * Translate consumes from shortcuts |
|
116 | + * @param String[] $mimeTypes |
|
117 | + * @return String[] |
|
118 | + */ |
|
119 | + protected static function translateMimeTypes($mimeTypes): array |
|
120 | + { |
|
121 | + foreach ($mimeTypes as &$mimeType) { |
|
122 | + if (isset(self::$mime_types[strtolower($mimeType)])) { |
|
123 | + $mimeType = self::$mime_types[strtolower($mimeType)]; |
|
124 | + } |
|
125 | + } |
|
126 | + |
|
127 | + return $mimeTypes; |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * @param string $command |
|
132 | + * @param string $data |
|
133 | + * @return AbstractObject|boolean |
|
134 | + */ |
|
135 | + public function handleCommand($command, $data = null) |
|
136 | + { |
|
137 | + if (stripos($command, 'x-') === 0) { |
|
138 | + $this->extensions[$command] = empty($data) ? $data : trim($data); |
|
139 | + return $this; |
|
140 | + } |
|
141 | + |
|
142 | + return false; |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * @return AbstractObject|null |
|
147 | + */ |
|
148 | + protected function getParent(): ?AbstractObject |
|
149 | + { |
|
150 | + return $this->parent; |
|
151 | + } |
|
152 | + |
|
153 | + protected function getParentClass($classname): AbstractObject |
|
154 | + { |
|
155 | + if (is_a($this, $classname)) { |
|
156 | + return $this; |
|
157 | + } |
|
158 | + return $this->parent->getParentClass($classname); |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * @return Swagger |
|
163 | + */ |
|
164 | + protected function getSwagger(): Swagger |
|
165 | + { |
|
166 | + return $this->parent->getSwagger(); |
|
167 | + } |
|
168 | + |
|
169 | + /** |
|
170 | + * @return TypeRegistry |
|
171 | + */ |
|
172 | + protected function getTypeRegistry(): TypeRegistry |
|
173 | + { |
|
174 | + return $this->parent->getTypeRegistry(); |
|
175 | + } |
|
176 | 176 | |
177 | 177 | } |
@@ -62,7 +62,7 @@ discard block |
||
62 | 62 | */ |
63 | 63 | public static function arrayFilterNull($array): array |
64 | 64 | { |
65 | - return array_filter($array, static function ($value) { |
|
65 | + return array_filter($array, static function($value) { |
|
66 | 66 | return $value !== null && $value !== []; |
67 | 67 | }); |
68 | 68 | } |
@@ -74,7 +74,7 @@ discard block |
||
74 | 74 | */ |
75 | 75 | public static function objectsToArray($array): array |
76 | 76 | { |
77 | - return array_map(static function (AbstractObject $item) { |
|
77 | + return array_map(static function(AbstractObject $item) { |
|
78 | 78 | return $item->toArray(); |
79 | 79 | }, $array); |
80 | 80 | } |
@@ -38,215 +38,215 @@ |
||
38 | 38 | class SwaggerGen |
39 | 39 | { |
40 | 40 | |
41 | - public const FORMAT_ARRAY = ''; |
|
42 | - public const FORMAT_JSON = 'json'; |
|
43 | - public const FORMAT_JSON_PRETTY = 'json+'; |
|
44 | - public const FORMAT_YAML = 'yaml'; |
|
45 | - |
|
46 | - private string $host; |
|
47 | - private string $basePath; |
|
48 | - private array $dirs = []; |
|
49 | - private array $defines = []; |
|
50 | - |
|
51 | - /** |
|
52 | - * @var TypeRegistry |
|
53 | - */ |
|
54 | - private $typeRegistry; |
|
55 | - |
|
56 | - /** |
|
57 | - * Create a new SwaggerGen instance |
|
58 | - * |
|
59 | - * @param string $host |
|
60 | - * @param string $basePath |
|
61 | - * @param string[] $dirs |
|
62 | - * @param TypeRegistry $typeRegistry |
|
63 | - */ |
|
64 | - public function __construct($host = '', $basePath = '', $dirs = [], $typeRegistry = null) |
|
65 | - { |
|
66 | - $this->host = $host; |
|
67 | - $this->basePath = $basePath; |
|
68 | - $this->dirs = $dirs; |
|
69 | - $this->typeRegistry = $typeRegistry; |
|
70 | - } |
|
71 | - |
|
72 | - /** |
|
73 | - * Set a new type registry |
|
74 | - * |
|
75 | - * @param TypeRegistry $typeRegistry |
|
76 | - */ |
|
77 | - public function setTypeRegistry($typeRegistry = null) |
|
78 | - { |
|
79 | - $this->typeRegistry = $typeRegistry; |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * @param string $name |
|
84 | - */ |
|
85 | - public function define($name, $value = 1) |
|
86 | - { |
|
87 | - $this->defines[$name] = $value; |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * @param string $name |
|
92 | - */ |
|
93 | - public function undefine($name) |
|
94 | - { |
|
95 | - unset($this->defines[$name]); |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * Get Swagger 2.x output |
|
100 | - * |
|
101 | - * @param string[] $files |
|
102 | - * @param string[] $dirs |
|
103 | - * @param string $format |
|
104 | - * @return array|false|string |
|
105 | - * @throws Exception |
|
106 | - * @throws StatementException |
|
107 | - */ |
|
108 | - public function getSwagger(array $files, array $dirs = [], string $format = self::FORMAT_ARRAY) |
|
109 | - { |
|
110 | - $dirs = array_merge($this->dirs, $dirs); |
|
111 | - |
|
112 | - $statements = []; |
|
113 | - foreach ($files as $file) { |
|
114 | - switch (pathinfo($file, PATHINFO_EXTENSION)) { |
|
115 | - case 'php': |
|
116 | - $fileStatements = $this->parsePhpFile($file, $dirs); |
|
117 | - break; |
|
118 | - |
|
119 | - case 'txt': |
|
120 | - $fileStatements = $this->parseTextFile($file, $dirs); |
|
121 | - break; |
|
122 | - |
|
123 | - default: |
|
124 | - $fileStatements = $this->parseText($file, $dirs); |
|
125 | - break; |
|
126 | - } |
|
127 | - |
|
128 | - $statements[] = $fileStatements; |
|
129 | - } |
|
130 | - $statements = array_merge(...$statements); |
|
131 | - |
|
132 | - $output = $this->parseStatements($this->host, $this->basePath, $statements)->toArray(); |
|
133 | - |
|
134 | - switch ($format) { |
|
135 | - case self::FORMAT_JSON: |
|
136 | - $output = json_encode($output); |
|
137 | - break; |
|
138 | - |
|
139 | - case self::FORMAT_JSON_PRETTY: |
|
140 | - $flags = (defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 0); // Since PHP 5.4.0 |
|
141 | - $output = json_encode($output, $flags); |
|
142 | - break; |
|
143 | - |
|
144 | - case self::FORMAT_YAML: |
|
145 | - if (!function_exists('yaml_emit')) { |
|
146 | - throw new Exception('YAML extension not installed.'); |
|
147 | - } |
|
148 | - array_walk_recursive($output, static function (&$value) { |
|
149 | - if (is_object($value)) { |
|
150 | - $value = (array)$value; |
|
151 | - } |
|
152 | - }); |
|
153 | - $output = yaml_emit($output, YAML_UTF8_ENCODING, YAML_LN_BREAK); |
|
154 | - break; |
|
155 | - } |
|
156 | - |
|
157 | - return $output; |
|
158 | - } |
|
159 | - |
|
160 | - /** |
|
161 | - * @param string $file |
|
162 | - * @param string[] $dirs |
|
163 | - * @return Statement[] |
|
164 | - * @throws Exception |
|
165 | - */ |
|
166 | - private function parsePhpFile(string $file, array $dirs): array |
|
167 | - { |
|
168 | - return (new Parser())->parse($file, $dirs, $this->defines); |
|
169 | - } |
|
170 | - |
|
171 | - /** |
|
172 | - * @param string $file |
|
173 | - * @param string[] $dirs |
|
174 | - * @return Statement[] |
|
175 | - */ |
|
176 | - private function parseTextFile($file, $dirs) |
|
177 | - { |
|
178 | - return (new TextParser())->parse($file, $dirs, $this->defines); |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * @param string $text |
|
183 | - * @param string[] $dirs |
|
184 | - * @return Statement[] |
|
185 | - */ |
|
186 | - private function parseText(string $text, array $dirs): array |
|
187 | - { |
|
188 | - return (new TextParser())->parseText($text, $dirs, $this->defines); |
|
189 | - } |
|
190 | - |
|
191 | - /** |
|
192 | - * Creates Swagger\Swagger object and populates it with statements |
|
193 | - * |
|
194 | - * This effectively converts the linear list of statements into parse-tree |
|
195 | - * like structure, performing some checks (like rejecting unknown |
|
196 | - * subcommands) during the process. Returned Swagger\Swagger object is |
|
197 | - * ready for serialization with {@see Swagger\Swagger::toArray} |
|
198 | - * |
|
199 | - * @param string $host |
|
200 | - * @param string $basePath |
|
201 | - * @param Statement[] $statements |
|
202 | - * @return Swagger |
|
203 | - * @throws StatementException |
|
204 | - */ |
|
205 | - public function parseStatements($host, $basePath, $statements) |
|
206 | - { |
|
207 | - $swagger = new Swagger($host, $basePath, $this->typeRegistry); |
|
208 | - |
|
209 | - $stack = array($swagger); |
|
210 | - /* @var AbstractObject[] $stack */ |
|
211 | - foreach ($statements as $statement) { |
|
212 | - try { |
|
213 | - $top = end($stack); |
|
214 | - |
|
215 | - do { |
|
216 | - $result = $top->handleCommand($statement->getCommand(), $statement->getData()); |
|
217 | - |
|
218 | - if ($result) { |
|
219 | - if ($result !== $top) { |
|
220 | - // Remove all similar classes from array first! |
|
221 | - $classname = get_class($result); |
|
222 | - $stack = array_filter($stack, static function ($class) use ($classname) { |
|
223 | - return !(is_a($class, $classname)); |
|
224 | - }); |
|
225 | - |
|
226 | - $stack[] = $result; |
|
227 | - } |
|
228 | - } else { |
|
229 | - $top = prev($stack); |
|
230 | - } |
|
231 | - } while (!$result && $top); |
|
232 | - } catch (Exception $e) { |
|
233 | - throw new StatementException($e->getMessage(), $e->getCode(), $e, $statement); |
|
234 | - } |
|
235 | - |
|
236 | - if (!$result && !$top) { |
|
237 | - $messages = array("Unsupported or unknown command: {$statement->getCommand()} {$statement->getData()}"); |
|
238 | - |
|
239 | - $stacktrace = []; |
|
240 | - foreach ($stack as $object) { |
|
241 | - $stacktrace[] = (string)$object; |
|
242 | - } |
|
243 | - $messages[] = implode(', ' . PHP_EOL, $stacktrace); |
|
244 | - |
|
245 | - throw new StatementException(implode('. ', $messages), 0, null, $statement); |
|
246 | - } |
|
247 | - } |
|
248 | - |
|
249 | - return $swagger; |
|
250 | - } |
|
41 | + public const FORMAT_ARRAY = ''; |
|
42 | + public const FORMAT_JSON = 'json'; |
|
43 | + public const FORMAT_JSON_PRETTY = 'json+'; |
|
44 | + public const FORMAT_YAML = 'yaml'; |
|
45 | + |
|
46 | + private string $host; |
|
47 | + private string $basePath; |
|
48 | + private array $dirs = []; |
|
49 | + private array $defines = []; |
|
50 | + |
|
51 | + /** |
|
52 | + * @var TypeRegistry |
|
53 | + */ |
|
54 | + private $typeRegistry; |
|
55 | + |
|
56 | + /** |
|
57 | + * Create a new SwaggerGen instance |
|
58 | + * |
|
59 | + * @param string $host |
|
60 | + * @param string $basePath |
|
61 | + * @param string[] $dirs |
|
62 | + * @param TypeRegistry $typeRegistry |
|
63 | + */ |
|
64 | + public function __construct($host = '', $basePath = '', $dirs = [], $typeRegistry = null) |
|
65 | + { |
|
66 | + $this->host = $host; |
|
67 | + $this->basePath = $basePath; |
|
68 | + $this->dirs = $dirs; |
|
69 | + $this->typeRegistry = $typeRegistry; |
|
70 | + } |
|
71 | + |
|
72 | + /** |
|
73 | + * Set a new type registry |
|
74 | + * |
|
75 | + * @param TypeRegistry $typeRegistry |
|
76 | + */ |
|
77 | + public function setTypeRegistry($typeRegistry = null) |
|
78 | + { |
|
79 | + $this->typeRegistry = $typeRegistry; |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * @param string $name |
|
84 | + */ |
|
85 | + public function define($name, $value = 1) |
|
86 | + { |
|
87 | + $this->defines[$name] = $value; |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * @param string $name |
|
92 | + */ |
|
93 | + public function undefine($name) |
|
94 | + { |
|
95 | + unset($this->defines[$name]); |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * Get Swagger 2.x output |
|
100 | + * |
|
101 | + * @param string[] $files |
|
102 | + * @param string[] $dirs |
|
103 | + * @param string $format |
|
104 | + * @return array|false|string |
|
105 | + * @throws Exception |
|
106 | + * @throws StatementException |
|
107 | + */ |
|
108 | + public function getSwagger(array $files, array $dirs = [], string $format = self::FORMAT_ARRAY) |
|
109 | + { |
|
110 | + $dirs = array_merge($this->dirs, $dirs); |
|
111 | + |
|
112 | + $statements = []; |
|
113 | + foreach ($files as $file) { |
|
114 | + switch (pathinfo($file, PATHINFO_EXTENSION)) { |
|
115 | + case 'php': |
|
116 | + $fileStatements = $this->parsePhpFile($file, $dirs); |
|
117 | + break; |
|
118 | + |
|
119 | + case 'txt': |
|
120 | + $fileStatements = $this->parseTextFile($file, $dirs); |
|
121 | + break; |
|
122 | + |
|
123 | + default: |
|
124 | + $fileStatements = $this->parseText($file, $dirs); |
|
125 | + break; |
|
126 | + } |
|
127 | + |
|
128 | + $statements[] = $fileStatements; |
|
129 | + } |
|
130 | + $statements = array_merge(...$statements); |
|
131 | + |
|
132 | + $output = $this->parseStatements($this->host, $this->basePath, $statements)->toArray(); |
|
133 | + |
|
134 | + switch ($format) { |
|
135 | + case self::FORMAT_JSON: |
|
136 | + $output = json_encode($output); |
|
137 | + break; |
|
138 | + |
|
139 | + case self::FORMAT_JSON_PRETTY: |
|
140 | + $flags = (defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 0); // Since PHP 5.4.0 |
|
141 | + $output = json_encode($output, $flags); |
|
142 | + break; |
|
143 | + |
|
144 | + case self::FORMAT_YAML: |
|
145 | + if (!function_exists('yaml_emit')) { |
|
146 | + throw new Exception('YAML extension not installed.'); |
|
147 | + } |
|
148 | + array_walk_recursive($output, static function (&$value) { |
|
149 | + if (is_object($value)) { |
|
150 | + $value = (array)$value; |
|
151 | + } |
|
152 | + }); |
|
153 | + $output = yaml_emit($output, YAML_UTF8_ENCODING, YAML_LN_BREAK); |
|
154 | + break; |
|
155 | + } |
|
156 | + |
|
157 | + return $output; |
|
158 | + } |
|
159 | + |
|
160 | + /** |
|
161 | + * @param string $file |
|
162 | + * @param string[] $dirs |
|
163 | + * @return Statement[] |
|
164 | + * @throws Exception |
|
165 | + */ |
|
166 | + private function parsePhpFile(string $file, array $dirs): array |
|
167 | + { |
|
168 | + return (new Parser())->parse($file, $dirs, $this->defines); |
|
169 | + } |
|
170 | + |
|
171 | + /** |
|
172 | + * @param string $file |
|
173 | + * @param string[] $dirs |
|
174 | + * @return Statement[] |
|
175 | + */ |
|
176 | + private function parseTextFile($file, $dirs) |
|
177 | + { |
|
178 | + return (new TextParser())->parse($file, $dirs, $this->defines); |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * @param string $text |
|
183 | + * @param string[] $dirs |
|
184 | + * @return Statement[] |
|
185 | + */ |
|
186 | + private function parseText(string $text, array $dirs): array |
|
187 | + { |
|
188 | + return (new TextParser())->parseText($text, $dirs, $this->defines); |
|
189 | + } |
|
190 | + |
|
191 | + /** |
|
192 | + * Creates Swagger\Swagger object and populates it with statements |
|
193 | + * |
|
194 | + * This effectively converts the linear list of statements into parse-tree |
|
195 | + * like structure, performing some checks (like rejecting unknown |
|
196 | + * subcommands) during the process. Returned Swagger\Swagger object is |
|
197 | + * ready for serialization with {@see Swagger\Swagger::toArray} |
|
198 | + * |
|
199 | + * @param string $host |
|
200 | + * @param string $basePath |
|
201 | + * @param Statement[] $statements |
|
202 | + * @return Swagger |
|
203 | + * @throws StatementException |
|
204 | + */ |
|
205 | + public function parseStatements($host, $basePath, $statements) |
|
206 | + { |
|
207 | + $swagger = new Swagger($host, $basePath, $this->typeRegistry); |
|
208 | + |
|
209 | + $stack = array($swagger); |
|
210 | + /* @var AbstractObject[] $stack */ |
|
211 | + foreach ($statements as $statement) { |
|
212 | + try { |
|
213 | + $top = end($stack); |
|
214 | + |
|
215 | + do { |
|
216 | + $result = $top->handleCommand($statement->getCommand(), $statement->getData()); |
|
217 | + |
|
218 | + if ($result) { |
|
219 | + if ($result !== $top) { |
|
220 | + // Remove all similar classes from array first! |
|
221 | + $classname = get_class($result); |
|
222 | + $stack = array_filter($stack, static function ($class) use ($classname) { |
|
223 | + return !(is_a($class, $classname)); |
|
224 | + }); |
|
225 | + |
|
226 | + $stack[] = $result; |
|
227 | + } |
|
228 | + } else { |
|
229 | + $top = prev($stack); |
|
230 | + } |
|
231 | + } while (!$result && $top); |
|
232 | + } catch (Exception $e) { |
|
233 | + throw new StatementException($e->getMessage(), $e->getCode(), $e, $statement); |
|
234 | + } |
|
235 | + |
|
236 | + if (!$result && !$top) { |
|
237 | + $messages = array("Unsupported or unknown command: {$statement->getCommand()} {$statement->getData()}"); |
|
238 | + |
|
239 | + $stacktrace = []; |
|
240 | + foreach ($stack as $object) { |
|
241 | + $stacktrace[] = (string)$object; |
|
242 | + } |
|
243 | + $messages[] = implode(', ' . PHP_EOL, $stacktrace); |
|
244 | + |
|
245 | + throw new StatementException(implode('. ', $messages), 0, null, $statement); |
|
246 | + } |
|
247 | + } |
|
248 | + |
|
249 | + return $swagger; |
|
250 | + } |
|
251 | 251 | |
252 | 252 | } |
@@ -145,9 +145,9 @@ discard block |
||
145 | 145 | if (!function_exists('yaml_emit')) { |
146 | 146 | throw new Exception('YAML extension not installed.'); |
147 | 147 | } |
148 | - array_walk_recursive($output, static function (&$value) { |
|
148 | + array_walk_recursive($output, static function(&$value) { |
|
149 | 149 | if (is_object($value)) { |
150 | - $value = (array)$value; |
|
150 | + $value = (array) $value; |
|
151 | 151 | } |
152 | 152 | }); |
153 | 153 | $output = yaml_emit($output, YAML_UTF8_ENCODING, YAML_LN_BREAK); |
@@ -219,7 +219,7 @@ discard block |
||
219 | 219 | if ($result !== $top) { |
220 | 220 | // Remove all similar classes from array first! |
221 | 221 | $classname = get_class($result); |
222 | - $stack = array_filter($stack, static function ($class) use ($classname) { |
|
222 | + $stack = array_filter($stack, static function($class) use ($classname) { |
|
223 | 223 | return !(is_a($class, $classname)); |
224 | 224 | }); |
225 | 225 | |
@@ -238,7 +238,7 @@ discard block |
||
238 | 238 | |
239 | 239 | $stacktrace = []; |
240 | 240 | foreach ($stack as $object) { |
241 | - $stacktrace[] = (string)$object; |
|
241 | + $stacktrace[] = (string) $object; |
|
242 | 242 | } |
243 | 243 | $messages[] = implode(', ' . PHP_EOL, $stacktrace); |
244 | 244 |
@@ -15,57 +15,57 @@ |
||
15 | 15 | class TypeRegistry |
16 | 16 | { |
17 | 17 | |
18 | - /** |
|
19 | - * Map of format-name => class-name |
|
20 | - * |
|
21 | - * @var array |
|
22 | - */ |
|
23 | - private $formats = []; |
|
18 | + /** |
|
19 | + * Map of format-name => class-name |
|
20 | + * |
|
21 | + * @var array |
|
22 | + */ |
|
23 | + private $formats = []; |
|
24 | 24 | |
25 | - /** |
|
26 | - * Add a type name from classname |
|
27 | - * |
|
28 | - * @param string $classname |
|
29 | - */ |
|
30 | - public function add(string $classname): void |
|
31 | - { |
|
32 | - if (is_subclass_of($classname, ICustomType::class)) { |
|
33 | - foreach ($classname::getFormats() as $format) { |
|
34 | - $this->formats[$format] = $classname; |
|
35 | - } |
|
36 | - } |
|
37 | - } |
|
25 | + /** |
|
26 | + * Add a type name from classname |
|
27 | + * |
|
28 | + * @param string $classname |
|
29 | + */ |
|
30 | + public function add(string $classname): void |
|
31 | + { |
|
32 | + if (is_subclass_of($classname, ICustomType::class)) { |
|
33 | + foreach ($classname::getFormats() as $format) { |
|
34 | + $this->formats[$format] = $classname; |
|
35 | + } |
|
36 | + } |
|
37 | + } |
|
38 | 38 | |
39 | - /** |
|
40 | - * Remove type format by explicitly nulling it (disables it) |
|
41 | - * |
|
42 | - * @param string $name |
|
43 | - */ |
|
44 | - public function remove($name) |
|
45 | - { |
|
46 | - $this->formats[$name] = null; |
|
47 | - } |
|
39 | + /** |
|
40 | + * Remove type format by explicitly nulling it (disables it) |
|
41 | + * |
|
42 | + * @param string $name |
|
43 | + */ |
|
44 | + public function remove($name) |
|
45 | + { |
|
46 | + $this->formats[$name] = null; |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * Is a type format known? |
|
51 | - * |
|
52 | - * @return bool |
|
53 | - */ |
|
54 | - public function has($name) |
|
55 | - { |
|
56 | - return !empty($this->formats[$name]); |
|
57 | - } |
|
49 | + /** |
|
50 | + * Is a type format known? |
|
51 | + * |
|
52 | + * @return bool |
|
53 | + */ |
|
54 | + public function has($name) |
|
55 | + { |
|
56 | + return !empty($this->formats[$name]); |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * Get the format class name |
|
61 | - * |
|
62 | - * @return null|string |
|
63 | - */ |
|
64 | - public function get($name) |
|
65 | - { |
|
66 | - return !empty($this->formats[$name]) |
|
67 | - ? $this->formats[$name] |
|
68 | - : null; |
|
69 | - } |
|
59 | + /** |
|
60 | + * Get the format class name |
|
61 | + * |
|
62 | + * @return null|string |
|
63 | + */ |
|
64 | + public function get($name) |
|
65 | + { |
|
66 | + return !empty($this->formats[$name]) |
|
67 | + ? $this->formats[$name] |
|
68 | + : null; |
|
69 | + } |
|
70 | 70 | |
71 | 71 | } |
@@ -12,8 +12,8 @@ discard block |
||
12 | 12 | * @copyright 2014-2025 Martijn van der Lee |
13 | 13 | * @license https://opensource.org/licenses/MIT MIT |
14 | 14 | */ |
15 | -class TypeRegistry |
|
16 | -{ |
|
15 | +class TypeRegistry |
|
16 | +{ |
|
17 | 17 | |
18 | 18 | /** |
19 | 19 | * Map of format-name => class-name |
@@ -29,8 +29,8 @@ discard block |
||
29 | 29 | */ |
30 | 30 | public function add(string $classname): void |
31 | 31 | { |
32 | - if (is_subclass_of($classname, ICustomType::class)) { |
|
33 | - foreach ($classname::getFormats() as $format) { |
|
32 | + if (is_subclass_of($classname, ICustomType::class)) { |
|
33 | + foreach ($classname::getFormats() as $format) { |
|
34 | 34 | $this->formats[$format] = $classname; |
35 | 35 | } |
36 | 36 | } |
@@ -41,8 +41,8 @@ discard block |
||
41 | 41 | * |
42 | 42 | * @param string $name |
43 | 43 | */ |
44 | - public function remove($name) |
|
45 | - { |
|
44 | + public function remove($name) |
|
45 | + { |
|
46 | 46 | $this->formats[$name] = null; |
47 | 47 | } |
48 | 48 | |
@@ -51,8 +51,8 @@ discard block |
||
51 | 51 | * |
52 | 52 | * @return bool |
53 | 53 | */ |
54 | - public function has($name) |
|
55 | - { |
|
54 | + public function has($name) |
|
55 | + { |
|
56 | 56 | return !empty($this->formats[$name]); |
57 | 57 | } |
58 | 58 | |
@@ -61,8 +61,8 @@ discard block |
||
61 | 61 | * |
62 | 62 | * @return null|string |
63 | 63 | */ |
64 | - public function get($name) |
|
65 | - { |
|
64 | + public function get($name) |
|
65 | + { |
|
66 | 66 | return !empty($this->formats[$name]) |
67 | 67 | ? $this->formats[$name] |
68 | 68 | : null; |