@@ -13,17 +13,17 @@ |
||
13 | 13 | class Example |
14 | 14 | { |
15 | 15 | |
16 | - public function Dummy() |
|
17 | - { |
|
18 | - /** |
|
19 | - * @rest\endpoint /v1/users/{id} |
|
20 | - * @rest\method GET Return a JSON with all the user attributes |
|
21 | - * @rest\path Int id The ID of the User |
|
22 | - * @rest\response 200 User |
|
23 | - */ |
|
24 | - $app->get('/v1/users/{id:[0-9]+}', function ($request, $response, $args) { |
|
25 | - // ... |
|
26 | - }); |
|
27 | - } |
|
16 | + public function Dummy() |
|
17 | + { |
|
18 | + /** |
|
19 | + * @rest\endpoint /v1/users/{id} |
|
20 | + * @rest\method GET Return a JSON with all the user attributes |
|
21 | + * @rest\path Int id The ID of the User |
|
22 | + * @rest\response 200 User |
|
23 | + */ |
|
24 | + $app->get('/v1/users/{id:[0-9]+}', function ($request, $response, $args) { |
|
25 | + // ... |
|
26 | + }); |
|
27 | + } |
|
28 | 28 | |
29 | 29 | } |
@@ -21,7 +21,7 @@ |
||
21 | 21 | * @rest\path Int id The ID of the User |
22 | 22 | * @rest\response 200 User |
23 | 23 | */ |
24 | - $app->get('/v1/users/{id:[0-9]+}', function ($request, $response, $args) { |
|
24 | + $app->get('/v1/users/{id:[0-9]+}', function($request, $response, $args) { |
|
25 | 25 | // ... |
26 | 26 | }); |
27 | 27 | } |
@@ -5,191 +5,191 @@ |
||
5 | 5 | class ParserTest extends \SwaggerGen_TestCase |
6 | 6 | { |
7 | 7 | |
8 | - /** |
|
9 | - * @covers \SwaggerGen\Parser\Text\Parser::__construct |
|
10 | - */ |
|
11 | - public function testConstructor_Empty() |
|
12 | - { |
|
13 | - $object = new \SwaggerGen\Parser\Text\Parser(); |
|
14 | - $this->assertInstanceOf('\SwaggerGen\Parser\Text\Parser', $object); |
|
15 | - } |
|
16 | - |
|
17 | - /** |
|
18 | - * @covers \SwaggerGen\Parser\Text\Parser::__construct |
|
19 | - */ |
|
20 | - public function testConstructor_Dirs() |
|
21 | - { |
|
22 | - $this->markTestIncomplete('Not yet implemented.'); |
|
23 | - } |
|
24 | - |
|
25 | - /** |
|
26 | - * @covers \SwaggerGen\Parser\Text\Parser::addDirs |
|
27 | - */ |
|
28 | - public function testAddDirs() |
|
29 | - { |
|
30 | - $this->markTestIncomplete('Not yet implemented.'); |
|
31 | - } |
|
32 | - |
|
33 | - /** |
|
34 | - * @covers \SwaggerGen\Parser\Text\Parser::parseText |
|
35 | - */ |
|
36 | - public function testParseText() |
|
37 | - { |
|
38 | - $object = new \SwaggerGen\Parser\Text\Parser(); |
|
39 | - $this->assertInstanceOf('\SwaggerGen\Parser\Text\Parser', $object); |
|
40 | - |
|
41 | - $statements = $object->parseText('title Some words'); |
|
42 | - |
|
43 | - $this->assertCount(1, $statements); |
|
44 | - $this->assertInstanceOf('\SwaggerGen\Statement', $statements[0]); |
|
45 | - $this->assertSame('title', $statements[0]->getCommand()); |
|
46 | - $this->assertSame('Some words', $statements[0]->getData()); |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * @covers \SwaggerGen\Parser\Text\Parser::parseText |
|
51 | - */ |
|
52 | - public function testParseText_Whitespace() |
|
53 | - { |
|
54 | - $object = new \SwaggerGen\Parser\Text\Parser(); |
|
55 | - $this->assertInstanceOf('\SwaggerGen\Parser\Text\Parser', $object); |
|
56 | - |
|
57 | - $statements = $object->parseText(" \t title \t\t Some words \t "); |
|
58 | - |
|
59 | - $this->assertCount(1, $statements); |
|
60 | - $this->assertInstanceOf('\SwaggerGen\Statement', $statements[0]); |
|
61 | - $this->assertSame('title', $statements[0]->getCommand()); |
|
62 | - $this->assertSame('Some words', $statements[0]->getData()); |
|
63 | - } |
|
64 | - |
|
65 | - /** |
|
66 | - * @covers \SwaggerGen\Parser\Text\Parser::parseText |
|
67 | - */ |
|
68 | - public function testParseText_Multiple_LF() |
|
69 | - { |
|
70 | - $object = new \SwaggerGen\Parser\Text\Parser(); |
|
71 | - $this->assertInstanceOf('\SwaggerGen\Parser\Text\Parser', $object); |
|
72 | - |
|
73 | - $statements = $object->parseText("title Some words\nSome Random words"); |
|
74 | - |
|
75 | - $this->assertCount(2, $statements); |
|
76 | - |
|
77 | - $this->assertInstanceOf('\SwaggerGen\Statement', $statements[0]); |
|
78 | - $this->assertSame('title', $statements[0]->getCommand()); |
|
79 | - $this->assertSame('Some words', $statements[0]->getData()); |
|
80 | - |
|
81 | - $this->assertInstanceOf('\SwaggerGen\Statement', $statements[1]); |
|
82 | - $this->assertSame('Some', $statements[1]->getCommand()); |
|
83 | - $this->assertSame('Random words', $statements[1]->getData()); |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * @covers \SwaggerGen\Parser\Text\Parser::parseText |
|
88 | - */ |
|
89 | - public function testParseText_Multiple_CR() |
|
90 | - { |
|
91 | - $object = new \SwaggerGen\Parser\Text\Parser(); |
|
92 | - $this->assertInstanceOf('\SwaggerGen\Parser\Text\Parser', $object); |
|
93 | - |
|
94 | - $statements = $object->parseText("title Some words\rSome Random words"); |
|
95 | - |
|
96 | - $this->assertCount(2, $statements); |
|
97 | - |
|
98 | - $this->assertInstanceOf('\SwaggerGen\Statement', $statements[0]); |
|
99 | - $this->assertSame('title', $statements[0]->getCommand()); |
|
100 | - $this->assertSame('Some words', $statements[0]->getData()); |
|
101 | - |
|
102 | - $this->assertInstanceOf('\SwaggerGen\Statement', $statements[1]); |
|
103 | - $this->assertSame('Some', $statements[1]->getCommand()); |
|
104 | - $this->assertSame('Random words', $statements[1]->getData()); |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * @covers \SwaggerGen\Parser\Text\Parser::parseText |
|
109 | - */ |
|
110 | - public function testParseText_Multiple_CRLF() |
|
111 | - { |
|
112 | - $object = new \SwaggerGen\Parser\Text\Parser(); |
|
113 | - $this->assertInstanceOf('\SwaggerGen\Parser\Text\Parser', $object); |
|
114 | - |
|
115 | - $statements = $object->parseText("title Some words\r\nSome Random words"); |
|
116 | - |
|
117 | - $this->assertCount(2, $statements); |
|
118 | - |
|
119 | - $this->assertInstanceOf('\SwaggerGen\Statement', $statements[0]); |
|
120 | - $this->assertSame('title', $statements[0]->getCommand()); |
|
121 | - $this->assertSame('Some words', $statements[0]->getData()); |
|
122 | - |
|
123 | - $this->assertInstanceOf('\SwaggerGen\Statement', $statements[1]); |
|
124 | - $this->assertSame('Some', $statements[1]->getCommand()); |
|
125 | - $this->assertSame('Random words', $statements[1]->getData()); |
|
126 | - } |
|
127 | - |
|
128 | - /** |
|
129 | - * @covers \SwaggerGen\Parser\Text\Parser::parseText |
|
130 | - */ |
|
131 | - public function testParseText_Multiple_BlankLines() |
|
132 | - { |
|
133 | - $object = new \SwaggerGen\Parser\Text\Parser(); |
|
134 | - $this->assertInstanceOf('\SwaggerGen\Parser\Text\Parser', $object); |
|
135 | - |
|
136 | - $statements = $object->parseText("title Some words\r\n\n\n\rSome Random words"); |
|
137 | - |
|
138 | - $this->assertCount(2, $statements); |
|
139 | - |
|
140 | - $this->assertInstanceOf('\SwaggerGen\Statement', $statements[0]); |
|
141 | - $this->assertSame('title', $statements[0]->getCommand()); |
|
142 | - $this->assertSame('Some words', $statements[0]->getData()); |
|
143 | - |
|
144 | - $this->assertInstanceOf('\SwaggerGen\Statement', $statements[1]); |
|
145 | - $this->assertSame('Some', $statements[1]->getCommand()); |
|
146 | - $this->assertSame('Random words', $statements[1]->getData()); |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * @covers \SwaggerGen\Parser\Text\Parser::parseText |
|
151 | - */ |
|
152 | - public function testParseText_Dirs() |
|
153 | - { |
|
154 | - $object = new \SwaggerGen\Parser\Text\Parser(); |
|
155 | - $this->assertInstanceOf('\SwaggerGen\Parser\Text\Parser', $object); |
|
156 | - |
|
157 | - $statements = $object->parseText("title Some words\r\n\n\n\rSome Random words", array( |
|
158 | - __DIR__ . '/ParserTest/not used by text parser', |
|
159 | - )); |
|
160 | - |
|
161 | - $this->assertCount(2, $statements); |
|
162 | - |
|
163 | - $this->assertInstanceOf('\SwaggerGen\Statement', $statements[0]); |
|
164 | - $this->assertSame('title', $statements[0]->getCommand()); |
|
165 | - $this->assertSame('Some words', $statements[0]->getData()); |
|
166 | - |
|
167 | - $this->assertInstanceOf('\SwaggerGen\Statement', $statements[1]); |
|
168 | - $this->assertSame('Some', $statements[1]->getCommand()); |
|
169 | - $this->assertSame('Random words', $statements[1]->getData()); |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * @covers \SwaggerGen\Parser\Text\Parser::parse |
|
174 | - */ |
|
175 | - public function testParse() |
|
176 | - { |
|
177 | - $object = new \SwaggerGen\Parser\Text\Parser(); |
|
178 | - $this->assertInstanceOf('\SwaggerGen\Parser\Text\Parser', $object); |
|
179 | - |
|
180 | - $statements = $object->parse(__DIR__ . '/ParserTest/testParse.txt', array( |
|
181 | - __DIR__ . '/ParserTest/not used by text parser', |
|
182 | - )); |
|
183 | - |
|
184 | - $this->assertCount(2, $statements); |
|
185 | - |
|
186 | - $this->assertInstanceOf('\SwaggerGen\Statement', $statements[0]); |
|
187 | - $this->assertSame('title', $statements[0]->getCommand()); |
|
188 | - $this->assertSame('Some words', $statements[0]->getData()); |
|
189 | - |
|
190 | - $this->assertInstanceOf('\SwaggerGen\Statement', $statements[1]); |
|
191 | - $this->assertSame('Some', $statements[1]->getCommand()); |
|
192 | - $this->assertSame('Random words', $statements[1]->getData()); |
|
193 | - } |
|
8 | + /** |
|
9 | + * @covers \SwaggerGen\Parser\Text\Parser::__construct |
|
10 | + */ |
|
11 | + public function testConstructor_Empty() |
|
12 | + { |
|
13 | + $object = new \SwaggerGen\Parser\Text\Parser(); |
|
14 | + $this->assertInstanceOf('\SwaggerGen\Parser\Text\Parser', $object); |
|
15 | + } |
|
16 | + |
|
17 | + /** |
|
18 | + * @covers \SwaggerGen\Parser\Text\Parser::__construct |
|
19 | + */ |
|
20 | + public function testConstructor_Dirs() |
|
21 | + { |
|
22 | + $this->markTestIncomplete('Not yet implemented.'); |
|
23 | + } |
|
24 | + |
|
25 | + /** |
|
26 | + * @covers \SwaggerGen\Parser\Text\Parser::addDirs |
|
27 | + */ |
|
28 | + public function testAddDirs() |
|
29 | + { |
|
30 | + $this->markTestIncomplete('Not yet implemented.'); |
|
31 | + } |
|
32 | + |
|
33 | + /** |
|
34 | + * @covers \SwaggerGen\Parser\Text\Parser::parseText |
|
35 | + */ |
|
36 | + public function testParseText() |
|
37 | + { |
|
38 | + $object = new \SwaggerGen\Parser\Text\Parser(); |
|
39 | + $this->assertInstanceOf('\SwaggerGen\Parser\Text\Parser', $object); |
|
40 | + |
|
41 | + $statements = $object->parseText('title Some words'); |
|
42 | + |
|
43 | + $this->assertCount(1, $statements); |
|
44 | + $this->assertInstanceOf('\SwaggerGen\Statement', $statements[0]); |
|
45 | + $this->assertSame('title', $statements[0]->getCommand()); |
|
46 | + $this->assertSame('Some words', $statements[0]->getData()); |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * @covers \SwaggerGen\Parser\Text\Parser::parseText |
|
51 | + */ |
|
52 | + public function testParseText_Whitespace() |
|
53 | + { |
|
54 | + $object = new \SwaggerGen\Parser\Text\Parser(); |
|
55 | + $this->assertInstanceOf('\SwaggerGen\Parser\Text\Parser', $object); |
|
56 | + |
|
57 | + $statements = $object->parseText(" \t title \t\t Some words \t "); |
|
58 | + |
|
59 | + $this->assertCount(1, $statements); |
|
60 | + $this->assertInstanceOf('\SwaggerGen\Statement', $statements[0]); |
|
61 | + $this->assertSame('title', $statements[0]->getCommand()); |
|
62 | + $this->assertSame('Some words', $statements[0]->getData()); |
|
63 | + } |
|
64 | + |
|
65 | + /** |
|
66 | + * @covers \SwaggerGen\Parser\Text\Parser::parseText |
|
67 | + */ |
|
68 | + public function testParseText_Multiple_LF() |
|
69 | + { |
|
70 | + $object = new \SwaggerGen\Parser\Text\Parser(); |
|
71 | + $this->assertInstanceOf('\SwaggerGen\Parser\Text\Parser', $object); |
|
72 | + |
|
73 | + $statements = $object->parseText("title Some words\nSome Random words"); |
|
74 | + |
|
75 | + $this->assertCount(2, $statements); |
|
76 | + |
|
77 | + $this->assertInstanceOf('\SwaggerGen\Statement', $statements[0]); |
|
78 | + $this->assertSame('title', $statements[0]->getCommand()); |
|
79 | + $this->assertSame('Some words', $statements[0]->getData()); |
|
80 | + |
|
81 | + $this->assertInstanceOf('\SwaggerGen\Statement', $statements[1]); |
|
82 | + $this->assertSame('Some', $statements[1]->getCommand()); |
|
83 | + $this->assertSame('Random words', $statements[1]->getData()); |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * @covers \SwaggerGen\Parser\Text\Parser::parseText |
|
88 | + */ |
|
89 | + public function testParseText_Multiple_CR() |
|
90 | + { |
|
91 | + $object = new \SwaggerGen\Parser\Text\Parser(); |
|
92 | + $this->assertInstanceOf('\SwaggerGen\Parser\Text\Parser', $object); |
|
93 | + |
|
94 | + $statements = $object->parseText("title Some words\rSome Random words"); |
|
95 | + |
|
96 | + $this->assertCount(2, $statements); |
|
97 | + |
|
98 | + $this->assertInstanceOf('\SwaggerGen\Statement', $statements[0]); |
|
99 | + $this->assertSame('title', $statements[0]->getCommand()); |
|
100 | + $this->assertSame('Some words', $statements[0]->getData()); |
|
101 | + |
|
102 | + $this->assertInstanceOf('\SwaggerGen\Statement', $statements[1]); |
|
103 | + $this->assertSame('Some', $statements[1]->getCommand()); |
|
104 | + $this->assertSame('Random words', $statements[1]->getData()); |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * @covers \SwaggerGen\Parser\Text\Parser::parseText |
|
109 | + */ |
|
110 | + public function testParseText_Multiple_CRLF() |
|
111 | + { |
|
112 | + $object = new \SwaggerGen\Parser\Text\Parser(); |
|
113 | + $this->assertInstanceOf('\SwaggerGen\Parser\Text\Parser', $object); |
|
114 | + |
|
115 | + $statements = $object->parseText("title Some words\r\nSome Random words"); |
|
116 | + |
|
117 | + $this->assertCount(2, $statements); |
|
118 | + |
|
119 | + $this->assertInstanceOf('\SwaggerGen\Statement', $statements[0]); |
|
120 | + $this->assertSame('title', $statements[0]->getCommand()); |
|
121 | + $this->assertSame('Some words', $statements[0]->getData()); |
|
122 | + |
|
123 | + $this->assertInstanceOf('\SwaggerGen\Statement', $statements[1]); |
|
124 | + $this->assertSame('Some', $statements[1]->getCommand()); |
|
125 | + $this->assertSame('Random words', $statements[1]->getData()); |
|
126 | + } |
|
127 | + |
|
128 | + /** |
|
129 | + * @covers \SwaggerGen\Parser\Text\Parser::parseText |
|
130 | + */ |
|
131 | + public function testParseText_Multiple_BlankLines() |
|
132 | + { |
|
133 | + $object = new \SwaggerGen\Parser\Text\Parser(); |
|
134 | + $this->assertInstanceOf('\SwaggerGen\Parser\Text\Parser', $object); |
|
135 | + |
|
136 | + $statements = $object->parseText("title Some words\r\n\n\n\rSome Random words"); |
|
137 | + |
|
138 | + $this->assertCount(2, $statements); |
|
139 | + |
|
140 | + $this->assertInstanceOf('\SwaggerGen\Statement', $statements[0]); |
|
141 | + $this->assertSame('title', $statements[0]->getCommand()); |
|
142 | + $this->assertSame('Some words', $statements[0]->getData()); |
|
143 | + |
|
144 | + $this->assertInstanceOf('\SwaggerGen\Statement', $statements[1]); |
|
145 | + $this->assertSame('Some', $statements[1]->getCommand()); |
|
146 | + $this->assertSame('Random words', $statements[1]->getData()); |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * @covers \SwaggerGen\Parser\Text\Parser::parseText |
|
151 | + */ |
|
152 | + public function testParseText_Dirs() |
|
153 | + { |
|
154 | + $object = new \SwaggerGen\Parser\Text\Parser(); |
|
155 | + $this->assertInstanceOf('\SwaggerGen\Parser\Text\Parser', $object); |
|
156 | + |
|
157 | + $statements = $object->parseText("title Some words\r\n\n\n\rSome Random words", array( |
|
158 | + __DIR__ . '/ParserTest/not used by text parser', |
|
159 | + )); |
|
160 | + |
|
161 | + $this->assertCount(2, $statements); |
|
162 | + |
|
163 | + $this->assertInstanceOf('\SwaggerGen\Statement', $statements[0]); |
|
164 | + $this->assertSame('title', $statements[0]->getCommand()); |
|
165 | + $this->assertSame('Some words', $statements[0]->getData()); |
|
166 | + |
|
167 | + $this->assertInstanceOf('\SwaggerGen\Statement', $statements[1]); |
|
168 | + $this->assertSame('Some', $statements[1]->getCommand()); |
|
169 | + $this->assertSame('Random words', $statements[1]->getData()); |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * @covers \SwaggerGen\Parser\Text\Parser::parse |
|
174 | + */ |
|
175 | + public function testParse() |
|
176 | + { |
|
177 | + $object = new \SwaggerGen\Parser\Text\Parser(); |
|
178 | + $this->assertInstanceOf('\SwaggerGen\Parser\Text\Parser', $object); |
|
179 | + |
|
180 | + $statements = $object->parse(__DIR__ . '/ParserTest/testParse.txt', array( |
|
181 | + __DIR__ . '/ParserTest/not used by text parser', |
|
182 | + )); |
|
183 | + |
|
184 | + $this->assertCount(2, $statements); |
|
185 | + |
|
186 | + $this->assertInstanceOf('\SwaggerGen\Statement', $statements[0]); |
|
187 | + $this->assertSame('title', $statements[0]->getCommand()); |
|
188 | + $this->assertSame('Some words', $statements[0]->getData()); |
|
189 | + |
|
190 | + $this->assertInstanceOf('\SwaggerGen\Statement', $statements[1]); |
|
191 | + $this->assertSame('Some', $statements[1]->getCommand()); |
|
192 | + $this->assertSame('Random words', $statements[1]->getData()); |
|
193 | + } |
|
194 | 194 | |
195 | 195 | } |
@@ -11,15 +11,15 @@ |
||
11 | 11 | class Example |
12 | 12 | { |
13 | 13 | |
14 | - /** |
|
15 | - * @rest\endpoint /endpoint/{listid} |
|
16 | - * @rest\method GET Something |
|
17 | - * @rest\param listid |
|
18 | - * @rest\response 200 |
|
19 | - */ |
|
20 | - public function Dummy() |
|
21 | - { |
|
14 | + /** |
|
15 | + * @rest\endpoint /endpoint/{listid} |
|
16 | + * @rest\method GET Something |
|
17 | + * @rest\param listid |
|
18 | + * @rest\response 200 |
|
19 | + */ |
|
20 | + public function Dummy() |
|
21 | + { |
|
22 | 22 | |
23 | - } |
|
23 | + } |
|
24 | 24 | |
25 | 25 | } |
@@ -14,44 +14,44 @@ |
||
14 | 14 | class ExternalDocumentation extends AbstractObject |
15 | 15 | { |
16 | 16 | |
17 | - private $url; |
|
18 | - private $description; |
|
19 | - |
|
20 | - public function __construct(AbstractObject $parent, $url, $description = null) |
|
21 | - { |
|
22 | - parent::__construct($parent); |
|
23 | - $this->url = $url; |
|
24 | - $this->description = $description; |
|
25 | - } |
|
26 | - |
|
27 | - /** |
|
28 | - * @param string $command |
|
29 | - * @param string $data |
|
30 | - * @return AbstractObject|boolean |
|
31 | - */ |
|
32 | - public function handleCommand($command, $data = null) |
|
33 | - { |
|
34 | - switch (strtolower($command)) { |
|
35 | - case 'url': |
|
36 | - case 'description': |
|
37 | - $this->$command = $data; |
|
38 | - return $this; |
|
39 | - } |
|
40 | - |
|
41 | - return parent::handleCommand($command, $data); |
|
42 | - } |
|
43 | - |
|
44 | - public function toArray(): array |
|
45 | - { |
|
46 | - return self::arrayFilterNull(array_merge(array( |
|
47 | - 'url' => $this->url, |
|
48 | - 'description' => empty($this->description) ? null : $this->description, |
|
49 | - ), parent::toArray())); |
|
50 | - } |
|
51 | - |
|
52 | - public function __toString() |
|
53 | - { |
|
54 | - return __CLASS__ . ' ' . $this->url; |
|
55 | - } |
|
17 | + private $url; |
|
18 | + private $description; |
|
19 | + |
|
20 | + public function __construct(AbstractObject $parent, $url, $description = null) |
|
21 | + { |
|
22 | + parent::__construct($parent); |
|
23 | + $this->url = $url; |
|
24 | + $this->description = $description; |
|
25 | + } |
|
26 | + |
|
27 | + /** |
|
28 | + * @param string $command |
|
29 | + * @param string $data |
|
30 | + * @return AbstractObject|boolean |
|
31 | + */ |
|
32 | + public function handleCommand($command, $data = null) |
|
33 | + { |
|
34 | + switch (strtolower($command)) { |
|
35 | + case 'url': |
|
36 | + case 'description': |
|
37 | + $this->$command = $data; |
|
38 | + return $this; |
|
39 | + } |
|
40 | + |
|
41 | + return parent::handleCommand($command, $data); |
|
42 | + } |
|
43 | + |
|
44 | + public function toArray(): array |
|
45 | + { |
|
46 | + return self::arrayFilterNull(array_merge(array( |
|
47 | + 'url' => $this->url, |
|
48 | + 'description' => empty($this->description) ? null : $this->description, |
|
49 | + ), parent::toArray())); |
|
50 | + } |
|
51 | + |
|
52 | + public function __toString() |
|
53 | + { |
|
54 | + return __CLASS__ . ' ' . $this->url; |
|
55 | + } |
|
56 | 56 | |
57 | 57 | } |
@@ -142,7 +142,7 @@ |
||
142 | 142 | if ($data === '') { |
143 | 143 | throw new Exception("Missing content for type example"); |
144 | 144 | } |
145 | - $json = preg_replace_callback('/([^{}:,]+)/', static function ($match) { |
|
145 | + $json = preg_replace_callback('/([^{}:,]+)/', static function($match) { |
|
146 | 146 | json_decode($match[1]); |
147 | 147 | return json_last_error() === JSON_ERROR_NONE ? $match[1] : json_encode($match[1]); |
148 | 148 | }, trim($data)); |
@@ -200,7 +200,7 @@ |
||
200 | 200 | |
201 | 201 | if (isset(self::$classTypes[$format])) { |
202 | 202 | $type = self::$classTypes[$format]; |
203 | - $class = "\\SwaggerGen\\Swagger\\Type\\{$type}Type"; |
|
203 | + $class = "\\SwaggerGen\\Swagger\\Type\\{$type}type"; |
|
204 | 204 | return new $class($parent, $definition); |
205 | 205 | } |
206 | 206 |
@@ -15,195 +15,195 @@ |
||
15 | 15 | */ |
16 | 16 | abstract class AbstractType extends AbstractObject |
17 | 17 | { |
18 | - protected const REGEX_START = '/^'; |
|
19 | - protected const REGEX_FORMAT = '([a-z][a-z0-9]*)'; |
|
20 | - protected const REGEX_CONTENT = '(?:\((.*)\))?'; |
|
21 | - protected const REGEX_RANGE = '(?:([[<])(\\d*),(\\d*)([\\]>]))?'; |
|
22 | - protected const REGEX_DEFAULT = '(?:=(.+))?'; |
|
23 | - protected const REGEX_END = '$/i'; |
|
24 | - |
|
25 | - private static $classTypes = array( |
|
26 | - 'integer' => 'Integer', |
|
27 | - 'int' => 'Integer', |
|
28 | - 'int32' => 'Integer', |
|
29 | - 'int64' => 'Integer', |
|
30 | - 'long' => 'Integer', |
|
31 | - 'float' => 'Number', |
|
32 | - 'double' => 'Number', |
|
33 | - 'string' => 'String', |
|
34 | - 'uuid' => 'StringUuid', |
|
35 | - 'byte' => 'String', |
|
36 | - 'binary' => 'String', |
|
37 | - 'password' => 'String', |
|
38 | - 'enum' => 'String', |
|
39 | - 'boolean' => 'Boolean', |
|
40 | - 'bool' => 'Boolean', |
|
41 | - 'array' => 'Array', |
|
42 | - 'csv' => 'Array', |
|
43 | - 'ssv' => 'Array', |
|
44 | - 'tsv' => 'Array', |
|
45 | - 'pipes' => 'Array', |
|
46 | - 'date' => 'Date', |
|
47 | - 'datetime' => 'Date', |
|
48 | - 'date-time' => 'Date', |
|
49 | - 'object' => 'Object', |
|
50 | - 'refobject' => 'ReferenceObject', |
|
51 | - 'allof' => 'AllOf', |
|
52 | - ); |
|
53 | - |
|
54 | - private $example; |
|
55 | - |
|
56 | - /** |
|
57 | - * @param AbstractObject $parent |
|
58 | - * @param $definition |
|
59 | - * @constructor |
|
60 | - */ |
|
61 | - public function __construct(AbstractObject $parent, $definition) |
|
62 | - { |
|
63 | - parent::__construct($parent); |
|
64 | - |
|
65 | - $this->parseDefinition($definition); |
|
66 | - } |
|
67 | - |
|
68 | - abstract protected function parseDefinition($definition); |
|
69 | - |
|
70 | - /** |
|
71 | - * @param AbstractObject $parent |
|
72 | - * @param string $definition |
|
73 | - * @param string $error |
|
74 | - * @return self |
|
75 | - * @throws Exception |
|
76 | - */ |
|
77 | - public static function typeFactory($parent, $definition, $error = "Unparseable schema type definition: '%s'") |
|
78 | - { |
|
79 | - // Parse regex |
|
80 | - $match = []; |
|
81 | - if (preg_match('/^([a-z]+)/i', $definition, $match) === 1) { |
|
82 | - $format = strtolower($match[1]); |
|
83 | - } elseif (preg_match('/^(\[)(?:.*?)\]$/', $definition, $match) === 1) { |
|
84 | - $format = 'array'; |
|
85 | - } elseif (preg_match('/^(\{)(?:.*?)\}$/', $definition, $match) === 1) { |
|
86 | - $format = 'object'; |
|
87 | - } else { |
|
88 | - throw new Exception(sprintf($error, $definition)); |
|
89 | - } |
|
90 | - |
|
91 | - // Internal type if type known and not overwritten by definition |
|
92 | - if ($parent->getTypeRegistry()->has($format)) { |
|
93 | - $class = $parent->getTypeRegistry()->get($format); |
|
94 | - return new $class($parent, $definition); |
|
95 | - } |
|
96 | - |
|
97 | - if (isset(self::$classTypes[$format])) { |
|
98 | - $type = self::$classTypes[$format]; |
|
99 | - $class = "\\SwaggerGen\\Swagger\\Type\\{$type}Type"; |
|
100 | - return new $class($parent, $definition); |
|
101 | - } |
|
102 | - |
|
103 | - return new ReferenceObjectType($parent, $definition); |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * Swap values of two variables. |
|
108 | - * Used for sorting. |
|
109 | - * @param mixed $a |
|
110 | - * @param mixed $b |
|
111 | - */ |
|
112 | - protected static function swap(&$a, &$b): void |
|
113 | - { |
|
114 | - $tmp = $a; |
|
115 | - $a = $b; |
|
116 | - $b = $tmp; |
|
117 | - } |
|
118 | - |
|
119 | - /** |
|
120 | - * @param string $list |
|
121 | - * @return array |
|
122 | - */ |
|
123 | - protected static function parseList($list): array |
|
124 | - { |
|
125 | - $ret = []; |
|
126 | - while ($item = self::parseListItem($list)) { |
|
127 | - $ret[] = $item; |
|
128 | - } |
|
129 | - return $ret; |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Extract an item from a comma-separated list of items. |
|
134 | - * |
|
135 | - * i.e. `a(x(x,x)),b(x)` returns `a(x(x,x))` and changes `$list` into `b(x)`. |
|
136 | - * Note: brace nesting is not checked, e.g. `a{b(})` is a valid list item. |
|
137 | - * |
|
138 | - * @param string $list the list to parse |
|
139 | - * @return string the extracted item |
|
140 | - */ |
|
141 | - protected static function parseListItem(&$list): string |
|
142 | - { |
|
143 | - $item = ''; |
|
144 | - |
|
145 | - $depth = 0; |
|
146 | - $index = 0; |
|
147 | - while ($index < strlen($list)) { |
|
148 | - $c = $list[$index++]; |
|
149 | - |
|
150 | - if (str_contains('{([<', $c)) { |
|
151 | - ++$depth; |
|
152 | - } elseif (str_contains('})]>', $c)) { |
|
153 | - --$depth; |
|
154 | - } elseif ($c === ',' && $depth === 0) { |
|
155 | - break; |
|
156 | - } |
|
157 | - |
|
158 | - $item .= $c; |
|
159 | - } |
|
160 | - $list = substr($list, $index); |
|
161 | - |
|
162 | - return $item; |
|
163 | - } |
|
164 | - |
|
165 | - /** |
|
166 | - * Overwrites default AbstractObject parser, since Types should not handle |
|
167 | - * extensions themselves. |
|
168 | - * |
|
169 | - * @param string $command |
|
170 | - * @param string $data |
|
171 | - * @return AbstractType|boolean |
|
172 | - * @throws Exception |
|
173 | - */ |
|
174 | - public function handleCommand($command, $data = null) |
|
175 | - { |
|
176 | - if (strtolower($command) === 'example') { |
|
177 | - if ($data === '') { |
|
178 | - throw new Exception("Missing content for type example"); |
|
179 | - } |
|
180 | - $json = preg_replace_callback('/([^{}:,]+)/', static function ($match) { |
|
181 | - json_decode($match[1]); |
|
182 | - return json_last_error() === JSON_ERROR_NONE ? $match[1] : json_encode($match[1]); |
|
183 | - }, trim($data)); |
|
184 | - $this->example = json_decode($json, true); |
|
185 | - |
|
186 | - // In case input contains special chars, the above preg_replace would fail |
|
187 | - // Input could be a well-formed json already though |
|
188 | - if ($this->example === null) { |
|
189 | - $this->example = json_decode($data, true); |
|
190 | - } |
|
191 | - // If all fails, use input as-is |
|
192 | - if ($this->example === null) { |
|
193 | - $this->example = $data; |
|
194 | - } |
|
195 | - |
|
196 | - return $this; |
|
197 | - } |
|
198 | - |
|
199 | - return false; |
|
200 | - } |
|
201 | - |
|
202 | - public function toArray(): array |
|
203 | - { |
|
204 | - return self::arrayFilterNull(array_merge(array( |
|
205 | - 'example' => $this->example, |
|
206 | - ), parent::toArray())); |
|
207 | - } |
|
18 | + protected const REGEX_START = '/^'; |
|
19 | + protected const REGEX_FORMAT = '([a-z][a-z0-9]*)'; |
|
20 | + protected const REGEX_CONTENT = '(?:\((.*)\))?'; |
|
21 | + protected const REGEX_RANGE = '(?:([[<])(\\d*),(\\d*)([\\]>]))?'; |
|
22 | + protected const REGEX_DEFAULT = '(?:=(.+))?'; |
|
23 | + protected const REGEX_END = '$/i'; |
|
24 | + |
|
25 | + private static $classTypes = array( |
|
26 | + 'integer' => 'Integer', |
|
27 | + 'int' => 'Integer', |
|
28 | + 'int32' => 'Integer', |
|
29 | + 'int64' => 'Integer', |
|
30 | + 'long' => 'Integer', |
|
31 | + 'float' => 'Number', |
|
32 | + 'double' => 'Number', |
|
33 | + 'string' => 'String', |
|
34 | + 'uuid' => 'StringUuid', |
|
35 | + 'byte' => 'String', |
|
36 | + 'binary' => 'String', |
|
37 | + 'password' => 'String', |
|
38 | + 'enum' => 'String', |
|
39 | + 'boolean' => 'Boolean', |
|
40 | + 'bool' => 'Boolean', |
|
41 | + 'array' => 'Array', |
|
42 | + 'csv' => 'Array', |
|
43 | + 'ssv' => 'Array', |
|
44 | + 'tsv' => 'Array', |
|
45 | + 'pipes' => 'Array', |
|
46 | + 'date' => 'Date', |
|
47 | + 'datetime' => 'Date', |
|
48 | + 'date-time' => 'Date', |
|
49 | + 'object' => 'Object', |
|
50 | + 'refobject' => 'ReferenceObject', |
|
51 | + 'allof' => 'AllOf', |
|
52 | + ); |
|
53 | + |
|
54 | + private $example; |
|
55 | + |
|
56 | + /** |
|
57 | + * @param AbstractObject $parent |
|
58 | + * @param $definition |
|
59 | + * @constructor |
|
60 | + */ |
|
61 | + public function __construct(AbstractObject $parent, $definition) |
|
62 | + { |
|
63 | + parent::__construct($parent); |
|
64 | + |
|
65 | + $this->parseDefinition($definition); |
|
66 | + } |
|
67 | + |
|
68 | + abstract protected function parseDefinition($definition); |
|
69 | + |
|
70 | + /** |
|
71 | + * @param AbstractObject $parent |
|
72 | + * @param string $definition |
|
73 | + * @param string $error |
|
74 | + * @return self |
|
75 | + * @throws Exception |
|
76 | + */ |
|
77 | + public static function typeFactory($parent, $definition, $error = "Unparseable schema type definition: '%s'") |
|
78 | + { |
|
79 | + // Parse regex |
|
80 | + $match = []; |
|
81 | + if (preg_match('/^([a-z]+)/i', $definition, $match) === 1) { |
|
82 | + $format = strtolower($match[1]); |
|
83 | + } elseif (preg_match('/^(\[)(?:.*?)\]$/', $definition, $match) === 1) { |
|
84 | + $format = 'array'; |
|
85 | + } elseif (preg_match('/^(\{)(?:.*?)\}$/', $definition, $match) === 1) { |
|
86 | + $format = 'object'; |
|
87 | + } else { |
|
88 | + throw new Exception(sprintf($error, $definition)); |
|
89 | + } |
|
90 | + |
|
91 | + // Internal type if type known and not overwritten by definition |
|
92 | + if ($parent->getTypeRegistry()->has($format)) { |
|
93 | + $class = $parent->getTypeRegistry()->get($format); |
|
94 | + return new $class($parent, $definition); |
|
95 | + } |
|
96 | + |
|
97 | + if (isset(self::$classTypes[$format])) { |
|
98 | + $type = self::$classTypes[$format]; |
|
99 | + $class = "\\SwaggerGen\\Swagger\\Type\\{$type}Type"; |
|
100 | + return new $class($parent, $definition); |
|
101 | + } |
|
102 | + |
|
103 | + return new ReferenceObjectType($parent, $definition); |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * Swap values of two variables. |
|
108 | + * Used for sorting. |
|
109 | + * @param mixed $a |
|
110 | + * @param mixed $b |
|
111 | + */ |
|
112 | + protected static function swap(&$a, &$b): void |
|
113 | + { |
|
114 | + $tmp = $a; |
|
115 | + $a = $b; |
|
116 | + $b = $tmp; |
|
117 | + } |
|
118 | + |
|
119 | + /** |
|
120 | + * @param string $list |
|
121 | + * @return array |
|
122 | + */ |
|
123 | + protected static function parseList($list): array |
|
124 | + { |
|
125 | + $ret = []; |
|
126 | + while ($item = self::parseListItem($list)) { |
|
127 | + $ret[] = $item; |
|
128 | + } |
|
129 | + return $ret; |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Extract an item from a comma-separated list of items. |
|
134 | + * |
|
135 | + * i.e. `a(x(x,x)),b(x)` returns `a(x(x,x))` and changes `$list` into `b(x)`. |
|
136 | + * Note: brace nesting is not checked, e.g. `a{b(})` is a valid list item. |
|
137 | + * |
|
138 | + * @param string $list the list to parse |
|
139 | + * @return string the extracted item |
|
140 | + */ |
|
141 | + protected static function parseListItem(&$list): string |
|
142 | + { |
|
143 | + $item = ''; |
|
144 | + |
|
145 | + $depth = 0; |
|
146 | + $index = 0; |
|
147 | + while ($index < strlen($list)) { |
|
148 | + $c = $list[$index++]; |
|
149 | + |
|
150 | + if (str_contains('{([<', $c)) { |
|
151 | + ++$depth; |
|
152 | + } elseif (str_contains('})]>', $c)) { |
|
153 | + --$depth; |
|
154 | + } elseif ($c === ',' && $depth === 0) { |
|
155 | + break; |
|
156 | + } |
|
157 | + |
|
158 | + $item .= $c; |
|
159 | + } |
|
160 | + $list = substr($list, $index); |
|
161 | + |
|
162 | + return $item; |
|
163 | + } |
|
164 | + |
|
165 | + /** |
|
166 | + * Overwrites default AbstractObject parser, since Types should not handle |
|
167 | + * extensions themselves. |
|
168 | + * |
|
169 | + * @param string $command |
|
170 | + * @param string $data |
|
171 | + * @return AbstractType|boolean |
|
172 | + * @throws Exception |
|
173 | + */ |
|
174 | + public function handleCommand($command, $data = null) |
|
175 | + { |
|
176 | + if (strtolower($command) === 'example') { |
|
177 | + if ($data === '') { |
|
178 | + throw new Exception("Missing content for type example"); |
|
179 | + } |
|
180 | + $json = preg_replace_callback('/([^{}:,]+)/', static function ($match) { |
|
181 | + json_decode($match[1]); |
|
182 | + return json_last_error() === JSON_ERROR_NONE ? $match[1] : json_encode($match[1]); |
|
183 | + }, trim($data)); |
|
184 | + $this->example = json_decode($json, true); |
|
185 | + |
|
186 | + // In case input contains special chars, the above preg_replace would fail |
|
187 | + // Input could be a well-formed json already though |
|
188 | + if ($this->example === null) { |
|
189 | + $this->example = json_decode($data, true); |
|
190 | + } |
|
191 | + // If all fails, use input as-is |
|
192 | + if ($this->example === null) { |
|
193 | + $this->example = $data; |
|
194 | + } |
|
195 | + |
|
196 | + return $this; |
|
197 | + } |
|
198 | + |
|
199 | + return false; |
|
200 | + } |
|
201 | + |
|
202 | + public function toArray(): array |
|
203 | + { |
|
204 | + return self::arrayFilterNull(array_merge(array( |
|
205 | + 'example' => $this->example, |
|
206 | + ), parent::toArray())); |
|
207 | + } |
|
208 | 208 | |
209 | 209 | } |
@@ -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 | } |
@@ -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 | } |
@@ -15,199 +15,199 @@ |
||
15 | 15 | class StringType extends AbstractType |
16 | 16 | { |
17 | 17 | |
18 | - private static $formats = array( |
|
19 | - 'string' => '', |
|
20 | - 'byte' => 'byte', |
|
21 | - 'binary' => 'binary', |
|
22 | - 'password' => 'password', |
|
23 | - 'enum' => '', |
|
24 | - ); |
|
25 | - |
|
26 | - /** |
|
27 | - * Name of the type |
|
28 | - * @var string |
|
29 | - */ |
|
30 | - protected $format = ''; |
|
31 | - protected $pattern; |
|
32 | - protected $default; |
|
33 | - protected $maxLength; |
|
34 | - protected $minLength; |
|
35 | - protected $enum = []; |
|
36 | - |
|
37 | - /** |
|
38 | - * @param string $command The comment command |
|
39 | - * @param string $data Any data added after the command |
|
40 | - * @return AbstractType|boolean |
|
41 | - * @throws Exception |
|
42 | - * @throws Exception |
|
43 | - * @throws Exception |
|
44 | - */ |
|
45 | - public function handleCommand($command, $data = null) |
|
46 | - { |
|
47 | - switch (strtolower($command)) { |
|
48 | - case 'default': |
|
49 | - $this->default = $this->validateDefault($data); |
|
50 | - return $this; |
|
51 | - |
|
52 | - case 'pattern': |
|
53 | - $this->pattern = $data; |
|
54 | - return $this; |
|
55 | - |
|
56 | - case 'enum': |
|
57 | - if ($this->minLength !== null || $this->maxLength !== null) { |
|
58 | - throw new Exception("Enumeration not allowed in ranged string: '{$data}'"); |
|
59 | - } |
|
60 | - $words = self::wordSplit($data); |
|
61 | - $this->enum = is_array($this->enum) ? array_merge($this->enum, $words) : $words; |
|
62 | - return $this; |
|
63 | - } |
|
64 | - |
|
65 | - return parent::handleCommand($command, $data); |
|
66 | - } |
|
67 | - |
|
68 | - /** |
|
69 | - * Validate a default string value, depending on subtype |
|
70 | - * |
|
71 | - * @param string $value the value to validate |
|
72 | - * @return string the value after validation (might become trimmed) |
|
73 | - * @throws Exception |
|
74 | - */ |
|
75 | - protected function validateDefault($value): string |
|
76 | - { |
|
77 | - if (empty($value)) { |
|
78 | - if ($this->format) { |
|
79 | - $type = $this->format; |
|
80 | - } else { |
|
81 | - $type = $this->enum ? 'enum' : 'string'; |
|
82 | - } |
|
83 | - throw new Exception("Empty {$type} default"); |
|
84 | - } |
|
85 | - |
|
86 | - if (!empty($this->enum) && !in_array($value, $this->enum, true)) { |
|
87 | - throw new Exception("Invalid enum default: '{$value}'"); |
|
88 | - } |
|
89 | - |
|
90 | - if ($this->maxLength !== null && mb_strlen($value) > $this->maxLength) { |
|
91 | - if ($this->format) { |
|
92 | - $type = $this->format; |
|
93 | - } else { |
|
94 | - $type = $this->enum ? 'enum' : 'string'; |
|
95 | - } |
|
96 | - throw new Exception("Default {$type} length beyond maximum: '{$value}'"); |
|
97 | - } |
|
98 | - |
|
99 | - if ($this->minLength !== null && mb_strlen($value) < $this->minLength) { |
|
100 | - if ($this->format) { |
|
101 | - $type = $this->format; |
|
102 | - } else { |
|
103 | - $type = $this->enum ? 'enum' : 'string'; |
|
104 | - } |
|
105 | - throw new Exception("Default {$type} length beyond minimum: '{$value}'"); |
|
106 | - } |
|
107 | - |
|
108 | - return $value; |
|
109 | - } |
|
110 | - |
|
111 | - public function toArray(): array |
|
112 | - { |
|
113 | - return self::arrayFilterNull(array_merge(array( |
|
114 | - 'type' => 'string', |
|
115 | - 'format' => empty($this->format) ? null : $this->format, |
|
116 | - 'pattern' => $this->pattern, |
|
117 | - 'default' => $this->default, |
|
118 | - 'minLength' => $this->minLength ? (int)$this->minLength : null, |
|
119 | - 'maxLength' => $this->maxLength ? (int)$this->maxLength : null, |
|
120 | - 'enum' => $this->enum, |
|
121 | - ), parent::toArray())); |
|
122 | - } |
|
123 | - |
|
124 | - public function __toString() |
|
125 | - { |
|
126 | - return __CLASS__; |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * @throws Exception |
|
131 | - */ |
|
132 | - protected function parseDefinition($definition): void |
|
133 | - { |
|
134 | - $definition = self::trim($definition); |
|
135 | - |
|
136 | - $match = []; |
|
137 | - if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_CONTENT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
138 | - throw new Exception("Unparseable string definition: '{$definition}'"); |
|
139 | - } |
|
140 | - |
|
141 | - $this->parseFormat($definition, $match); |
|
142 | - $this->parseContent($definition, $match); |
|
143 | - $this->parseRange($definition, $match); |
|
144 | - $this->parseDefault($definition, $match); |
|
145 | - } |
|
146 | - |
|
147 | - /** |
|
148 | - * @param string $definition |
|
149 | - * @param string[] $match |
|
150 | - * @throws Exception |
|
151 | - */ |
|
152 | - private function parseFormat($definition, $match): void |
|
153 | - { |
|
154 | - $type = strtolower($match[1]); |
|
155 | - if (!isset(self::$formats[$type])) { |
|
156 | - throw new Exception("Not a string: '{$definition}'"); |
|
157 | - } |
|
158 | - $this->format = self::$formats[$type]; |
|
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * @param string $definition |
|
163 | - * @param string[] $match |
|
164 | - */ |
|
165 | - private function parseContent($definition, $match): void |
|
166 | - { |
|
167 | - if (strtolower($match[1]) === 'enum') { |
|
168 | - $this->enum = explode(',', $match[2]); |
|
169 | - } else { |
|
170 | - $this->pattern = empty($match[2]) ? null : $match[2]; |
|
171 | - } |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * @param string $definition |
|
176 | - * @param string[] $match |
|
177 | - * @throws Exception |
|
178 | - * @throws Exception |
|
179 | - */ |
|
180 | - private function parseRange($definition, $match): void |
|
181 | - { |
|
182 | - |
|
183 | - if (!empty($match[3])) { |
|
184 | - if ($match[1] === 'enum') { |
|
185 | - throw new Exception("Range not allowed in enumeration definition: '{$definition}'"); |
|
186 | - } |
|
187 | - if ($match[4] === '' && $match[5] === '') { |
|
188 | - throw new Exception("Empty string range: '{$definition}'"); |
|
189 | - } |
|
190 | - $exclusiveMinimum = $match[3] === '<'; |
|
191 | - $this->minLength = $match[4] === '' ? null : $match[4]; |
|
192 | - $this->maxLength = $match[5] === '' ? null : $match[5]; |
|
193 | - $exclusiveMaximum = isset($match[6]) ? ($match[6] === '>') : null; |
|
194 | - if ($this->minLength !== null && $this->maxLength !== null && $this->minLength > $this->maxLength) { |
|
195 | - self::swap($this->minLength, $this->maxLength); |
|
196 | - self::swap($exclusiveMinimum, $exclusiveMaximum); |
|
197 | - } |
|
198 | - $this->minLength = $this->minLength === null ? null : max(0, $exclusiveMinimum ? $this->minLength + 1 : $this->minLength); |
|
199 | - $this->maxLength = $this->maxLength === null ? null : max(0, $exclusiveMaximum ? $this->maxLength - 1 : $this->maxLength); |
|
200 | - } |
|
201 | - } |
|
202 | - |
|
203 | - /** |
|
204 | - * @param string $definition |
|
205 | - * @param string[] $match |
|
206 | - * @throws Exception |
|
207 | - */ |
|
208 | - private function parseDefault($definition, $match): void |
|
209 | - { |
|
210 | - $this->default = isset($match[7]) && $match[7] !== '' ? $this->validateDefault($match[7]) : null; |
|
211 | - } |
|
18 | + private static $formats = array( |
|
19 | + 'string' => '', |
|
20 | + 'byte' => 'byte', |
|
21 | + 'binary' => 'binary', |
|
22 | + 'password' => 'password', |
|
23 | + 'enum' => '', |
|
24 | + ); |
|
25 | + |
|
26 | + /** |
|
27 | + * Name of the type |
|
28 | + * @var string |
|
29 | + */ |
|
30 | + protected $format = ''; |
|
31 | + protected $pattern; |
|
32 | + protected $default; |
|
33 | + protected $maxLength; |
|
34 | + protected $minLength; |
|
35 | + protected $enum = []; |
|
36 | + |
|
37 | + /** |
|
38 | + * @param string $command The comment command |
|
39 | + * @param string $data Any data added after the command |
|
40 | + * @return AbstractType|boolean |
|
41 | + * @throws Exception |
|
42 | + * @throws Exception |
|
43 | + * @throws Exception |
|
44 | + */ |
|
45 | + public function handleCommand($command, $data = null) |
|
46 | + { |
|
47 | + switch (strtolower($command)) { |
|
48 | + case 'default': |
|
49 | + $this->default = $this->validateDefault($data); |
|
50 | + return $this; |
|
51 | + |
|
52 | + case 'pattern': |
|
53 | + $this->pattern = $data; |
|
54 | + return $this; |
|
55 | + |
|
56 | + case 'enum': |
|
57 | + if ($this->minLength !== null || $this->maxLength !== null) { |
|
58 | + throw new Exception("Enumeration not allowed in ranged string: '{$data}'"); |
|
59 | + } |
|
60 | + $words = self::wordSplit($data); |
|
61 | + $this->enum = is_array($this->enum) ? array_merge($this->enum, $words) : $words; |
|
62 | + return $this; |
|
63 | + } |
|
64 | + |
|
65 | + return parent::handleCommand($command, $data); |
|
66 | + } |
|
67 | + |
|
68 | + /** |
|
69 | + * Validate a default string value, depending on subtype |
|
70 | + * |
|
71 | + * @param string $value the value to validate |
|
72 | + * @return string the value after validation (might become trimmed) |
|
73 | + * @throws Exception |
|
74 | + */ |
|
75 | + protected function validateDefault($value): string |
|
76 | + { |
|
77 | + if (empty($value)) { |
|
78 | + if ($this->format) { |
|
79 | + $type = $this->format; |
|
80 | + } else { |
|
81 | + $type = $this->enum ? 'enum' : 'string'; |
|
82 | + } |
|
83 | + throw new Exception("Empty {$type} default"); |
|
84 | + } |
|
85 | + |
|
86 | + if (!empty($this->enum) && !in_array($value, $this->enum, true)) { |
|
87 | + throw new Exception("Invalid enum default: '{$value}'"); |
|
88 | + } |
|
89 | + |
|
90 | + if ($this->maxLength !== null && mb_strlen($value) > $this->maxLength) { |
|
91 | + if ($this->format) { |
|
92 | + $type = $this->format; |
|
93 | + } else { |
|
94 | + $type = $this->enum ? 'enum' : 'string'; |
|
95 | + } |
|
96 | + throw new Exception("Default {$type} length beyond maximum: '{$value}'"); |
|
97 | + } |
|
98 | + |
|
99 | + if ($this->minLength !== null && mb_strlen($value) < $this->minLength) { |
|
100 | + if ($this->format) { |
|
101 | + $type = $this->format; |
|
102 | + } else { |
|
103 | + $type = $this->enum ? 'enum' : 'string'; |
|
104 | + } |
|
105 | + throw new Exception("Default {$type} length beyond minimum: '{$value}'"); |
|
106 | + } |
|
107 | + |
|
108 | + return $value; |
|
109 | + } |
|
110 | + |
|
111 | + public function toArray(): array |
|
112 | + { |
|
113 | + return self::arrayFilterNull(array_merge(array( |
|
114 | + 'type' => 'string', |
|
115 | + 'format' => empty($this->format) ? null : $this->format, |
|
116 | + 'pattern' => $this->pattern, |
|
117 | + 'default' => $this->default, |
|
118 | + 'minLength' => $this->minLength ? (int)$this->minLength : null, |
|
119 | + 'maxLength' => $this->maxLength ? (int)$this->maxLength : null, |
|
120 | + 'enum' => $this->enum, |
|
121 | + ), parent::toArray())); |
|
122 | + } |
|
123 | + |
|
124 | + public function __toString() |
|
125 | + { |
|
126 | + return __CLASS__; |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * @throws Exception |
|
131 | + */ |
|
132 | + protected function parseDefinition($definition): void |
|
133 | + { |
|
134 | + $definition = self::trim($definition); |
|
135 | + |
|
136 | + $match = []; |
|
137 | + if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_CONTENT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
138 | + throw new Exception("Unparseable string definition: '{$definition}'"); |
|
139 | + } |
|
140 | + |
|
141 | + $this->parseFormat($definition, $match); |
|
142 | + $this->parseContent($definition, $match); |
|
143 | + $this->parseRange($definition, $match); |
|
144 | + $this->parseDefault($definition, $match); |
|
145 | + } |
|
146 | + |
|
147 | + /** |
|
148 | + * @param string $definition |
|
149 | + * @param string[] $match |
|
150 | + * @throws Exception |
|
151 | + */ |
|
152 | + private function parseFormat($definition, $match): void |
|
153 | + { |
|
154 | + $type = strtolower($match[1]); |
|
155 | + if (!isset(self::$formats[$type])) { |
|
156 | + throw new Exception("Not a string: '{$definition}'"); |
|
157 | + } |
|
158 | + $this->format = self::$formats[$type]; |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * @param string $definition |
|
163 | + * @param string[] $match |
|
164 | + */ |
|
165 | + private function parseContent($definition, $match): void |
|
166 | + { |
|
167 | + if (strtolower($match[1]) === 'enum') { |
|
168 | + $this->enum = explode(',', $match[2]); |
|
169 | + } else { |
|
170 | + $this->pattern = empty($match[2]) ? null : $match[2]; |
|
171 | + } |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * @param string $definition |
|
176 | + * @param string[] $match |
|
177 | + * @throws Exception |
|
178 | + * @throws Exception |
|
179 | + */ |
|
180 | + private function parseRange($definition, $match): void |
|
181 | + { |
|
182 | + |
|
183 | + if (!empty($match[3])) { |
|
184 | + if ($match[1] === 'enum') { |
|
185 | + throw new Exception("Range not allowed in enumeration definition: '{$definition}'"); |
|
186 | + } |
|
187 | + if ($match[4] === '' && $match[5] === '') { |
|
188 | + throw new Exception("Empty string range: '{$definition}'"); |
|
189 | + } |
|
190 | + $exclusiveMinimum = $match[3] === '<'; |
|
191 | + $this->minLength = $match[4] === '' ? null : $match[4]; |
|
192 | + $this->maxLength = $match[5] === '' ? null : $match[5]; |
|
193 | + $exclusiveMaximum = isset($match[6]) ? ($match[6] === '>') : null; |
|
194 | + if ($this->minLength !== null && $this->maxLength !== null && $this->minLength > $this->maxLength) { |
|
195 | + self::swap($this->minLength, $this->maxLength); |
|
196 | + self::swap($exclusiveMinimum, $exclusiveMaximum); |
|
197 | + } |
|
198 | + $this->minLength = $this->minLength === null ? null : max(0, $exclusiveMinimum ? $this->minLength + 1 : $this->minLength); |
|
199 | + $this->maxLength = $this->maxLength === null ? null : max(0, $exclusiveMaximum ? $this->maxLength - 1 : $this->maxLength); |
|
200 | + } |
|
201 | + } |
|
202 | + |
|
203 | + /** |
|
204 | + * @param string $definition |
|
205 | + * @param string[] $match |
|
206 | + * @throws Exception |
|
207 | + */ |
|
208 | + private function parseDefault($definition, $match): void |
|
209 | + { |
|
210 | + $this->default = isset($match[7]) && $match[7] !== '' ? $this->validateDefault($match[7]) : null; |
|
211 | + } |
|
212 | 212 | |
213 | 213 | } |
@@ -80,7 +80,7 @@ discard block |
||
80 | 80 | } else { |
81 | 81 | $type = $this->enum ? 'enum' : 'string'; |
82 | 82 | } |
83 | - throw new Exception("Empty {$type} default"); |
|
83 | + throw new Exception("empty {$type} default"); |
|
84 | 84 | } |
85 | 85 | |
86 | 86 | if (!empty($this->enum) && !in_array($value, $this->enum, true)) { |
@@ -93,7 +93,7 @@ discard block |
||
93 | 93 | } else { |
94 | 94 | $type = $this->enum ? 'enum' : 'string'; |
95 | 95 | } |
96 | - throw new Exception("Default {$type} length beyond maximum: '{$value}'"); |
|
96 | + throw new Exception("default {$type} length beyond maximum: '{$value}'"); |
|
97 | 97 | } |
98 | 98 | |
99 | 99 | if ($this->minLength !== null && mb_strlen($value) < $this->minLength) { |
@@ -102,7 +102,7 @@ discard block |
||
102 | 102 | } else { |
103 | 103 | $type = $this->enum ? 'enum' : 'string'; |
104 | 104 | } |
105 | - throw new Exception("Default {$type} length beyond minimum: '{$value}'"); |
|
105 | + throw new Exception("default {$type} length beyond minimum: '{$value}'"); |
|
106 | 106 | } |
107 | 107 | |
108 | 108 | return $value; |
@@ -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 | } |
@@ -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)); |
@@ -16,177 +16,177 @@ |
||
16 | 16 | class Response extends AbstractObject |
17 | 17 | { |
18 | 18 | |
19 | - const OK = 200; |
|
20 | - const CREATED = 201; |
|
21 | - const ACCEPTED = 202; |
|
22 | - const NON_AUTHORITATIVE_INFORMATION = 203; |
|
23 | - const NO_CONTENT = 204; |
|
24 | - const RESET_CONTENT = 205; |
|
25 | - const PARTIAL_CONTENT = 206; |
|
26 | - const BAD_REQUEST = 400; |
|
27 | - const UNAUTHORIZED = 401; |
|
28 | - const PAYMENT_REQUIRED = 402; |
|
29 | - const FORBIDDEN = 403; |
|
30 | - const NOT_FOUND = 404; |
|
31 | - const METHOD_NOT_ALLOWED = 405; |
|
32 | - const NOT_ACCEPTABLE = 406; |
|
33 | - const PROXY_AUTHENTICATION_REQUIRED = 407; |
|
34 | - const REQUEST_TIMEOUT = 408; |
|
35 | - const CONFLICT = 409; |
|
36 | - const GONE = 410; |
|
37 | - const LENGTH_REQUIRED = 411; |
|
38 | - const PRECONDITION_FAILED = 412; |
|
39 | - const REQUEST_ENTITY_TOO_LARGE = 413; |
|
40 | - const REQUEST_URI_TOO_LONG = 414; |
|
41 | - const UNSUPPORTED_MEDIA_TYPE = 415; |
|
42 | - const REQUESTED_RANGE_NOT_SATISFIABLE = 416; |
|
43 | - const EXPECTATION_FAILED = 417; |
|
44 | - const UNPROCESSABLE_ENTITY = 422; |
|
45 | - const TOO_MANY_REQUESTS = 429; |
|
46 | - const INTERNAL_SERVER_ERROR = 500; |
|
47 | - const NOT_IMPLEMENTED = 501; // When method is supported for none of the resources |
|
48 | - |
|
49 | - protected static $httpCodes = array( |
|
50 | - self::OK => 'OK', |
|
51 | - self::CREATED => 'Created', |
|
52 | - self::ACCEPTED => 'Accepted', |
|
53 | - self::NON_AUTHORITATIVE_INFORMATION => 'Non-Authoritative Information', |
|
54 | - self::NO_CONTENT => 'No Content', |
|
55 | - self::RESET_CONTENT => 'Reset Content', |
|
56 | - self::PARTIAL_CONTENT => 'Partial Content', |
|
57 | - self::BAD_REQUEST => 'Bad Request', |
|
58 | - self::UNAUTHORIZED => 'Unauthorized', |
|
59 | - self::PAYMENT_REQUIRED => 'Payment Required', |
|
60 | - self::FORBIDDEN => 'Forbidden', |
|
61 | - self::NOT_FOUND => 'Not Found', |
|
62 | - self::METHOD_NOT_ALLOWED => 'Method Not Allowed', |
|
63 | - self::NOT_ACCEPTABLE => 'Not Acceptable', |
|
64 | - self::PROXY_AUTHENTICATION_REQUIRED => 'Proxy Authentication Required', |
|
65 | - self::REQUEST_TIMEOUT => 'Request Timeout', |
|
66 | - self::CONFLICT => 'Conflict', |
|
67 | - self::GONE => 'Gone', |
|
68 | - self::LENGTH_REQUIRED => 'Length Required', |
|
69 | - self::PRECONDITION_FAILED => 'Precondition Failed', |
|
70 | - self::REQUEST_ENTITY_TOO_LARGE => 'Request Entity Too Large', |
|
71 | - self::REQUEST_URI_TOO_LONG => 'Request-URI Too Long', |
|
72 | - self::UNSUPPORTED_MEDIA_TYPE => 'Unsupported Media Type', |
|
73 | - self::REQUESTED_RANGE_NOT_SATISFIABLE => 'Requested Range Not Satisfiable', |
|
74 | - self::EXPECTATION_FAILED => 'Expectation Failed', |
|
75 | - self::UNPROCESSABLE_ENTITY => 'Unprocessable Entity', |
|
76 | - self::TOO_MANY_REQUESTS => 'Too Many Requests', |
|
77 | - self::INTERNAL_SERVER_ERROR => 'Internal Server Error', |
|
78 | - self::NOT_IMPLEMENTED => 'Not Implemented', |
|
79 | - ); |
|
80 | - private $description = ''; |
|
81 | - private $schema; |
|
82 | - |
|
83 | - /** |
|
84 | - * @var Header[] |
|
85 | - */ |
|
86 | - private $Headers = []; |
|
87 | - |
|
88 | - /** |
|
89 | - * JSON examples |
|
90 | - * @var array |
|
91 | - */ |
|
92 | - private $examples = []; |
|
93 | - |
|
94 | - /** |
|
95 | - * @throws Exception |
|
96 | - */ |
|
97 | - public function __construct(AbstractObject $parent, $code, $definition = null, $description = null) |
|
98 | - { |
|
99 | - parent::__construct($parent); |
|
100 | - |
|
101 | - if ($definition) { |
|
102 | - $this->schema = new Schema($this, $definition); |
|
103 | - } |
|
104 | - |
|
105 | - if (!empty($description)) { |
|
106 | - $this->description = $description; |
|
107 | - } elseif (isset(self::$httpCodes[$code])) { |
|
108 | - $this->description = self::$httpCodes[$code]; |
|
109 | - } |
|
110 | - } |
|
111 | - |
|
112 | - public static function getCode($search) |
|
113 | - { |
|
114 | - static $lookup = null; |
|
115 | - |
|
116 | - if (is_numeric($search)) { |
|
117 | - return (int)$search; |
|
118 | - } |
|
119 | - |
|
120 | - // build static lookup table |
|
121 | - if (!$lookup) { |
|
122 | - $lookup = []; |
|
123 | - foreach (self::$httpCodes as $code => $text) { |
|
124 | - $lookup[preg_replace('/[^a-z]+/', '', strtolower($text))] = $code; |
|
125 | - } |
|
126 | - } |
|
127 | - |
|
128 | - $search = preg_replace('/[^a-z]+/', '', strtolower($search)); |
|
129 | - return $lookup[$search] ?? null; |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * @param string $command |
|
134 | - * @param string $data |
|
135 | - * @return AbstractObject|boolean |
|
136 | - * @throws Exception |
|
137 | - * @throws Exception |
|
138 | - * @throws Exception |
|
139 | - * @throws Exception |
|
140 | - * @throws Exception |
|
141 | - */ |
|
142 | - public function handleCommand($command, $data = null) |
|
143 | - { |
|
144 | - switch (strtolower($command)) { |
|
145 | - case 'header': |
|
146 | - $type = self::wordShift($data); |
|
147 | - if (empty($type)) { |
|
148 | - throw new Exception('Missing type for header'); |
|
149 | - } |
|
150 | - $name = self::wordShift($data); |
|
151 | - if (empty($name)) { |
|
152 | - throw new Exception('Missing name for header type \'' . $type . '\''); |
|
153 | - } |
|
154 | - $Header = new Header($this, $type, $data); |
|
155 | - $this->Headers[$name] = $Header; |
|
156 | - return $Header; |
|
157 | - |
|
158 | - case 'example': |
|
159 | - $name = self::wordShift($data); |
|
160 | - if (empty($name)) { |
|
161 | - throw new Exception('Missing name for example'); |
|
162 | - } |
|
163 | - if ($data === '') { |
|
164 | - throw new Exception('Missing content for example `' . $name . '`'); |
|
165 | - } |
|
166 | - $json = preg_replace_callback('/([^{}:]+)/', static function ($match) { |
|
167 | - json_decode($match[1]); |
|
168 | - return json_last_error() === JSON_ERROR_NONE ? $match[1] : json_encode($match[1]); |
|
169 | - }, trim($data)); |
|
170 | - $this->examples[$name] = json_decode($json, true); |
|
171 | - return $this; |
|
172 | - } |
|
173 | - |
|
174 | - return parent::handleCommand($command, $data); |
|
175 | - } |
|
176 | - |
|
177 | - public function toArray(): array |
|
178 | - { |
|
179 | - return self::arrayFilterNull(array_merge(array( |
|
180 | - 'description' => $this->description, |
|
181 | - 'schema' => $this->schema?->toArray(), |
|
182 | - 'headers' => self::objectsToArray($this->Headers), |
|
183 | - 'examples' => $this->examples, |
|
184 | - ), parent::toArray())); |
|
185 | - } |
|
186 | - |
|
187 | - public function __toString() |
|
188 | - { |
|
189 | - return __CLASS__; |
|
190 | - } |
|
19 | + const OK = 200; |
|
20 | + const CREATED = 201; |
|
21 | + const ACCEPTED = 202; |
|
22 | + const NON_AUTHORITATIVE_INFORMATION = 203; |
|
23 | + const NO_CONTENT = 204; |
|
24 | + const RESET_CONTENT = 205; |
|
25 | + const PARTIAL_CONTENT = 206; |
|
26 | + const BAD_REQUEST = 400; |
|
27 | + const UNAUTHORIZED = 401; |
|
28 | + const PAYMENT_REQUIRED = 402; |
|
29 | + const FORBIDDEN = 403; |
|
30 | + const NOT_FOUND = 404; |
|
31 | + const METHOD_NOT_ALLOWED = 405; |
|
32 | + const NOT_ACCEPTABLE = 406; |
|
33 | + const PROXY_AUTHENTICATION_REQUIRED = 407; |
|
34 | + const REQUEST_TIMEOUT = 408; |
|
35 | + const CONFLICT = 409; |
|
36 | + const GONE = 410; |
|
37 | + const LENGTH_REQUIRED = 411; |
|
38 | + const PRECONDITION_FAILED = 412; |
|
39 | + const REQUEST_ENTITY_TOO_LARGE = 413; |
|
40 | + const REQUEST_URI_TOO_LONG = 414; |
|
41 | + const UNSUPPORTED_MEDIA_TYPE = 415; |
|
42 | + const REQUESTED_RANGE_NOT_SATISFIABLE = 416; |
|
43 | + const EXPECTATION_FAILED = 417; |
|
44 | + const UNPROCESSABLE_ENTITY = 422; |
|
45 | + const TOO_MANY_REQUESTS = 429; |
|
46 | + const INTERNAL_SERVER_ERROR = 500; |
|
47 | + const NOT_IMPLEMENTED = 501; // When method is supported for none of the resources |
|
48 | + |
|
49 | + protected static $httpCodes = array( |
|
50 | + self::OK => 'OK', |
|
51 | + self::CREATED => 'Created', |
|
52 | + self::ACCEPTED => 'Accepted', |
|
53 | + self::NON_AUTHORITATIVE_INFORMATION => 'Non-Authoritative Information', |
|
54 | + self::NO_CONTENT => 'No Content', |
|
55 | + self::RESET_CONTENT => 'Reset Content', |
|
56 | + self::PARTIAL_CONTENT => 'Partial Content', |
|
57 | + self::BAD_REQUEST => 'Bad Request', |
|
58 | + self::UNAUTHORIZED => 'Unauthorized', |
|
59 | + self::PAYMENT_REQUIRED => 'Payment Required', |
|
60 | + self::FORBIDDEN => 'Forbidden', |
|
61 | + self::NOT_FOUND => 'Not Found', |
|
62 | + self::METHOD_NOT_ALLOWED => 'Method Not Allowed', |
|
63 | + self::NOT_ACCEPTABLE => 'Not Acceptable', |
|
64 | + self::PROXY_AUTHENTICATION_REQUIRED => 'Proxy Authentication Required', |
|
65 | + self::REQUEST_TIMEOUT => 'Request Timeout', |
|
66 | + self::CONFLICT => 'Conflict', |
|
67 | + self::GONE => 'Gone', |
|
68 | + self::LENGTH_REQUIRED => 'Length Required', |
|
69 | + self::PRECONDITION_FAILED => 'Precondition Failed', |
|
70 | + self::REQUEST_ENTITY_TOO_LARGE => 'Request Entity Too Large', |
|
71 | + self::REQUEST_URI_TOO_LONG => 'Request-URI Too Long', |
|
72 | + self::UNSUPPORTED_MEDIA_TYPE => 'Unsupported Media Type', |
|
73 | + self::REQUESTED_RANGE_NOT_SATISFIABLE => 'Requested Range Not Satisfiable', |
|
74 | + self::EXPECTATION_FAILED => 'Expectation Failed', |
|
75 | + self::UNPROCESSABLE_ENTITY => 'Unprocessable Entity', |
|
76 | + self::TOO_MANY_REQUESTS => 'Too Many Requests', |
|
77 | + self::INTERNAL_SERVER_ERROR => 'Internal Server Error', |
|
78 | + self::NOT_IMPLEMENTED => 'Not Implemented', |
|
79 | + ); |
|
80 | + private $description = ''; |
|
81 | + private $schema; |
|
82 | + |
|
83 | + /** |
|
84 | + * @var Header[] |
|
85 | + */ |
|
86 | + private $Headers = []; |
|
87 | + |
|
88 | + /** |
|
89 | + * JSON examples |
|
90 | + * @var array |
|
91 | + */ |
|
92 | + private $examples = []; |
|
93 | + |
|
94 | + /** |
|
95 | + * @throws Exception |
|
96 | + */ |
|
97 | + public function __construct(AbstractObject $parent, $code, $definition = null, $description = null) |
|
98 | + { |
|
99 | + parent::__construct($parent); |
|
100 | + |
|
101 | + if ($definition) { |
|
102 | + $this->schema = new Schema($this, $definition); |
|
103 | + } |
|
104 | + |
|
105 | + if (!empty($description)) { |
|
106 | + $this->description = $description; |
|
107 | + } elseif (isset(self::$httpCodes[$code])) { |
|
108 | + $this->description = self::$httpCodes[$code]; |
|
109 | + } |
|
110 | + } |
|
111 | + |
|
112 | + public static function getCode($search) |
|
113 | + { |
|
114 | + static $lookup = null; |
|
115 | + |
|
116 | + if (is_numeric($search)) { |
|
117 | + return (int)$search; |
|
118 | + } |
|
119 | + |
|
120 | + // build static lookup table |
|
121 | + if (!$lookup) { |
|
122 | + $lookup = []; |
|
123 | + foreach (self::$httpCodes as $code => $text) { |
|
124 | + $lookup[preg_replace('/[^a-z]+/', '', strtolower($text))] = $code; |
|
125 | + } |
|
126 | + } |
|
127 | + |
|
128 | + $search = preg_replace('/[^a-z]+/', '', strtolower($search)); |
|
129 | + return $lookup[$search] ?? null; |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * @param string $command |
|
134 | + * @param string $data |
|
135 | + * @return AbstractObject|boolean |
|
136 | + * @throws Exception |
|
137 | + * @throws Exception |
|
138 | + * @throws Exception |
|
139 | + * @throws Exception |
|
140 | + * @throws Exception |
|
141 | + */ |
|
142 | + public function handleCommand($command, $data = null) |
|
143 | + { |
|
144 | + switch (strtolower($command)) { |
|
145 | + case 'header': |
|
146 | + $type = self::wordShift($data); |
|
147 | + if (empty($type)) { |
|
148 | + throw new Exception('Missing type for header'); |
|
149 | + } |
|
150 | + $name = self::wordShift($data); |
|
151 | + if (empty($name)) { |
|
152 | + throw new Exception('Missing name for header type \'' . $type . '\''); |
|
153 | + } |
|
154 | + $Header = new Header($this, $type, $data); |
|
155 | + $this->Headers[$name] = $Header; |
|
156 | + return $Header; |
|
157 | + |
|
158 | + case 'example': |
|
159 | + $name = self::wordShift($data); |
|
160 | + if (empty($name)) { |
|
161 | + throw new Exception('Missing name for example'); |
|
162 | + } |
|
163 | + if ($data === '') { |
|
164 | + throw new Exception('Missing content for example `' . $name . '`'); |
|
165 | + } |
|
166 | + $json = preg_replace_callback('/([^{}:]+)/', static function ($match) { |
|
167 | + json_decode($match[1]); |
|
168 | + return json_last_error() === JSON_ERROR_NONE ? $match[1] : json_encode($match[1]); |
|
169 | + }, trim($data)); |
|
170 | + $this->examples[$name] = json_decode($json, true); |
|
171 | + return $this; |
|
172 | + } |
|
173 | + |
|
174 | + return parent::handleCommand($command, $data); |
|
175 | + } |
|
176 | + |
|
177 | + public function toArray(): array |
|
178 | + { |
|
179 | + return self::arrayFilterNull(array_merge(array( |
|
180 | + 'description' => $this->description, |
|
181 | + 'schema' => $this->schema?->toArray(), |
|
182 | + 'headers' => self::objectsToArray($this->Headers), |
|
183 | + 'examples' => $this->examples, |
|
184 | + ), parent::toArray())); |
|
185 | + } |
|
186 | + |
|
187 | + public function __toString() |
|
188 | + { |
|
189 | + return __CLASS__; |
|
190 | + } |
|
191 | 191 | |
192 | 192 | } |