@@ -15,157 +15,157 @@ |
||
15 | 15 | class NumberType extends AbstractType |
16 | 16 | { |
17 | 17 | |
18 | - /** @noinspection PhpRegExpUnsupportedModifierInspection */ |
|
19 | - const REGEX_RANGE = '(?:([[<])(-?(?:\\d*\\.?\\d+|\\d+\\.\\d*))?,(-?(?:\\d*\\.?\\d+|\\d+\\.\\d*))?([\\]>]))?'; |
|
20 | - /** @noinspection PhpRegExpUnsupportedModifierInspection */ |
|
21 | - const REGEX_DEFAULT = '(?:=(-?(?:\\d*\\.?\\d+|\\d+\\.\\d*)))?'; |
|
22 | - |
|
23 | - private static $formats = array( |
|
24 | - 'float' => 'float', |
|
25 | - 'double' => 'double', |
|
26 | - ); |
|
27 | - private $format; |
|
28 | - private $default = null; |
|
29 | - private $maximum = null; |
|
30 | - private $exclusiveMaximum = null; |
|
31 | - private $minimum = null; |
|
32 | - private $exclusiveMinimum = null; |
|
33 | - private $enum = array(); |
|
34 | - private $multipleOf = null; |
|
35 | - |
|
36 | - /** |
|
37 | - * @throws Exception |
|
38 | - */ |
|
39 | - protected function parseDefinition($definition) |
|
40 | - { |
|
41 | - $match = array(); |
|
42 | - if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
43 | - throw new Exception("Unparseable number definition: '{$definition}'"); |
|
44 | - } |
|
45 | - |
|
46 | - $this->parseFormat($definition, $match); |
|
47 | - $this->parseRange($definition, $match); |
|
48 | - $this->parseDefault($definition, $match); |
|
49 | - } |
|
50 | - |
|
51 | - /** |
|
52 | - * @param string[] $match |
|
53 | - * @throws Exception |
|
54 | - */ |
|
55 | - private function parseFormat($definition, $match) |
|
56 | - { |
|
57 | - if (!isset(self::$formats[strtolower($match[1])])) { |
|
58 | - throw new Exception("Not a number: '{$definition}'"); |
|
59 | - } |
|
60 | - $this->format = self::$formats[strtolower($match[1])]; |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * @param string[] $match |
|
65 | - * @throws Exception |
|
66 | - */ |
|
67 | - private function parseRange($definition, $match) |
|
68 | - { |
|
69 | - if (!empty($match[2])) { |
|
70 | - if ($match[3] === '' && $match[4] === '') { |
|
71 | - throw new Exception("Empty number range: '{$definition}'"); |
|
72 | - } |
|
73 | - |
|
74 | - $this->exclusiveMinimum = $match[2] == '<'; |
|
75 | - $this->minimum = $match[3] === '' ? null : (float)$match[3]; |
|
76 | - $this->maximum = $match[4] === '' ? null : (float)$match[4]; |
|
77 | - $this->exclusiveMaximum = isset($match[5]) ? ($match[5] == '>') : null; |
|
78 | - if ($this->minimum && $this->maximum && $this->minimum > $this->maximum) { |
|
79 | - self::swap($this->minimum, $this->maximum); |
|
80 | - self::swap($this->exclusiveMinimum, $this->exclusiveMaximum); |
|
81 | - } |
|
82 | - } |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * @param string[] $match |
|
87 | - * @throws Exception |
|
88 | - */ |
|
89 | - private function parseDefault($definition, $match) |
|
90 | - { |
|
91 | - $this->default = isset($match[6]) && $match[6] !== '' ? $this->validateDefault($match[6]) : null; |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * @param string $command The comment command |
|
96 | - * @param string $data Any data added after the command |
|
97 | - * @return AbstractType|boolean |
|
98 | - * @throws Exception |
|
99 | - * @throws Exception |
|
100 | - * @throws Exception |
|
101 | - */ |
|
102 | - public function handleCommand($command, $data = null) |
|
103 | - { |
|
104 | - switch (strtolower($command)) { |
|
105 | - case 'default': |
|
106 | - $this->default = $this->validateDefault($data); |
|
107 | - return $this; |
|
108 | - |
|
109 | - case 'enum': |
|
110 | - $words = self::wordSplit($data); |
|
111 | - foreach ($words as &$word) { |
|
112 | - $word = $this->validateDefault($word); |
|
113 | - } |
|
114 | - unset($word); |
|
115 | - $this->enum = array_merge($this->enum, $words); |
|
116 | - return $this; |
|
117 | - |
|
118 | - case 'step': |
|
119 | - if (($step = (float)$data) > 0) { |
|
120 | - $this->multipleOf = $step; |
|
121 | - } |
|
122 | - return $this; |
|
123 | - } |
|
124 | - |
|
125 | - return parent::handleCommand($command, $data); |
|
126 | - } |
|
127 | - |
|
128 | - public function toArray(): array |
|
129 | - { |
|
130 | - return self::arrayFilterNull(array_merge(array( |
|
131 | - 'type' => 'number', |
|
132 | - 'format' => $this->format, |
|
133 | - 'default' => $this->default, |
|
134 | - 'minimum' => $this->minimum, |
|
135 | - 'exclusiveMinimum' => ($this->exclusiveMinimum && !is_null($this->minimum)) ? true : null, |
|
136 | - 'maximum' => $this->maximum, |
|
137 | - 'exclusiveMaximum' => ($this->exclusiveMaximum && !is_null($this->maximum)) ? true : null, |
|
138 | - 'enum' => $this->enum, |
|
139 | - 'multipleOf' => $this->multipleOf, |
|
140 | - ), parent::toArray())); |
|
141 | - } |
|
142 | - |
|
143 | - public function __toString() |
|
144 | - { |
|
145 | - return __CLASS__; |
|
146 | - } |
|
147 | - |
|
148 | - /** |
|
149 | - * @throws Exception |
|
150 | - */ |
|
151 | - private function validateDefault($value) |
|
152 | - { |
|
153 | - if (preg_match('~^-?(?:\\d*\\.?\\d+|\\d+\\.\\d*)$~', $value) !== 1) { |
|
154 | - throw new Exception("Invalid number default: '{$value}'"); |
|
155 | - } |
|
156 | - |
|
157 | - if ($this->maximum) { |
|
158 | - if (($value > $this->maximum) || ($this->exclusiveMaximum && $value == $this->maximum)) { |
|
159 | - throw new Exception("Default number beyond maximum: '{$value}'"); |
|
160 | - } |
|
161 | - } |
|
162 | - if ($this->minimum) { |
|
163 | - if (($value < $this->minimum) || ($this->exclusiveMinimum && $value == $this->minimum)) { |
|
164 | - throw new Exception("Default number beyond minimum: '{$value}'"); |
|
165 | - } |
|
166 | - } |
|
167 | - |
|
168 | - return (float)$value; |
|
169 | - } |
|
18 | + /** @noinspection PhpRegExpUnsupportedModifierInspection */ |
|
19 | + const REGEX_RANGE = '(?:([[<])(-?(?:\\d*\\.?\\d+|\\d+\\.\\d*))?,(-?(?:\\d*\\.?\\d+|\\d+\\.\\d*))?([\\]>]))?'; |
|
20 | + /** @noinspection PhpRegExpUnsupportedModifierInspection */ |
|
21 | + const REGEX_DEFAULT = '(?:=(-?(?:\\d*\\.?\\d+|\\d+\\.\\d*)))?'; |
|
22 | + |
|
23 | + private static $formats = array( |
|
24 | + 'float' => 'float', |
|
25 | + 'double' => 'double', |
|
26 | + ); |
|
27 | + private $format; |
|
28 | + private $default = null; |
|
29 | + private $maximum = null; |
|
30 | + private $exclusiveMaximum = null; |
|
31 | + private $minimum = null; |
|
32 | + private $exclusiveMinimum = null; |
|
33 | + private $enum = array(); |
|
34 | + private $multipleOf = null; |
|
35 | + |
|
36 | + /** |
|
37 | + * @throws Exception |
|
38 | + */ |
|
39 | + protected function parseDefinition($definition) |
|
40 | + { |
|
41 | + $match = array(); |
|
42 | + if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
43 | + throw new Exception("Unparseable number definition: '{$definition}'"); |
|
44 | + } |
|
45 | + |
|
46 | + $this->parseFormat($definition, $match); |
|
47 | + $this->parseRange($definition, $match); |
|
48 | + $this->parseDefault($definition, $match); |
|
49 | + } |
|
50 | + |
|
51 | + /** |
|
52 | + * @param string[] $match |
|
53 | + * @throws Exception |
|
54 | + */ |
|
55 | + private function parseFormat($definition, $match) |
|
56 | + { |
|
57 | + if (!isset(self::$formats[strtolower($match[1])])) { |
|
58 | + throw new Exception("Not a number: '{$definition}'"); |
|
59 | + } |
|
60 | + $this->format = self::$formats[strtolower($match[1])]; |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * @param string[] $match |
|
65 | + * @throws Exception |
|
66 | + */ |
|
67 | + private function parseRange($definition, $match) |
|
68 | + { |
|
69 | + if (!empty($match[2])) { |
|
70 | + if ($match[3] === '' && $match[4] === '') { |
|
71 | + throw new Exception("Empty number range: '{$definition}'"); |
|
72 | + } |
|
73 | + |
|
74 | + $this->exclusiveMinimum = $match[2] == '<'; |
|
75 | + $this->minimum = $match[3] === '' ? null : (float)$match[3]; |
|
76 | + $this->maximum = $match[4] === '' ? null : (float)$match[4]; |
|
77 | + $this->exclusiveMaximum = isset($match[5]) ? ($match[5] == '>') : null; |
|
78 | + if ($this->minimum && $this->maximum && $this->minimum > $this->maximum) { |
|
79 | + self::swap($this->minimum, $this->maximum); |
|
80 | + self::swap($this->exclusiveMinimum, $this->exclusiveMaximum); |
|
81 | + } |
|
82 | + } |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * @param string[] $match |
|
87 | + * @throws Exception |
|
88 | + */ |
|
89 | + private function parseDefault($definition, $match) |
|
90 | + { |
|
91 | + $this->default = isset($match[6]) && $match[6] !== '' ? $this->validateDefault($match[6]) : null; |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * @param string $command The comment command |
|
96 | + * @param string $data Any data added after the command |
|
97 | + * @return AbstractType|boolean |
|
98 | + * @throws Exception |
|
99 | + * @throws Exception |
|
100 | + * @throws Exception |
|
101 | + */ |
|
102 | + public function handleCommand($command, $data = null) |
|
103 | + { |
|
104 | + switch (strtolower($command)) { |
|
105 | + case 'default': |
|
106 | + $this->default = $this->validateDefault($data); |
|
107 | + return $this; |
|
108 | + |
|
109 | + case 'enum': |
|
110 | + $words = self::wordSplit($data); |
|
111 | + foreach ($words as &$word) { |
|
112 | + $word = $this->validateDefault($word); |
|
113 | + } |
|
114 | + unset($word); |
|
115 | + $this->enum = array_merge($this->enum, $words); |
|
116 | + return $this; |
|
117 | + |
|
118 | + case 'step': |
|
119 | + if (($step = (float)$data) > 0) { |
|
120 | + $this->multipleOf = $step; |
|
121 | + } |
|
122 | + return $this; |
|
123 | + } |
|
124 | + |
|
125 | + return parent::handleCommand($command, $data); |
|
126 | + } |
|
127 | + |
|
128 | + public function toArray(): array |
|
129 | + { |
|
130 | + return self::arrayFilterNull(array_merge(array( |
|
131 | + 'type' => 'number', |
|
132 | + 'format' => $this->format, |
|
133 | + 'default' => $this->default, |
|
134 | + 'minimum' => $this->minimum, |
|
135 | + 'exclusiveMinimum' => ($this->exclusiveMinimum && !is_null($this->minimum)) ? true : null, |
|
136 | + 'maximum' => $this->maximum, |
|
137 | + 'exclusiveMaximum' => ($this->exclusiveMaximum && !is_null($this->maximum)) ? true : null, |
|
138 | + 'enum' => $this->enum, |
|
139 | + 'multipleOf' => $this->multipleOf, |
|
140 | + ), parent::toArray())); |
|
141 | + } |
|
142 | + |
|
143 | + public function __toString() |
|
144 | + { |
|
145 | + return __CLASS__; |
|
146 | + } |
|
147 | + |
|
148 | + /** |
|
149 | + * @throws Exception |
|
150 | + */ |
|
151 | + private function validateDefault($value) |
|
152 | + { |
|
153 | + if (preg_match('~^-?(?:\\d*\\.?\\d+|\\d+\\.\\d*)$~', $value) !== 1) { |
|
154 | + throw new Exception("Invalid number default: '{$value}'"); |
|
155 | + } |
|
156 | + |
|
157 | + if ($this->maximum) { |
|
158 | + if (($value > $this->maximum) || ($this->exclusiveMaximum && $value == $this->maximum)) { |
|
159 | + throw new Exception("Default number beyond maximum: '{$value}'"); |
|
160 | + } |
|
161 | + } |
|
162 | + if ($this->minimum) { |
|
163 | + if (($value < $this->minimum) || ($this->exclusiveMinimum && $value == $this->minimum)) { |
|
164 | + throw new Exception("Default number beyond minimum: '{$value}'"); |
|
165 | + } |
|
166 | + } |
|
167 | + |
|
168 | + return (float)$value; |
|
169 | + } |
|
170 | 170 | |
171 | 171 | } |
@@ -72,8 +72,8 @@ discard block |
||
72 | 72 | } |
73 | 73 | |
74 | 74 | $this->exclusiveMinimum = $match[2] == '<'; |
75 | - $this->minimum = $match[3] === '' ? null : (float)$match[3]; |
|
76 | - $this->maximum = $match[4] === '' ? null : (float)$match[4]; |
|
75 | + $this->minimum = $match[3] === '' ? null : (float) $match[3]; |
|
76 | + $this->maximum = $match[4] === '' ? null : (float) $match[4]; |
|
77 | 77 | $this->exclusiveMaximum = isset($match[5]) ? ($match[5] == '>') : null; |
78 | 78 | if ($this->minimum && $this->maximum && $this->minimum > $this->maximum) { |
79 | 79 | self::swap($this->minimum, $this->maximum); |
@@ -116,7 +116,7 @@ discard block |
||
116 | 116 | return $this; |
117 | 117 | |
118 | 118 | case 'step': |
119 | - if (($step = (float)$data) > 0) { |
|
119 | + if (($step = (float) $data) > 0) { |
|
120 | 120 | $this->multipleOf = $step; |
121 | 121 | } |
122 | 122 | return $this; |
@@ -165,7 +165,7 @@ discard block |
||
165 | 165 | } |
166 | 166 | } |
167 | 167 | |
168 | - return (float)$value; |
|
168 | + return (float) $value; |
|
169 | 169 | } |
170 | 170 | |
171 | 171 | } |
@@ -16,82 +16,82 @@ |
||
16 | 16 | class Property extends AbstractObject |
17 | 17 | { |
18 | 18 | |
19 | - /** |
|
20 | - * Description of this property |
|
21 | - * @var string |
|
22 | - */ |
|
23 | - private $description; |
|
19 | + /** |
|
20 | + * Description of this property |
|
21 | + * @var string |
|
22 | + */ |
|
23 | + private $description; |
|
24 | 24 | |
25 | - /** |
|
26 | - * Whether property is read only |
|
27 | - * @var bool |
|
28 | - */ |
|
29 | - private $readOnly; |
|
25 | + /** |
|
26 | + * Whether property is read only |
|
27 | + * @var bool |
|
28 | + */ |
|
29 | + private $readOnly; |
|
30 | 30 | |
31 | - /** |
|
32 | - * Type definition of this property |
|
33 | - * @var AbstractType |
|
34 | - */ |
|
35 | - private $Type; |
|
31 | + /** |
|
32 | + * Type definition of this property |
|
33 | + * @var AbstractType |
|
34 | + */ |
|
35 | + private $Type; |
|
36 | 36 | |
37 | - /** |
|
38 | - * Create a new property |
|
39 | - * @param AbstractObject $parent |
|
40 | - * @param string $definition Either a built-in type or a definition name |
|
41 | - * @param string $description description of the property |
|
42 | - * @param bool $readOnly Whether the property is read only |
|
43 | - * @throws Exception |
|
44 | - */ |
|
45 | - public function __construct(AbstractObject $parent, $definition, $description = null, $readOnly = null) |
|
46 | - { |
|
47 | - parent::__construct($parent); |
|
48 | - $this->Type = AbstractType::typeFactory($this, $definition, "Not a property: '%s'"); |
|
49 | - $this->description = $description; |
|
50 | - $this->readOnly = $readOnly; |
|
51 | - } |
|
37 | + /** |
|
38 | + * Create a new property |
|
39 | + * @param AbstractObject $parent |
|
40 | + * @param string $definition Either a built-in type or a definition name |
|
41 | + * @param string $description description of the property |
|
42 | + * @param bool $readOnly Whether the property is read only |
|
43 | + * @throws Exception |
|
44 | + */ |
|
45 | + public function __construct(AbstractObject $parent, $definition, $description = null, $readOnly = null) |
|
46 | + { |
|
47 | + parent::__construct($parent); |
|
48 | + $this->Type = AbstractType::typeFactory($this, $definition, "Not a property: '%s'"); |
|
49 | + $this->description = $description; |
|
50 | + $this->readOnly = $readOnly; |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * @param string $command The comment command |
|
55 | - * @param string $data Any data added after the command |
|
56 | - * @return self|boolean |
|
57 | - * @throws Exception |
|
58 | - */ |
|
59 | - public function handleCommand($command, $data = null) |
|
60 | - { |
|
61 | - // Pass through to Type |
|
62 | - if ($this->Type && $this->Type->handleCommand($command, $data)) { |
|
63 | - return $this; |
|
64 | - } |
|
53 | + /** |
|
54 | + * @param string $command The comment command |
|
55 | + * @param string $data Any data added after the command |
|
56 | + * @return self|boolean |
|
57 | + * @throws Exception |
|
58 | + */ |
|
59 | + public function handleCommand($command, $data = null) |
|
60 | + { |
|
61 | + // Pass through to Type |
|
62 | + if ($this->Type && $this->Type->handleCommand($command, $data)) { |
|
63 | + return $this; |
|
64 | + } |
|
65 | 65 | |
66 | - return parent::handleCommand($command, $data); |
|
67 | - } |
|
66 | + return parent::handleCommand($command, $data); |
|
67 | + } |
|
68 | 68 | |
69 | - public function toArray(): array |
|
70 | - { |
|
71 | - // Reference + readonly/description result in allOf construct |
|
72 | - // as it's semantically the same and that's what swagger tools |
|
73 | - // like swagger-ui can understand |
|
74 | - $requiresWrap = |
|
75 | - $this->Type instanceof ReferenceObjectType |
|
76 | - && (!empty($this->description) || !is_null($this->readOnly)); |
|
69 | + public function toArray(): array |
|
70 | + { |
|
71 | + // Reference + readonly/description result in allOf construct |
|
72 | + // as it's semantically the same and that's what swagger tools |
|
73 | + // like swagger-ui can understand |
|
74 | + $requiresWrap = |
|
75 | + $this->Type instanceof ReferenceObjectType |
|
76 | + && (!empty($this->description) || !is_null($this->readOnly)); |
|
77 | 77 | |
78 | - $valueType = $this->Type->toArray(); |
|
78 | + $valueType = $this->Type->toArray(); |
|
79 | 79 | |
80 | - if ($requiresWrap) { |
|
81 | - $valueType = array( |
|
82 | - 'allOf' => array($valueType), |
|
83 | - ); |
|
84 | - } |
|
80 | + if ($requiresWrap) { |
|
81 | + $valueType = array( |
|
82 | + 'allOf' => array($valueType), |
|
83 | + ); |
|
84 | + } |
|
85 | 85 | |
86 | - return self::arrayFilterNull(array_merge($valueType, array( |
|
87 | - 'description' => empty($this->description) ? null : $this->description, |
|
88 | - 'readOnly' => $this->readOnly |
|
89 | - ), parent::toArray())); |
|
90 | - } |
|
86 | + return self::arrayFilterNull(array_merge($valueType, array( |
|
87 | + 'description' => empty($this->description) ? null : $this->description, |
|
88 | + 'readOnly' => $this->readOnly |
|
89 | + ), parent::toArray())); |
|
90 | + } |
|
91 | 91 | |
92 | - public function __toString() |
|
93 | - { |
|
94 | - return __CLASS__; |
|
95 | - } |
|
92 | + public function __toString() |
|
93 | + { |
|
94 | + return __CLASS__; |
|
95 | + } |
|
96 | 96 | |
97 | 97 | } |
@@ -15,199 +15,199 @@ |
||
15 | 15 | class StringType extends AbstractType |
16 | 16 | { |
17 | 17 | |
18 | - private static $formats = array( |
|
19 | - 'string' => '', |
|
20 | - 'byte' => 'byte', |
|
21 | - 'binary' => 'binary', |
|
22 | - 'password' => 'password', |
|
23 | - 'enum' => '', |
|
24 | - ); |
|
25 | - |
|
26 | - /** |
|
27 | - * Name of the type |
|
28 | - * @var string |
|
29 | - */ |
|
30 | - protected $format = ''; |
|
31 | - protected $pattern = null; |
|
32 | - protected $default = null; |
|
33 | - protected $maxLength = null; |
|
34 | - protected $minLength = null; |
|
35 | - protected $enum = array(); |
|
36 | - |
|
37 | - /** |
|
38 | - * @throws Exception |
|
39 | - */ |
|
40 | - protected function parseDefinition($definition) |
|
41 | - { |
|
42 | - $definition = self::trim($definition); |
|
43 | - |
|
44 | - $match = array(); |
|
45 | - if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_CONTENT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
46 | - throw new Exception("Unparseable string definition: '{$definition}'"); |
|
47 | - } |
|
48 | - |
|
49 | - $this->parseFormat($definition, $match); |
|
50 | - $this->parseContent($definition, $match); |
|
51 | - $this->parseRange($definition, $match); |
|
52 | - $this->parseDefault($definition, $match); |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * @param string $definition |
|
57 | - * @param string[] $match |
|
58 | - * @throws Exception |
|
59 | - */ |
|
60 | - private function parseFormat($definition, $match) |
|
61 | - { |
|
62 | - $type = strtolower($match[1]); |
|
63 | - if (!isset(self::$formats[$type])) { |
|
64 | - throw new Exception("Not a string: '{$definition}'"); |
|
65 | - } |
|
66 | - $this->format = self::$formats[$type]; |
|
67 | - } |
|
68 | - |
|
69 | - /** |
|
70 | - * @param string $definition |
|
71 | - * @param string[] $match |
|
72 | - */ |
|
73 | - private function parseContent($definition, $match) |
|
74 | - { |
|
75 | - if (strtolower($match[1]) === 'enum') { |
|
76 | - $this->enum = explode(',', $match[2]); |
|
77 | - } else { |
|
78 | - $this->pattern = empty($match[2]) ? null : $match[2]; |
|
79 | - } |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * @param string $definition |
|
84 | - * @param string[] $match |
|
85 | - * @throws Exception |
|
86 | - * @throws Exception |
|
87 | - */ |
|
88 | - private function parseRange($definition, $match) |
|
89 | - { |
|
90 | - |
|
91 | - if (!empty($match[3])) { |
|
92 | - if ($match[1] === 'enum') { |
|
93 | - throw new Exception("Range not allowed in enumeration definition: '{$definition}'"); |
|
94 | - } |
|
95 | - if ($match[4] === '' && $match[5] === '') { |
|
96 | - throw new Exception("Empty string range: '{$definition}'"); |
|
97 | - } |
|
98 | - $exclusiveMinimum = $match[3] == '<'; |
|
99 | - $this->minLength = $match[4] === '' ? null : $match[4]; |
|
100 | - $this->maxLength = $match[5] === '' ? null : $match[5]; |
|
101 | - $exclusiveMaximum = isset($match[6]) ? ($match[6] == '>') : null; |
|
102 | - if ($this->minLength !== null && $this->maxLength !== null && $this->minLength > $this->maxLength) { |
|
103 | - self::swap($this->minLength, $this->maxLength); |
|
104 | - self::swap($exclusiveMinimum, $exclusiveMaximum); |
|
105 | - } |
|
106 | - $this->minLength = $this->minLength === null ? null : max(0, $exclusiveMinimum ? $this->minLength + 1 : $this->minLength); |
|
107 | - $this->maxLength = $this->maxLength === null ? null : max(0, $exclusiveMaximum ? $this->maxLength - 1 : $this->maxLength); |
|
108 | - } |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * @param string $definition |
|
113 | - * @param string[] $match |
|
114 | - * @throws Exception |
|
115 | - */ |
|
116 | - private function parseDefault($definition, $match) |
|
117 | - { |
|
118 | - $this->default = isset($match[7]) && $match[7] !== '' ? $this->validateDefault($match[7]) : null; |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * @param string $command The comment command |
|
123 | - * @param string $data Any data added after the command |
|
124 | - * @return AbstractType|boolean |
|
125 | - * @throws Exception |
|
126 | - * @throws Exception |
|
127 | - * @throws Exception |
|
128 | - */ |
|
129 | - public function handleCommand($command, $data = null) |
|
130 | - { |
|
131 | - switch (strtolower($command)) { |
|
132 | - case 'default': |
|
133 | - $this->default = $this->validateDefault($data); |
|
134 | - return $this; |
|
135 | - |
|
136 | - case 'pattern': |
|
137 | - $this->pattern = $data; |
|
138 | - return $this; |
|
139 | - |
|
140 | - case 'enum': |
|
141 | - if ($this->minLength !== null || $this->maxLength !== null) { |
|
142 | - throw new Exception("Enumeration not allowed in ranged string: '{$data}'"); |
|
143 | - } |
|
144 | - $words = self::wordSplit($data); |
|
145 | - $this->enum = is_array($this->enum) ? array_merge($this->enum, $words) : $words; |
|
146 | - return $this; |
|
147 | - } |
|
148 | - |
|
149 | - return parent::handleCommand($command, $data); |
|
150 | - } |
|
151 | - |
|
152 | - public function toArray(): array |
|
153 | - { |
|
154 | - return self::arrayFilterNull(array_merge(array( |
|
155 | - 'type' => 'string', |
|
156 | - 'format' => empty($this->format) ? null : $this->format, |
|
157 | - 'pattern' => $this->pattern, |
|
158 | - 'default' => $this->default, |
|
159 | - 'minLength' => $this->minLength ? (int)$this->minLength : null, |
|
160 | - 'maxLength' => $this->maxLength ? (int)$this->maxLength : null, |
|
161 | - 'enum' => $this->enum, |
|
162 | - ), parent::toArray())); |
|
163 | - } |
|
164 | - |
|
165 | - /** |
|
166 | - * Validate a default string value, depending on subtype |
|
167 | - * |
|
168 | - * @param string $value the value to validate |
|
169 | - * @return string the value after validation (might become trimmed) |
|
170 | - * @throws Exception |
|
171 | - */ |
|
172 | - protected function validateDefault($value) |
|
173 | - { |
|
174 | - if (empty($value)) { |
|
175 | - if ($this->format) { |
|
176 | - $type = $this->format; |
|
177 | - } else { |
|
178 | - $type = $this->enum ? 'enum' : 'string'; |
|
179 | - } |
|
180 | - throw new Exception("Empty {$type} default"); |
|
181 | - } |
|
182 | - |
|
183 | - if (!empty($this->enum) && !in_array($value, $this->enum)) { |
|
184 | - throw new Exception("Invalid enum default: '{$value}'"); |
|
185 | - } |
|
186 | - |
|
187 | - if ($this->maxLength !== null && mb_strlen($value) > $this->maxLength) { |
|
188 | - if ($this->format) { |
|
189 | - $type = $this->format; |
|
190 | - } else { |
|
191 | - $type = $this->enum ? 'enum' : 'string'; |
|
192 | - } |
|
193 | - throw new Exception("Default {$type} length beyond maximum: '{$value}'"); |
|
194 | - } |
|
195 | - |
|
196 | - if ($this->minLength !== null && mb_strlen($value) < $this->minLength) { |
|
197 | - if ($this->format) { |
|
198 | - $type = $this->format; |
|
199 | - } else { |
|
200 | - $type = $this->enum ? 'enum' : 'string'; |
|
201 | - } |
|
202 | - throw new Exception("Default {$type} length beyond minimum: '{$value}'"); |
|
203 | - } |
|
204 | - |
|
205 | - return $value; |
|
206 | - } |
|
207 | - |
|
208 | - public function __toString() |
|
209 | - { |
|
210 | - return __CLASS__; |
|
211 | - } |
|
18 | + private static $formats = array( |
|
19 | + 'string' => '', |
|
20 | + 'byte' => 'byte', |
|
21 | + 'binary' => 'binary', |
|
22 | + 'password' => 'password', |
|
23 | + 'enum' => '', |
|
24 | + ); |
|
25 | + |
|
26 | + /** |
|
27 | + * Name of the type |
|
28 | + * @var string |
|
29 | + */ |
|
30 | + protected $format = ''; |
|
31 | + protected $pattern = null; |
|
32 | + protected $default = null; |
|
33 | + protected $maxLength = null; |
|
34 | + protected $minLength = null; |
|
35 | + protected $enum = array(); |
|
36 | + |
|
37 | + /** |
|
38 | + * @throws Exception |
|
39 | + */ |
|
40 | + protected function parseDefinition($definition) |
|
41 | + { |
|
42 | + $definition = self::trim($definition); |
|
43 | + |
|
44 | + $match = array(); |
|
45 | + if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_CONTENT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
46 | + throw new Exception("Unparseable string definition: '{$definition}'"); |
|
47 | + } |
|
48 | + |
|
49 | + $this->parseFormat($definition, $match); |
|
50 | + $this->parseContent($definition, $match); |
|
51 | + $this->parseRange($definition, $match); |
|
52 | + $this->parseDefault($definition, $match); |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * @param string $definition |
|
57 | + * @param string[] $match |
|
58 | + * @throws Exception |
|
59 | + */ |
|
60 | + private function parseFormat($definition, $match) |
|
61 | + { |
|
62 | + $type = strtolower($match[1]); |
|
63 | + if (!isset(self::$formats[$type])) { |
|
64 | + throw new Exception("Not a string: '{$definition}'"); |
|
65 | + } |
|
66 | + $this->format = self::$formats[$type]; |
|
67 | + } |
|
68 | + |
|
69 | + /** |
|
70 | + * @param string $definition |
|
71 | + * @param string[] $match |
|
72 | + */ |
|
73 | + private function parseContent($definition, $match) |
|
74 | + { |
|
75 | + if (strtolower($match[1]) === 'enum') { |
|
76 | + $this->enum = explode(',', $match[2]); |
|
77 | + } else { |
|
78 | + $this->pattern = empty($match[2]) ? null : $match[2]; |
|
79 | + } |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * @param string $definition |
|
84 | + * @param string[] $match |
|
85 | + * @throws Exception |
|
86 | + * @throws Exception |
|
87 | + */ |
|
88 | + private function parseRange($definition, $match) |
|
89 | + { |
|
90 | + |
|
91 | + if (!empty($match[3])) { |
|
92 | + if ($match[1] === 'enum') { |
|
93 | + throw new Exception("Range not allowed in enumeration definition: '{$definition}'"); |
|
94 | + } |
|
95 | + if ($match[4] === '' && $match[5] === '') { |
|
96 | + throw new Exception("Empty string range: '{$definition}'"); |
|
97 | + } |
|
98 | + $exclusiveMinimum = $match[3] == '<'; |
|
99 | + $this->minLength = $match[4] === '' ? null : $match[4]; |
|
100 | + $this->maxLength = $match[5] === '' ? null : $match[5]; |
|
101 | + $exclusiveMaximum = isset($match[6]) ? ($match[6] == '>') : null; |
|
102 | + if ($this->minLength !== null && $this->maxLength !== null && $this->minLength > $this->maxLength) { |
|
103 | + self::swap($this->minLength, $this->maxLength); |
|
104 | + self::swap($exclusiveMinimum, $exclusiveMaximum); |
|
105 | + } |
|
106 | + $this->minLength = $this->minLength === null ? null : max(0, $exclusiveMinimum ? $this->minLength + 1 : $this->minLength); |
|
107 | + $this->maxLength = $this->maxLength === null ? null : max(0, $exclusiveMaximum ? $this->maxLength - 1 : $this->maxLength); |
|
108 | + } |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * @param string $definition |
|
113 | + * @param string[] $match |
|
114 | + * @throws Exception |
|
115 | + */ |
|
116 | + private function parseDefault($definition, $match) |
|
117 | + { |
|
118 | + $this->default = isset($match[7]) && $match[7] !== '' ? $this->validateDefault($match[7]) : null; |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * @param string $command The comment command |
|
123 | + * @param string $data Any data added after the command |
|
124 | + * @return AbstractType|boolean |
|
125 | + * @throws Exception |
|
126 | + * @throws Exception |
|
127 | + * @throws Exception |
|
128 | + */ |
|
129 | + public function handleCommand($command, $data = null) |
|
130 | + { |
|
131 | + switch (strtolower($command)) { |
|
132 | + case 'default': |
|
133 | + $this->default = $this->validateDefault($data); |
|
134 | + return $this; |
|
135 | + |
|
136 | + case 'pattern': |
|
137 | + $this->pattern = $data; |
|
138 | + return $this; |
|
139 | + |
|
140 | + case 'enum': |
|
141 | + if ($this->minLength !== null || $this->maxLength !== null) { |
|
142 | + throw new Exception("Enumeration not allowed in ranged string: '{$data}'"); |
|
143 | + } |
|
144 | + $words = self::wordSplit($data); |
|
145 | + $this->enum = is_array($this->enum) ? array_merge($this->enum, $words) : $words; |
|
146 | + return $this; |
|
147 | + } |
|
148 | + |
|
149 | + return parent::handleCommand($command, $data); |
|
150 | + } |
|
151 | + |
|
152 | + public function toArray(): array |
|
153 | + { |
|
154 | + return self::arrayFilterNull(array_merge(array( |
|
155 | + 'type' => 'string', |
|
156 | + 'format' => empty($this->format) ? null : $this->format, |
|
157 | + 'pattern' => $this->pattern, |
|
158 | + 'default' => $this->default, |
|
159 | + 'minLength' => $this->minLength ? (int)$this->minLength : null, |
|
160 | + 'maxLength' => $this->maxLength ? (int)$this->maxLength : null, |
|
161 | + 'enum' => $this->enum, |
|
162 | + ), parent::toArray())); |
|
163 | + } |
|
164 | + |
|
165 | + /** |
|
166 | + * Validate a default string value, depending on subtype |
|
167 | + * |
|
168 | + * @param string $value the value to validate |
|
169 | + * @return string the value after validation (might become trimmed) |
|
170 | + * @throws Exception |
|
171 | + */ |
|
172 | + protected function validateDefault($value) |
|
173 | + { |
|
174 | + if (empty($value)) { |
|
175 | + if ($this->format) { |
|
176 | + $type = $this->format; |
|
177 | + } else { |
|
178 | + $type = $this->enum ? 'enum' : 'string'; |
|
179 | + } |
|
180 | + throw new Exception("Empty {$type} default"); |
|
181 | + } |
|
182 | + |
|
183 | + if (!empty($this->enum) && !in_array($value, $this->enum)) { |
|
184 | + throw new Exception("Invalid enum default: '{$value}'"); |
|
185 | + } |
|
186 | + |
|
187 | + if ($this->maxLength !== null && mb_strlen($value) > $this->maxLength) { |
|
188 | + if ($this->format) { |
|
189 | + $type = $this->format; |
|
190 | + } else { |
|
191 | + $type = $this->enum ? 'enum' : 'string'; |
|
192 | + } |
|
193 | + throw new Exception("Default {$type} length beyond maximum: '{$value}'"); |
|
194 | + } |
|
195 | + |
|
196 | + if ($this->minLength !== null && mb_strlen($value) < $this->minLength) { |
|
197 | + if ($this->format) { |
|
198 | + $type = $this->format; |
|
199 | + } else { |
|
200 | + $type = $this->enum ? 'enum' : 'string'; |
|
201 | + } |
|
202 | + throw new Exception("Default {$type} length beyond minimum: '{$value}'"); |
|
203 | + } |
|
204 | + |
|
205 | + return $value; |
|
206 | + } |
|
207 | + |
|
208 | + public function __toString() |
|
209 | + { |
|
210 | + return __CLASS__; |
|
211 | + } |
|
212 | 212 | |
213 | 213 | } |
@@ -156,8 +156,8 @@ |
||
156 | 156 | 'format' => empty($this->format) ? null : $this->format, |
157 | 157 | 'pattern' => $this->pattern, |
158 | 158 | 'default' => $this->default, |
159 | - 'minLength' => $this->minLength ? (int)$this->minLength : null, |
|
160 | - 'maxLength' => $this->maxLength ? (int)$this->maxLength : null, |
|
159 | + 'minLength' => $this->minLength ? (int) $this->minLength : null, |
|
160 | + 'maxLength' => $this->maxLength ? (int) $this->maxLength : null, |
|
161 | 161 | 'enum' => $this->enum, |
162 | 162 | ), parent::toArray())); |
163 | 163 | } |
@@ -177,7 +177,7 @@ discard block |
||
177 | 177 | } else { |
178 | 178 | $type = $this->enum ? 'enum' : 'string'; |
179 | 179 | } |
180 | - throw new Exception("Empty {$type} default"); |
|
180 | + throw new Exception("empty {$type} default"); |
|
181 | 181 | } |
182 | 182 | |
183 | 183 | if (!empty($this->enum) && !in_array($value, $this->enum)) { |
@@ -190,7 +190,7 @@ discard block |
||
190 | 190 | } else { |
191 | 191 | $type = $this->enum ? 'enum' : 'string'; |
192 | 192 | } |
193 | - throw new Exception("Default {$type} length beyond maximum: '{$value}'"); |
|
193 | + throw new Exception("default {$type} length beyond maximum: '{$value}'"); |
|
194 | 194 | } |
195 | 195 | |
196 | 196 | if ($this->minLength !== null && mb_strlen($value) < $this->minLength) { |
@@ -199,7 +199,7 @@ discard block |
||
199 | 199 | } else { |
200 | 200 | $type = $this->enum ? 'enum' : 'string'; |
201 | 201 | } |
202 | - throw new Exception("Default {$type} length beyond minimum: '{$value}'"); |
|
202 | + throw new Exception("default {$type} length beyond minimum: '{$value}'"); |
|
203 | 203 | } |
204 | 204 | |
205 | 205 | return $value; |
@@ -15,48 +15,48 @@ |
||
15 | 15 | class ReferenceObjectType extends AbstractType |
16 | 16 | { |
17 | 17 | |
18 | - private $reference = null; |
|
19 | - |
|
20 | - /** |
|
21 | - * @throws Exception |
|
22 | - */ |
|
23 | - protected function parseDefinition($definition) |
|
24 | - { |
|
25 | - $definition = self::trim($definition); |
|
26 | - |
|
27 | - $match = array(); |
|
28 | - if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_CONTENT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
29 | - throw new Exception('Unparseable string definition: \'' . $definition . '\''); |
|
30 | - } |
|
31 | - |
|
32 | - $type = strtolower($match[1]); |
|
33 | - |
|
34 | - $reference = null; |
|
35 | - if ($type === 'refobject') { |
|
36 | - if (isset($match[2])) { |
|
37 | - $reference = $match[2]; |
|
38 | - } |
|
39 | - } else { |
|
40 | - $reference = $match[1]; |
|
41 | - } |
|
42 | - |
|
43 | - if (empty($reference)) { |
|
44 | - throw new Exception('Referenced object name missing: \'' . $definition . '\''); |
|
45 | - } |
|
46 | - |
|
47 | - $this->reference = $reference; |
|
48 | - } |
|
49 | - |
|
50 | - public function toArray(): array |
|
51 | - { |
|
52 | - return self::arrayFilterNull(array_merge(array( |
|
53 | - '$ref' => '#/definitions/' . $this->reference, |
|
54 | - ), parent::toArray())); |
|
55 | - } |
|
56 | - |
|
57 | - public function __toString() |
|
58 | - { |
|
59 | - return __CLASS__ . ' ' . $this->reference; |
|
60 | - } |
|
18 | + private $reference = null; |
|
19 | + |
|
20 | + /** |
|
21 | + * @throws Exception |
|
22 | + */ |
|
23 | + protected function parseDefinition($definition) |
|
24 | + { |
|
25 | + $definition = self::trim($definition); |
|
26 | + |
|
27 | + $match = array(); |
|
28 | + if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_CONTENT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
29 | + throw new Exception('Unparseable string definition: \'' . $definition . '\''); |
|
30 | + } |
|
31 | + |
|
32 | + $type = strtolower($match[1]); |
|
33 | + |
|
34 | + $reference = null; |
|
35 | + if ($type === 'refobject') { |
|
36 | + if (isset($match[2])) { |
|
37 | + $reference = $match[2]; |
|
38 | + } |
|
39 | + } else { |
|
40 | + $reference = $match[1]; |
|
41 | + } |
|
42 | + |
|
43 | + if (empty($reference)) { |
|
44 | + throw new Exception('Referenced object name missing: \'' . $definition . '\''); |
|
45 | + } |
|
46 | + |
|
47 | + $this->reference = $reference; |
|
48 | + } |
|
49 | + |
|
50 | + public function toArray(): array |
|
51 | + { |
|
52 | + return self::arrayFilterNull(array_merge(array( |
|
53 | + '$ref' => '#/definitions/' . $this->reference, |
|
54 | + ), parent::toArray())); |
|
55 | + } |
|
56 | + |
|
57 | + public function __toString() |
|
58 | + { |
|
59 | + return __CLASS__ . ' ' . $this->reference; |
|
60 | + } |
|
61 | 61 | |
62 | 62 | } |
@@ -13,47 +13,47 @@ |
||
13 | 13 | class Tag extends AbstractDocumentableObject |
14 | 14 | { |
15 | 15 | |
16 | - private $name; |
|
17 | - private $description; |
|
18 | - |
|
19 | - public function __construct(AbstractObject $parent, $name, $description = null) |
|
20 | - { |
|
21 | - parent::__construct($parent); |
|
22 | - $this->name = $name; |
|
23 | - $this->description = $description; |
|
24 | - } |
|
25 | - |
|
26 | - /** |
|
27 | - * @param string $command |
|
28 | - * @param string $data |
|
29 | - * @return AbstractObject|boolean |
|
30 | - */ |
|
31 | - public function handleCommand($command, $data = null) |
|
32 | - { |
|
33 | - if (strtolower($command) === 'description') { |
|
34 | - $this->description = $data; |
|
35 | - return $this; |
|
36 | - } |
|
37 | - |
|
38 | - return parent::handleCommand($command, $data); |
|
39 | - } |
|
40 | - |
|
41 | - public function toArray(): array |
|
42 | - { |
|
43 | - return self::arrayFilterNull(array_merge(array( |
|
44 | - 'name' => $this->name, |
|
45 | - 'description' => empty($this->description) ? null : $this->description, |
|
46 | - ), parent::toArray())); |
|
47 | - } |
|
48 | - |
|
49 | - public function getName() |
|
50 | - { |
|
51 | - return $this->name; |
|
52 | - } |
|
53 | - |
|
54 | - public function __toString() |
|
55 | - { |
|
56 | - return __CLASS__ . ' ' . $this->name; |
|
57 | - } |
|
16 | + private $name; |
|
17 | + private $description; |
|
18 | + |
|
19 | + public function __construct(AbstractObject $parent, $name, $description = null) |
|
20 | + { |
|
21 | + parent::__construct($parent); |
|
22 | + $this->name = $name; |
|
23 | + $this->description = $description; |
|
24 | + } |
|
25 | + |
|
26 | + /** |
|
27 | + * @param string $command |
|
28 | + * @param string $data |
|
29 | + * @return AbstractObject|boolean |
|
30 | + */ |
|
31 | + public function handleCommand($command, $data = null) |
|
32 | + { |
|
33 | + if (strtolower($command) === 'description') { |
|
34 | + $this->description = $data; |
|
35 | + return $this; |
|
36 | + } |
|
37 | + |
|
38 | + return parent::handleCommand($command, $data); |
|
39 | + } |
|
40 | + |
|
41 | + public function toArray(): array |
|
42 | + { |
|
43 | + return self::arrayFilterNull(array_merge(array( |
|
44 | + 'name' => $this->name, |
|
45 | + 'description' => empty($this->description) ? null : $this->description, |
|
46 | + ), parent::toArray())); |
|
47 | + } |
|
48 | + |
|
49 | + public function getName() |
|
50 | + { |
|
51 | + return $this->name; |
|
52 | + } |
|
53 | + |
|
54 | + public function __toString() |
|
55 | + { |
|
56 | + return __CLASS__ . ' ' . $this->name; |
|
57 | + } |
|
58 | 58 | |
59 | 59 | } |
@@ -16,162 +16,162 @@ |
||
16 | 16 | abstract class AbstractObject |
17 | 17 | { |
18 | 18 | |
19 | - private static $mime_types = array( |
|
20 | - 'fileform' => 'multipart/form-data', |
|
21 | - 'form' => 'application/x-www-form-urlencoded', |
|
22 | - 'json' => 'application/json', |
|
23 | - 'text' => 'text/plain', |
|
24 | - 'utf8' => 'text/plain; charset=utf-8', |
|
25 | - 'yml' => 'application/x-yaml', |
|
26 | - 'yaml' => 'application/x-yaml', |
|
27 | - 'php' => 'text/x-php', |
|
28 | - 'xml' => 'text/xml', |
|
29 | - ); |
|
30 | - |
|
31 | - /** |
|
32 | - * @var AbstractObject |
|
33 | - */ |
|
34 | - private $parent; |
|
35 | - |
|
36 | - /** |
|
37 | - * Map of extensions and their (trimmed) values |
|
38 | - * @var string[] |
|
39 | - */ |
|
40 | - private $extensions = array(); |
|
41 | - |
|
42 | - public function __construct(AbstractObject $parent = null) |
|
43 | - { |
|
44 | - $this->parent = $parent; |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * @return AbstractObject|null |
|
49 | - */ |
|
50 | - protected function getParent(): ?AbstractObject |
|
51 | - { |
|
52 | - return $this->parent; |
|
53 | - } |
|
54 | - |
|
55 | - protected function getParentClass($classname): AbstractObject |
|
56 | - { |
|
57 | - if (is_a($this, $classname)) { |
|
58 | - return $this; |
|
59 | - } |
|
60 | - return $this->parent->getParentClass($classname); |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * @return Swagger |
|
65 | - */ |
|
66 | - protected function getSwagger(): Swagger |
|
67 | - { |
|
68 | - return $this->parent->getSwagger(); |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * @return TypeRegistry |
|
73 | - */ |
|
74 | - protected function getTypeRegistry(): TypeRegistry |
|
75 | - { |
|
76 | - return $this->parent->getTypeRegistry(); |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * @param string $command |
|
81 | - * @param string $data |
|
82 | - * @return AbstractObject|boolean |
|
83 | - */ |
|
84 | - public function handleCommand($command, $data = null) |
|
85 | - { |
|
86 | - if (stripos($command, 'x-') === 0) { |
|
87 | - $this->extensions[$command] = empty($data) ? $data : trim($data); |
|
88 | - return $this; |
|
89 | - } |
|
90 | - |
|
91 | - return false; |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * @return array |
|
96 | - */ |
|
97 | - public function toArray(): array |
|
98 | - { |
|
99 | - return $this->extensions; |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * Translate consumes from shortcuts |
|
104 | - * @param String[] $mimeTypes |
|
105 | - * @return String[] |
|
106 | - */ |
|
107 | - protected static function translateMimeTypes($mimeTypes): array |
|
108 | - { |
|
109 | - foreach ($mimeTypes as &$mimeType) { |
|
110 | - if (isset(self::$mime_types[strtolower($mimeType)])) { |
|
111 | - $mimeType = self::$mime_types[strtolower($mimeType)]; |
|
112 | - } |
|
113 | - } |
|
114 | - |
|
115 | - return $mimeTypes; |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * Trim whitespace from a multibyte string |
|
120 | - * @param string $string |
|
121 | - * @return string |
|
122 | - */ |
|
123 | - public static function trim($string): string |
|
124 | - { |
|
125 | - return mb_ereg_replace('^\s*([\s\S]*?)\s*$', '\1', $string); |
|
126 | - } |
|
127 | - |
|
128 | - /** |
|
129 | - * Filter all items from an array where the value is either null or an |
|
130 | - * empty array. |
|
131 | - * @param array $array |
|
132 | - * @return array |
|
133 | - */ |
|
134 | - public static function arrayFilterNull($array): array |
|
135 | - { |
|
136 | - return array_filter($array, static function ($value) { |
|
137 | - return $value !== null && $value !== array(); |
|
138 | - }); |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * Recursively call toArray() on all objects to return an array |
|
143 | - * @param array $array |
|
144 | - * @return array |
|
145 | - */ |
|
146 | - public static function objectsToArray($array): array |
|
147 | - { |
|
148 | - return array_map(static function (AbstractObject $item) { |
|
149 | - return $item->toArray(); |
|
150 | - }, $array); |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * Shifts the first word off a text line and returns it |
|
155 | - * @param string $data |
|
156 | - * @return string|bool Either the first word or false if no more words available |
|
157 | - */ |
|
158 | - public static function wordShift(&$data) |
|
159 | - { |
|
160 | - if (preg_match('~^\s*(\S+)\s*(.*)$~s', $data, $matches) === 1) { |
|
161 | - $data = $matches[2]; |
|
162 | - return $matches[1]; |
|
163 | - } |
|
164 | - return false; |
|
165 | - } |
|
166 | - |
|
167 | - /** |
|
168 | - * Splits a text line in all it's words |
|
169 | - * @param string $data |
|
170 | - * @return string[] |
|
171 | - */ |
|
172 | - public static function wordSplit($data): array |
|
173 | - { |
|
174 | - return array_values(preg_grep('~\S~', preg_split('~\s+~', $data))); |
|
175 | - } |
|
19 | + private static $mime_types = array( |
|
20 | + 'fileform' => 'multipart/form-data', |
|
21 | + 'form' => 'application/x-www-form-urlencoded', |
|
22 | + 'json' => 'application/json', |
|
23 | + 'text' => 'text/plain', |
|
24 | + 'utf8' => 'text/plain; charset=utf-8', |
|
25 | + 'yml' => 'application/x-yaml', |
|
26 | + 'yaml' => 'application/x-yaml', |
|
27 | + 'php' => 'text/x-php', |
|
28 | + 'xml' => 'text/xml', |
|
29 | + ); |
|
30 | + |
|
31 | + /** |
|
32 | + * @var AbstractObject |
|
33 | + */ |
|
34 | + private $parent; |
|
35 | + |
|
36 | + /** |
|
37 | + * Map of extensions and their (trimmed) values |
|
38 | + * @var string[] |
|
39 | + */ |
|
40 | + private $extensions = array(); |
|
41 | + |
|
42 | + public function __construct(AbstractObject $parent = null) |
|
43 | + { |
|
44 | + $this->parent = $parent; |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * @return AbstractObject|null |
|
49 | + */ |
|
50 | + protected function getParent(): ?AbstractObject |
|
51 | + { |
|
52 | + return $this->parent; |
|
53 | + } |
|
54 | + |
|
55 | + protected function getParentClass($classname): AbstractObject |
|
56 | + { |
|
57 | + if (is_a($this, $classname)) { |
|
58 | + return $this; |
|
59 | + } |
|
60 | + return $this->parent->getParentClass($classname); |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * @return Swagger |
|
65 | + */ |
|
66 | + protected function getSwagger(): Swagger |
|
67 | + { |
|
68 | + return $this->parent->getSwagger(); |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * @return TypeRegistry |
|
73 | + */ |
|
74 | + protected function getTypeRegistry(): TypeRegistry |
|
75 | + { |
|
76 | + return $this->parent->getTypeRegistry(); |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * @param string $command |
|
81 | + * @param string $data |
|
82 | + * @return AbstractObject|boolean |
|
83 | + */ |
|
84 | + public function handleCommand($command, $data = null) |
|
85 | + { |
|
86 | + if (stripos($command, 'x-') === 0) { |
|
87 | + $this->extensions[$command] = empty($data) ? $data : trim($data); |
|
88 | + return $this; |
|
89 | + } |
|
90 | + |
|
91 | + return false; |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * @return array |
|
96 | + */ |
|
97 | + public function toArray(): array |
|
98 | + { |
|
99 | + return $this->extensions; |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * Translate consumes from shortcuts |
|
104 | + * @param String[] $mimeTypes |
|
105 | + * @return String[] |
|
106 | + */ |
|
107 | + protected static function translateMimeTypes($mimeTypes): array |
|
108 | + { |
|
109 | + foreach ($mimeTypes as &$mimeType) { |
|
110 | + if (isset(self::$mime_types[strtolower($mimeType)])) { |
|
111 | + $mimeType = self::$mime_types[strtolower($mimeType)]; |
|
112 | + } |
|
113 | + } |
|
114 | + |
|
115 | + return $mimeTypes; |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * Trim whitespace from a multibyte string |
|
120 | + * @param string $string |
|
121 | + * @return string |
|
122 | + */ |
|
123 | + public static function trim($string): string |
|
124 | + { |
|
125 | + return mb_ereg_replace('^\s*([\s\S]*?)\s*$', '\1', $string); |
|
126 | + } |
|
127 | + |
|
128 | + /** |
|
129 | + * Filter all items from an array where the value is either null or an |
|
130 | + * empty array. |
|
131 | + * @param array $array |
|
132 | + * @return array |
|
133 | + */ |
|
134 | + public static function arrayFilterNull($array): array |
|
135 | + { |
|
136 | + return array_filter($array, static function ($value) { |
|
137 | + return $value !== null && $value !== array(); |
|
138 | + }); |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * Recursively call toArray() on all objects to return an array |
|
143 | + * @param array $array |
|
144 | + * @return array |
|
145 | + */ |
|
146 | + public static function objectsToArray($array): array |
|
147 | + { |
|
148 | + return array_map(static function (AbstractObject $item) { |
|
149 | + return $item->toArray(); |
|
150 | + }, $array); |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * Shifts the first word off a text line and returns it |
|
155 | + * @param string $data |
|
156 | + * @return string|bool Either the first word or false if no more words available |
|
157 | + */ |
|
158 | + public static function wordShift(&$data) |
|
159 | + { |
|
160 | + if (preg_match('~^\s*(\S+)\s*(.*)$~s', $data, $matches) === 1) { |
|
161 | + $data = $matches[2]; |
|
162 | + return $matches[1]; |
|
163 | + } |
|
164 | + return false; |
|
165 | + } |
|
166 | + |
|
167 | + /** |
|
168 | + * Splits a text line in all it's words |
|
169 | + * @param string $data |
|
170 | + * @return string[] |
|
171 | + */ |
|
172 | + public static function wordSplit($data): array |
|
173 | + { |
|
174 | + return array_values(preg_grep('~\S~', preg_split('~\s+~', $data))); |
|
175 | + } |
|
176 | 176 | |
177 | 177 | } |
@@ -133,7 +133,7 @@ discard block |
||
133 | 133 | */ |
134 | 134 | public static function arrayFilterNull($array): array |
135 | 135 | { |
136 | - return array_filter($array, static function ($value) { |
|
136 | + return array_filter($array, static function($value) { |
|
137 | 137 | return $value !== null && $value !== array(); |
138 | 138 | }); |
139 | 139 | } |
@@ -145,7 +145,7 @@ discard block |
||
145 | 145 | */ |
146 | 146 | public static function objectsToArray($array): array |
147 | 147 | { |
148 | - return array_map(static function (AbstractObject $item) { |
|
148 | + return array_map(static function(AbstractObject $item) { |
|
149 | 149 | return $item->toArray(); |
150 | 150 | }, $array); |
151 | 151 | } |
@@ -17,88 +17,88 @@ |
||
17 | 17 | class Schema extends AbstractDocumentableObject implements IDefinition |
18 | 18 | { |
19 | 19 | |
20 | - /** |
|
21 | - * @var string |
|
22 | - */ |
|
23 | - private $description; |
|
24 | - |
|
25 | - /** |
|
26 | - * @var string |
|
27 | - */ |
|
28 | - private $title = null; |
|
29 | - |
|
30 | - /** |
|
31 | - * @var bool |
|
32 | - */ |
|
33 | - private $readOnly = null; |
|
34 | - |
|
35 | - /** |
|
36 | - * @var AbstractType |
|
37 | - */ |
|
38 | - private $type; |
|
39 | - |
|
40 | - /** |
|
41 | - * @param string $description |
|
42 | - * @throws Exception |
|
43 | - */ |
|
44 | - public function __construct(AbstractObject $parent, $definition = 'object', $description = null) |
|
45 | - { |
|
46 | - parent::__construct($parent); |
|
47 | - |
|
48 | - // Check if definition set |
|
49 | - if ($this->getSwagger()->hasDefinition($definition)) { |
|
50 | - $this->type = new Type\ReferenceObjectType($this, $definition); |
|
51 | - } else { |
|
52 | - $this->type = Type\AbstractType::typeFactory($this, $definition); |
|
53 | - } |
|
54 | - |
|
55 | - $this->description = $description; |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * @param string $command |
|
60 | - * @param string $data |
|
61 | - * @return AbstractObject|boolean |
|
62 | - * @throws Exception |
|
63 | - */ |
|
64 | - public function handleCommand($command, $data = null) |
|
65 | - { |
|
66 | - // Pass through to Type |
|
67 | - if ($this->type && $this->type->handleCommand($command, $data)) { |
|
68 | - return $this; |
|
69 | - } |
|
70 | - |
|
71 | - // handle all the rest manually |
|
72 | - switch (strtolower($command)) { |
|
73 | - case 'description': |
|
74 | - $this->description = $data; |
|
75 | - return $this; |
|
76 | - |
|
77 | - case 'title': |
|
78 | - $this->title = $data; |
|
79 | - return $this; |
|
80 | - } |
|
81 | - |
|
82 | - return parent::handleCommand($command, $data); |
|
83 | - } |
|
84 | - |
|
85 | - public function toArray(): array |
|
86 | - { |
|
87 | - return self::arrayFilterNull(array_merge($this->type->toArray(), array( |
|
88 | - 'title' => empty($this->title) ? null : $this->title, |
|
89 | - 'description' => empty($this->description) ? null : $this->description, |
|
90 | - 'readOnly' => $this->readOnly |
|
91 | - ), parent::toArray())); |
|
92 | - } |
|
93 | - |
|
94 | - public function __toString() |
|
95 | - { |
|
96 | - return __CLASS__; |
|
97 | - } |
|
98 | - |
|
99 | - public function setReadOnly() |
|
100 | - { |
|
101 | - $this->readOnly = true; |
|
102 | - } |
|
20 | + /** |
|
21 | + * @var string |
|
22 | + */ |
|
23 | + private $description; |
|
24 | + |
|
25 | + /** |
|
26 | + * @var string |
|
27 | + */ |
|
28 | + private $title = null; |
|
29 | + |
|
30 | + /** |
|
31 | + * @var bool |
|
32 | + */ |
|
33 | + private $readOnly = null; |
|
34 | + |
|
35 | + /** |
|
36 | + * @var AbstractType |
|
37 | + */ |
|
38 | + private $type; |
|
39 | + |
|
40 | + /** |
|
41 | + * @param string $description |
|
42 | + * @throws Exception |
|
43 | + */ |
|
44 | + public function __construct(AbstractObject $parent, $definition = 'object', $description = null) |
|
45 | + { |
|
46 | + parent::__construct($parent); |
|
47 | + |
|
48 | + // Check if definition set |
|
49 | + if ($this->getSwagger()->hasDefinition($definition)) { |
|
50 | + $this->type = new Type\ReferenceObjectType($this, $definition); |
|
51 | + } else { |
|
52 | + $this->type = Type\AbstractType::typeFactory($this, $definition); |
|
53 | + } |
|
54 | + |
|
55 | + $this->description = $description; |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * @param string $command |
|
60 | + * @param string $data |
|
61 | + * @return AbstractObject|boolean |
|
62 | + * @throws Exception |
|
63 | + */ |
|
64 | + public function handleCommand($command, $data = null) |
|
65 | + { |
|
66 | + // Pass through to Type |
|
67 | + if ($this->type && $this->type->handleCommand($command, $data)) { |
|
68 | + return $this; |
|
69 | + } |
|
70 | + |
|
71 | + // handle all the rest manually |
|
72 | + switch (strtolower($command)) { |
|
73 | + case 'description': |
|
74 | + $this->description = $data; |
|
75 | + return $this; |
|
76 | + |
|
77 | + case 'title': |
|
78 | + $this->title = $data; |
|
79 | + return $this; |
|
80 | + } |
|
81 | + |
|
82 | + return parent::handleCommand($command, $data); |
|
83 | + } |
|
84 | + |
|
85 | + public function toArray(): array |
|
86 | + { |
|
87 | + return self::arrayFilterNull(array_merge($this->type->toArray(), array( |
|
88 | + 'title' => empty($this->title) ? null : $this->title, |
|
89 | + 'description' => empty($this->description) ? null : $this->description, |
|
90 | + 'readOnly' => $this->readOnly |
|
91 | + ), parent::toArray())); |
|
92 | + } |
|
93 | + |
|
94 | + public function __toString() |
|
95 | + { |
|
96 | + return __CLASS__; |
|
97 | + } |
|
98 | + |
|
99 | + public function setReadOnly() |
|
100 | + { |
|
101 | + $this->readOnly = true; |
|
102 | + } |
|
103 | 103 | |
104 | 104 | } |
@@ -16,132 +16,132 @@ |
||
16 | 16 | class SecurityScheme extends AbstractObject |
17 | 17 | { |
18 | 18 | |
19 | - /** |
|
20 | - * 'basic', 'apikey' or 'oauth2' |
|
21 | - * @var string |
|
22 | - */ |
|
23 | - private $type; |
|
24 | - |
|
25 | - /** |
|
26 | - * @var string |
|
27 | - */ |
|
28 | - private $description; |
|
29 | - private $name; |
|
30 | - private $in; |
|
31 | - private $flow; |
|
32 | - private $authorizationUrl; |
|
33 | - private $tokenUrl; |
|
34 | - |
|
35 | - /** |
|
36 | - * Map of scope-name => description |
|
37 | - * @var [] |
|
38 | - */ |
|
39 | - private $scopes = array(); |
|
40 | - |
|
41 | - /** |
|
42 | - * Create a new SecurityScheme object |
|
43 | - * @param AbstractObject $parent |
|
44 | - * @param string $type |
|
45 | - * @param string $data |
|
46 | - * @throws Exception |
|
47 | - */ |
|
48 | - public function __construct(AbstractObject $parent, $type, $data = null) |
|
49 | - { |
|
50 | - parent::__construct($parent); |
|
51 | - |
|
52 | - if (!in_array(strtolower($type), array('basic', 'apikey', 'oauth2'))) { |
|
53 | - throw new Exception("Security scheme type must be either 'basic', 'apiKey' or 'oauth2', not '{$type}'"); |
|
54 | - } |
|
55 | - $this->type = strtolower($type); |
|
56 | - |
|
57 | - switch ($this->type) { |
|
58 | - case 'basic': |
|
59 | - $this->description = $data; |
|
60 | - break; |
|
61 | - |
|
62 | - case 'apikey': |
|
63 | - $this->name = self::wordShift($data); |
|
64 | - |
|
65 | - $in = strtolower(self::wordShift($data)); |
|
66 | - if (!in_array($in, array('query', 'header'))) { |
|
67 | - throw new Exception("ApiKey in must be either 'query' or 'header', not '{$in}'"); |
|
68 | - } |
|
69 | - $this->in = $in; |
|
70 | - |
|
71 | - $this->description = $data; |
|
72 | - break; |
|
73 | - |
|
74 | - case 'oauth2': |
|
75 | - $flow = strtolower(self::wordShift($data)); |
|
76 | - if (!in_array($flow, array('implicit', 'password', 'application', 'accesscode'))) { |
|
77 | - throw new Exception("OAuth2 flow must be either 'implicit', 'password', 'application' or 'accesscode', not '{$flow}'"); |
|
78 | - } |
|
79 | - $this->flow = $flow; |
|
80 | - |
|
81 | - if (in_array($flow, array('implicit', 'accesscode'))) { |
|
82 | - $authUrl = self::wordShift($data); |
|
83 | - if (!filter_var($authUrl, FILTER_VALIDATE_URL)) { |
|
84 | - throw new Exception("OAuth2 authorization URL invalid: '{$authUrl}'"); |
|
85 | - } |
|
86 | - $this->authorizationUrl = $authUrl; |
|
87 | - } |
|
88 | - |
|
89 | - if (in_array($flow, array('password', 'application', 'accesscode'))) { |
|
90 | - $tokenUrl = self::wordShift($data); |
|
91 | - if (!filter_var($tokenUrl, FILTER_VALIDATE_URL)) { |
|
92 | - throw new Exception("OAuth2 token URL invalid: '{$tokenUrl}'"); |
|
93 | - } |
|
94 | - $this->tokenUrl = $tokenUrl; |
|
95 | - } |
|
96 | - |
|
97 | - $this->description = $data; |
|
98 | - break; |
|
99 | - } |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * @param string $command |
|
104 | - * @param string $data |
|
105 | - * @return AbstractObject|boolean |
|
106 | - * @throws Exception |
|
107 | - */ |
|
108 | - public function handleCommand($command, $data = null) |
|
109 | - { |
|
110 | - switch (strtolower($command)) { |
|
111 | - case 'description': |
|
112 | - $this->description = $data; |
|
113 | - return $this; |
|
114 | - |
|
115 | - case 'scope': |
|
116 | - if ($this->type !== 'oauth2') { |
|
117 | - throw new Exception("Cannot set scope on type '{$this->type}'"); |
|
118 | - } |
|
119 | - |
|
120 | - $name = self::wordShift($data); |
|
121 | - $this->scopes[$name] = $data; |
|
122 | - return $this; |
|
123 | - } |
|
124 | - |
|
125 | - return parent::handleCommand($command, $data); |
|
126 | - } |
|
127 | - |
|
128 | - public function toArray(): array |
|
129 | - { |
|
130 | - return self::arrayFilterNull(array_merge(array( |
|
131 | - 'type' => $this->type === 'apikey' ? 'apiKey' : $this->type, |
|
132 | - 'description' => empty($this->description) ? null : $this->description, |
|
133 | - 'name' => $this->name, |
|
134 | - 'in' => $this->in, |
|
135 | - 'flow' => $this->flow === 'accesscode' ? 'accessCode' : $this->flow, |
|
136 | - 'authorizationUrl' => $this->authorizationUrl, |
|
137 | - 'tokenUrl' => $this->tokenUrl, |
|
138 | - 'scopes' => $this->scopes, |
|
139 | - ), parent::toArray())); |
|
140 | - } |
|
141 | - |
|
142 | - public function __toString() |
|
143 | - { |
|
144 | - return __CLASS__ . ' ' . $this->type; |
|
145 | - } |
|
19 | + /** |
|
20 | + * 'basic', 'apikey' or 'oauth2' |
|
21 | + * @var string |
|
22 | + */ |
|
23 | + private $type; |
|
24 | + |
|
25 | + /** |
|
26 | + * @var string |
|
27 | + */ |
|
28 | + private $description; |
|
29 | + private $name; |
|
30 | + private $in; |
|
31 | + private $flow; |
|
32 | + private $authorizationUrl; |
|
33 | + private $tokenUrl; |
|
34 | + |
|
35 | + /** |
|
36 | + * Map of scope-name => description |
|
37 | + * @var [] |
|
38 | + */ |
|
39 | + private $scopes = array(); |
|
40 | + |
|
41 | + /** |
|
42 | + * Create a new SecurityScheme object |
|
43 | + * @param AbstractObject $parent |
|
44 | + * @param string $type |
|
45 | + * @param string $data |
|
46 | + * @throws Exception |
|
47 | + */ |
|
48 | + public function __construct(AbstractObject $parent, $type, $data = null) |
|
49 | + { |
|
50 | + parent::__construct($parent); |
|
51 | + |
|
52 | + if (!in_array(strtolower($type), array('basic', 'apikey', 'oauth2'))) { |
|
53 | + throw new Exception("Security scheme type must be either 'basic', 'apiKey' or 'oauth2', not '{$type}'"); |
|
54 | + } |
|
55 | + $this->type = strtolower($type); |
|
56 | + |
|
57 | + switch ($this->type) { |
|
58 | + case 'basic': |
|
59 | + $this->description = $data; |
|
60 | + break; |
|
61 | + |
|
62 | + case 'apikey': |
|
63 | + $this->name = self::wordShift($data); |
|
64 | + |
|
65 | + $in = strtolower(self::wordShift($data)); |
|
66 | + if (!in_array($in, array('query', 'header'))) { |
|
67 | + throw new Exception("ApiKey in must be either 'query' or 'header', not '{$in}'"); |
|
68 | + } |
|
69 | + $this->in = $in; |
|
70 | + |
|
71 | + $this->description = $data; |
|
72 | + break; |
|
73 | + |
|
74 | + case 'oauth2': |
|
75 | + $flow = strtolower(self::wordShift($data)); |
|
76 | + if (!in_array($flow, array('implicit', 'password', 'application', 'accesscode'))) { |
|
77 | + throw new Exception("OAuth2 flow must be either 'implicit', 'password', 'application' or 'accesscode', not '{$flow}'"); |
|
78 | + } |
|
79 | + $this->flow = $flow; |
|
80 | + |
|
81 | + if (in_array($flow, array('implicit', 'accesscode'))) { |
|
82 | + $authUrl = self::wordShift($data); |
|
83 | + if (!filter_var($authUrl, FILTER_VALIDATE_URL)) { |
|
84 | + throw new Exception("OAuth2 authorization URL invalid: '{$authUrl}'"); |
|
85 | + } |
|
86 | + $this->authorizationUrl = $authUrl; |
|
87 | + } |
|
88 | + |
|
89 | + if (in_array($flow, array('password', 'application', 'accesscode'))) { |
|
90 | + $tokenUrl = self::wordShift($data); |
|
91 | + if (!filter_var($tokenUrl, FILTER_VALIDATE_URL)) { |
|
92 | + throw new Exception("OAuth2 token URL invalid: '{$tokenUrl}'"); |
|
93 | + } |
|
94 | + $this->tokenUrl = $tokenUrl; |
|
95 | + } |
|
96 | + |
|
97 | + $this->description = $data; |
|
98 | + break; |
|
99 | + } |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * @param string $command |
|
104 | + * @param string $data |
|
105 | + * @return AbstractObject|boolean |
|
106 | + * @throws Exception |
|
107 | + */ |
|
108 | + public function handleCommand($command, $data = null) |
|
109 | + { |
|
110 | + switch (strtolower($command)) { |
|
111 | + case 'description': |
|
112 | + $this->description = $data; |
|
113 | + return $this; |
|
114 | + |
|
115 | + case 'scope': |
|
116 | + if ($this->type !== 'oauth2') { |
|
117 | + throw new Exception("Cannot set scope on type '{$this->type}'"); |
|
118 | + } |
|
119 | + |
|
120 | + $name = self::wordShift($data); |
|
121 | + $this->scopes[$name] = $data; |
|
122 | + return $this; |
|
123 | + } |
|
124 | + |
|
125 | + return parent::handleCommand($command, $data); |
|
126 | + } |
|
127 | + |
|
128 | + public function toArray(): array |
|
129 | + { |
|
130 | + return self::arrayFilterNull(array_merge(array( |
|
131 | + 'type' => $this->type === 'apikey' ? 'apiKey' : $this->type, |
|
132 | + 'description' => empty($this->description) ? null : $this->description, |
|
133 | + 'name' => $this->name, |
|
134 | + 'in' => $this->in, |
|
135 | + 'flow' => $this->flow === 'accesscode' ? 'accessCode' : $this->flow, |
|
136 | + 'authorizationUrl' => $this->authorizationUrl, |
|
137 | + 'tokenUrl' => $this->tokenUrl, |
|
138 | + 'scopes' => $this->scopes, |
|
139 | + ), parent::toArray())); |
|
140 | + } |
|
141 | + |
|
142 | + public function __toString() |
|
143 | + { |
|
144 | + return __CLASS__ . ' ' . $this->type; |
|
145 | + } |
|
146 | 146 | |
147 | 147 | } |
@@ -16,177 +16,177 @@ |
||
16 | 16 | class Response extends AbstractObject |
17 | 17 | { |
18 | 18 | |
19 | - const OK = 200; |
|
20 | - const CREATED = 201; |
|
21 | - const ACCEPTED = 202; |
|
22 | - const NON_AUTHORITATIVE_INFORMATION = 203; |
|
23 | - const NO_CONTENT = 204; |
|
24 | - const RESET_CONTENT = 205; |
|
25 | - const PARTIAL_CONTENT = 206; |
|
26 | - const BAD_REQUEST = 400; |
|
27 | - const UNAUTHORIZED = 401; |
|
28 | - const PAYMENT_REQUIRED = 402; |
|
29 | - const FORBIDDEN = 403; |
|
30 | - const NOT_FOUND = 404; |
|
31 | - const METHOD_NOT_ALLOWED = 405; |
|
32 | - const NOT_ACCEPTABLE = 406; |
|
33 | - const PROXY_AUTHENTICATION_REQUIRED = 407; |
|
34 | - const REQUEST_TIMEOUT = 408; |
|
35 | - const CONFLICT = 409; |
|
36 | - const GONE = 410; |
|
37 | - const LENGTH_REQUIRED = 411; |
|
38 | - const PRECONDITION_FAILED = 412; |
|
39 | - const REQUEST_ENTITY_TOO_LARGE = 413; |
|
40 | - const REQUEST_URI_TOO_LONG = 414; |
|
41 | - const UNSUPPORTED_MEDIA_TYPE = 415; |
|
42 | - const REQUESTED_RANGE_NOT_SATISFIABLE = 416; |
|
43 | - const EXPECTATION_FAILED = 417; |
|
44 | - const UNPROCESSABLE_ENTITY = 422; |
|
45 | - const TOO_MANY_REQUESTS = 429; |
|
46 | - const INTERNAL_SERVER_ERROR = 500; |
|
47 | - const NOT_IMPLEMENTED = 501; // When method is supported for none of the resources |
|
48 | - |
|
49 | - protected static $httpCodes = array( |
|
50 | - self::OK => 'OK', |
|
51 | - self::CREATED => 'Created', |
|
52 | - self::ACCEPTED => 'Accepted', |
|
53 | - self::NON_AUTHORITATIVE_INFORMATION => 'Non-Authoritative Information', |
|
54 | - self::NO_CONTENT => 'No Content', |
|
55 | - self::RESET_CONTENT => 'Reset Content', |
|
56 | - self::PARTIAL_CONTENT => 'Partial Content', |
|
57 | - self::BAD_REQUEST => 'Bad Request', |
|
58 | - self::UNAUTHORIZED => 'Unauthorized', |
|
59 | - self::PAYMENT_REQUIRED => 'Payment Required', |
|
60 | - self::FORBIDDEN => 'Forbidden', |
|
61 | - self::NOT_FOUND => 'Not Found', |
|
62 | - self::METHOD_NOT_ALLOWED => 'Method Not Allowed', |
|
63 | - self::NOT_ACCEPTABLE => 'Not Acceptable', |
|
64 | - self::PROXY_AUTHENTICATION_REQUIRED => 'Proxy Authentication Required', |
|
65 | - self::REQUEST_TIMEOUT => 'Request Timeout', |
|
66 | - self::CONFLICT => 'Conflict', |
|
67 | - self::GONE => 'Gone', |
|
68 | - self::LENGTH_REQUIRED => 'Length Required', |
|
69 | - self::PRECONDITION_FAILED => 'Precondition Failed', |
|
70 | - self::REQUEST_ENTITY_TOO_LARGE => 'Request Entity Too Large', |
|
71 | - self::REQUEST_URI_TOO_LONG => 'Request-URI Too Long', |
|
72 | - self::UNSUPPORTED_MEDIA_TYPE => 'Unsupported Media Type', |
|
73 | - self::REQUESTED_RANGE_NOT_SATISFIABLE => 'Requested Range Not Satisfiable', |
|
74 | - self::EXPECTATION_FAILED => 'Expectation Failed', |
|
75 | - self::UNPROCESSABLE_ENTITY => 'Unprocessable Entity', |
|
76 | - self::TOO_MANY_REQUESTS => 'Too Many Requests', |
|
77 | - self::INTERNAL_SERVER_ERROR => 'Internal Server Error', |
|
78 | - self::NOT_IMPLEMENTED => 'Not Implemented', |
|
79 | - ); |
|
80 | - private $description = ''; |
|
81 | - private $schema; |
|
82 | - |
|
83 | - /** |
|
84 | - * @var Header[] |
|
85 | - */ |
|
86 | - private $Headers = array(); |
|
87 | - |
|
88 | - /** |
|
89 | - * JSON examples |
|
90 | - * @var array |
|
91 | - */ |
|
92 | - private $examples = array(); |
|
93 | - |
|
94 | - public static function getCode($search) |
|
95 | - { |
|
96 | - static $lookup = null; |
|
97 | - |
|
98 | - if (is_numeric($search)) { |
|
99 | - return (int)$search; |
|
100 | - } |
|
101 | - |
|
102 | - // build static lookup table |
|
103 | - if (!$lookup) { |
|
104 | - $lookup = array(); |
|
105 | - foreach (self::$httpCodes as $code => $text) { |
|
106 | - $lookup[preg_replace('/[^a-z]+/', '', strtolower($text))] = $code; |
|
107 | - } |
|
108 | - } |
|
109 | - |
|
110 | - $search = preg_replace('/[^a-z]+/', '', strtolower($search)); |
|
111 | - return $lookup[$search] ?? null; |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * @throws Exception |
|
116 | - */ |
|
117 | - public function __construct(AbstractObject $parent, $code, $definition = null, $description = null) |
|
118 | - { |
|
119 | - parent::__construct($parent); |
|
120 | - |
|
121 | - if ($definition) { |
|
122 | - $this->schema = new Schema($this, $definition); |
|
123 | - } |
|
124 | - |
|
125 | - if (!empty($description)) { |
|
126 | - $this->description = $description; |
|
127 | - } elseif (isset(self::$httpCodes[$code])) { |
|
128 | - $this->description = self::$httpCodes[$code]; |
|
129 | - } |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * @param string $command |
|
134 | - * @param string $data |
|
135 | - * @return AbstractObject|boolean |
|
136 | - * @throws Exception |
|
137 | - * @throws Exception |
|
138 | - * @throws Exception |
|
139 | - * @throws Exception |
|
140 | - * @throws Exception |
|
141 | - */ |
|
142 | - public function handleCommand($command, $data = null) |
|
143 | - { |
|
144 | - switch (strtolower($command)) { |
|
145 | - case 'header': |
|
146 | - $type = self::wordShift($data); |
|
147 | - if (empty($type)) { |
|
148 | - throw new Exception('Missing type for header'); |
|
149 | - } |
|
150 | - $name = self::wordShift($data); |
|
151 | - if (empty($name)) { |
|
152 | - throw new Exception('Missing name for header type \'' . $type . '\''); |
|
153 | - } |
|
154 | - $Header = new Header($this, $type, $data); |
|
155 | - $this->Headers[$name] = $Header; |
|
156 | - return $Header; |
|
157 | - |
|
158 | - case 'example': |
|
159 | - $name = self::wordShift($data); |
|
160 | - if (empty($name)) { |
|
161 | - throw new Exception('Missing name for example'); |
|
162 | - } |
|
163 | - if ($data === '') { |
|
164 | - throw new Exception('Missing content for example `' . $name . '`'); |
|
165 | - } |
|
166 | - $json = preg_replace_callback('/([^{}:]+)/', static function ($match) { |
|
167 | - json_decode($match[1]); |
|
168 | - return json_last_error() === JSON_ERROR_NONE ? $match[1] : json_encode($match[1]); |
|
169 | - }, trim($data)); |
|
170 | - $this->examples[$name] = json_decode($json, true); |
|
171 | - return $this; |
|
172 | - } |
|
173 | - |
|
174 | - return parent::handleCommand($command, $data); |
|
175 | - } |
|
176 | - |
|
177 | - public function toArray(): array |
|
178 | - { |
|
179 | - return self::arrayFilterNull(array_merge(array( |
|
180 | - 'description' => $this->description, |
|
181 | - 'schema' => $this->schema ? $this->schema->toArray() : null, |
|
182 | - 'headers' => self::objectsToArray($this->Headers), |
|
183 | - 'examples' => $this->examples, |
|
184 | - ), parent::toArray())); |
|
185 | - } |
|
186 | - |
|
187 | - public function __toString() |
|
188 | - { |
|
189 | - return __CLASS__; |
|
190 | - } |
|
19 | + const OK = 200; |
|
20 | + const CREATED = 201; |
|
21 | + const ACCEPTED = 202; |
|
22 | + const NON_AUTHORITATIVE_INFORMATION = 203; |
|
23 | + const NO_CONTENT = 204; |
|
24 | + const RESET_CONTENT = 205; |
|
25 | + const PARTIAL_CONTENT = 206; |
|
26 | + const BAD_REQUEST = 400; |
|
27 | + const UNAUTHORIZED = 401; |
|
28 | + const PAYMENT_REQUIRED = 402; |
|
29 | + const FORBIDDEN = 403; |
|
30 | + const NOT_FOUND = 404; |
|
31 | + const METHOD_NOT_ALLOWED = 405; |
|
32 | + const NOT_ACCEPTABLE = 406; |
|
33 | + const PROXY_AUTHENTICATION_REQUIRED = 407; |
|
34 | + const REQUEST_TIMEOUT = 408; |
|
35 | + const CONFLICT = 409; |
|
36 | + const GONE = 410; |
|
37 | + const LENGTH_REQUIRED = 411; |
|
38 | + const PRECONDITION_FAILED = 412; |
|
39 | + const REQUEST_ENTITY_TOO_LARGE = 413; |
|
40 | + const REQUEST_URI_TOO_LONG = 414; |
|
41 | + const UNSUPPORTED_MEDIA_TYPE = 415; |
|
42 | + const REQUESTED_RANGE_NOT_SATISFIABLE = 416; |
|
43 | + const EXPECTATION_FAILED = 417; |
|
44 | + const UNPROCESSABLE_ENTITY = 422; |
|
45 | + const TOO_MANY_REQUESTS = 429; |
|
46 | + const INTERNAL_SERVER_ERROR = 500; |
|
47 | + const NOT_IMPLEMENTED = 501; // When method is supported for none of the resources |
|
48 | + |
|
49 | + protected static $httpCodes = array( |
|
50 | + self::OK => 'OK', |
|
51 | + self::CREATED => 'Created', |
|
52 | + self::ACCEPTED => 'Accepted', |
|
53 | + self::NON_AUTHORITATIVE_INFORMATION => 'Non-Authoritative Information', |
|
54 | + self::NO_CONTENT => 'No Content', |
|
55 | + self::RESET_CONTENT => 'Reset Content', |
|
56 | + self::PARTIAL_CONTENT => 'Partial Content', |
|
57 | + self::BAD_REQUEST => 'Bad Request', |
|
58 | + self::UNAUTHORIZED => 'Unauthorized', |
|
59 | + self::PAYMENT_REQUIRED => 'Payment Required', |
|
60 | + self::FORBIDDEN => 'Forbidden', |
|
61 | + self::NOT_FOUND => 'Not Found', |
|
62 | + self::METHOD_NOT_ALLOWED => 'Method Not Allowed', |
|
63 | + self::NOT_ACCEPTABLE => 'Not Acceptable', |
|
64 | + self::PROXY_AUTHENTICATION_REQUIRED => 'Proxy Authentication Required', |
|
65 | + self::REQUEST_TIMEOUT => 'Request Timeout', |
|
66 | + self::CONFLICT => 'Conflict', |
|
67 | + self::GONE => 'Gone', |
|
68 | + self::LENGTH_REQUIRED => 'Length Required', |
|
69 | + self::PRECONDITION_FAILED => 'Precondition Failed', |
|
70 | + self::REQUEST_ENTITY_TOO_LARGE => 'Request Entity Too Large', |
|
71 | + self::REQUEST_URI_TOO_LONG => 'Request-URI Too Long', |
|
72 | + self::UNSUPPORTED_MEDIA_TYPE => 'Unsupported Media Type', |
|
73 | + self::REQUESTED_RANGE_NOT_SATISFIABLE => 'Requested Range Not Satisfiable', |
|
74 | + self::EXPECTATION_FAILED => 'Expectation Failed', |
|
75 | + self::UNPROCESSABLE_ENTITY => 'Unprocessable Entity', |
|
76 | + self::TOO_MANY_REQUESTS => 'Too Many Requests', |
|
77 | + self::INTERNAL_SERVER_ERROR => 'Internal Server Error', |
|
78 | + self::NOT_IMPLEMENTED => 'Not Implemented', |
|
79 | + ); |
|
80 | + private $description = ''; |
|
81 | + private $schema; |
|
82 | + |
|
83 | + /** |
|
84 | + * @var Header[] |
|
85 | + */ |
|
86 | + private $Headers = array(); |
|
87 | + |
|
88 | + /** |
|
89 | + * JSON examples |
|
90 | + * @var array |
|
91 | + */ |
|
92 | + private $examples = array(); |
|
93 | + |
|
94 | + public static function getCode($search) |
|
95 | + { |
|
96 | + static $lookup = null; |
|
97 | + |
|
98 | + if (is_numeric($search)) { |
|
99 | + return (int)$search; |
|
100 | + } |
|
101 | + |
|
102 | + // build static lookup table |
|
103 | + if (!$lookup) { |
|
104 | + $lookup = array(); |
|
105 | + foreach (self::$httpCodes as $code => $text) { |
|
106 | + $lookup[preg_replace('/[^a-z]+/', '', strtolower($text))] = $code; |
|
107 | + } |
|
108 | + } |
|
109 | + |
|
110 | + $search = preg_replace('/[^a-z]+/', '', strtolower($search)); |
|
111 | + return $lookup[$search] ?? null; |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * @throws Exception |
|
116 | + */ |
|
117 | + public function __construct(AbstractObject $parent, $code, $definition = null, $description = null) |
|
118 | + { |
|
119 | + parent::__construct($parent); |
|
120 | + |
|
121 | + if ($definition) { |
|
122 | + $this->schema = new Schema($this, $definition); |
|
123 | + } |
|
124 | + |
|
125 | + if (!empty($description)) { |
|
126 | + $this->description = $description; |
|
127 | + } elseif (isset(self::$httpCodes[$code])) { |
|
128 | + $this->description = self::$httpCodes[$code]; |
|
129 | + } |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * @param string $command |
|
134 | + * @param string $data |
|
135 | + * @return AbstractObject|boolean |
|
136 | + * @throws Exception |
|
137 | + * @throws Exception |
|
138 | + * @throws Exception |
|
139 | + * @throws Exception |
|
140 | + * @throws Exception |
|
141 | + */ |
|
142 | + public function handleCommand($command, $data = null) |
|
143 | + { |
|
144 | + switch (strtolower($command)) { |
|
145 | + case 'header': |
|
146 | + $type = self::wordShift($data); |
|
147 | + if (empty($type)) { |
|
148 | + throw new Exception('Missing type for header'); |
|
149 | + } |
|
150 | + $name = self::wordShift($data); |
|
151 | + if (empty($name)) { |
|
152 | + throw new Exception('Missing name for header type \'' . $type . '\''); |
|
153 | + } |
|
154 | + $Header = new Header($this, $type, $data); |
|
155 | + $this->Headers[$name] = $Header; |
|
156 | + return $Header; |
|
157 | + |
|
158 | + case 'example': |
|
159 | + $name = self::wordShift($data); |
|
160 | + if (empty($name)) { |
|
161 | + throw new Exception('Missing name for example'); |
|
162 | + } |
|
163 | + if ($data === '') { |
|
164 | + throw new Exception('Missing content for example `' . $name . '`'); |
|
165 | + } |
|
166 | + $json = preg_replace_callback('/([^{}:]+)/', static function ($match) { |
|
167 | + json_decode($match[1]); |
|
168 | + return json_last_error() === JSON_ERROR_NONE ? $match[1] : json_encode($match[1]); |
|
169 | + }, trim($data)); |
|
170 | + $this->examples[$name] = json_decode($json, true); |
|
171 | + return $this; |
|
172 | + } |
|
173 | + |
|
174 | + return parent::handleCommand($command, $data); |
|
175 | + } |
|
176 | + |
|
177 | + public function toArray(): array |
|
178 | + { |
|
179 | + return self::arrayFilterNull(array_merge(array( |
|
180 | + 'description' => $this->description, |
|
181 | + 'schema' => $this->schema ? $this->schema->toArray() : null, |
|
182 | + 'headers' => self::objectsToArray($this->Headers), |
|
183 | + 'examples' => $this->examples, |
|
184 | + ), parent::toArray())); |
|
185 | + } |
|
186 | + |
|
187 | + public function __toString() |
|
188 | + { |
|
189 | + return __CLASS__; |
|
190 | + } |
|
191 | 191 | |
192 | 192 | } |
@@ -96,7 +96,7 @@ discard block |
||
96 | 96 | static $lookup = null; |
97 | 97 | |
98 | 98 | if (is_numeric($search)) { |
99 | - return (int)$search; |
|
99 | + return (int) $search; |
|
100 | 100 | } |
101 | 101 | |
102 | 102 | // build static lookup table |
@@ -163,7 +163,7 @@ discard block |
||
163 | 163 | if ($data === '') { |
164 | 164 | throw new Exception('Missing content for example `' . $name . '`'); |
165 | 165 | } |
166 | - $json = preg_replace_callback('/([^{}:]+)/', static function ($match) { |
|
166 | + $json = preg_replace_callback('/([^{}:]+)/', static function($match) { |
|
167 | 167 | json_decode($match[1]); |
168 | 168 | return json_last_error() === JSON_ERROR_NONE ? $match[1] : json_encode($match[1]); |
169 | 169 | }, trim($data)); |