Skip to content

Commit 161464f

Browse files
committed
ArrayAccess and Interator for Paths and ResponseArrayAccess and
Interator for Paths and Responsess
1 parent 51653a1 commit 161464f

File tree

6 files changed

+344
-2
lines changed

6 files changed

+344
-2
lines changed

doc/class-diagram.png

29 KB
Loading

doc/class-diagram.uml

Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Diagram>
3+
<ID>PHP</ID>
4+
<OriginalElement>\cebe\openapi\SpecObjectInterface</OriginalElement>
5+
<nodes>
6+
<node x="2464.0" y="270.0">\cebe\openapi\spec\SecurityRequirement</node>
7+
<node x="1640.5" y="0.0">\cebe\openapi\SpecObjectInterface</node>
8+
<node x="1727.0" y="85.0">\cebe\openapi\spec\Responses</node>
9+
<node x="2763.0" y="270.0">\cebe\openapi\spec\PathItem</node>
10+
<node x="718.0000000000001" y="270.0">\cebe\openapi\spec\Xml</node>
11+
<node x="1833.0" y="270.0">\cebe\openapi\spec\Response</node>
12+
<node x="1484.0" y="270.0">\cebe\openapi\spec\RequestBody</node>
13+
<node x="1219.0" y="270.0">\cebe\openapi\spec\Encoding</node>
14+
<node x="267.0" y="270.0">\cebe\openapi\spec\ExternalDocumentation</node>
15+
<node x="1734.0" y="270.0">\cebe\openapi\spec\Server</node>
16+
<node x="2111.0" y="270.0">\cebe\openapi\spec\MediaType</node>
17+
<node x="125.0" y="270.0">\cebe\openapi\spec\Discriminator</node>
18+
<node x="2657.0" y="270.0">\cebe\openapi\spec\Schema</node>
19+
<node x="1474.0" y="85.0">\cebe\openapi\SpecBaseObject</node>
20+
<node x="478.0" y="270.0">\cebe\openapi\spec\License</node>
21+
<node x="875.0000000000001" y="270.0">\cebe\openapi\spec\OAuthFlow</node>
22+
<node x="581.0000000000001" y="270.0">\cebe\openapi\spec\Components</node>
23+
<node x="1331.0" y="270.0">\cebe\openapi\spec\ServerVariable</node>
24+
<node x="0.0" y="270.0">\cebe\openapi\spec\Parameter</node>
25+
<node x="2344.0" y="270.0">\cebe\openapi\spec\Operation</node>
26+
<node x="1625.0" y="270.0">\cebe\openapi\spec\OpenApi</node>
27+
<node x="1635.0" y="85.0">\cebe\openapi\spec\Paths</node>
28+
<node x="1138.0" y="270.0">\cebe\openapi\spec\Link</node>
29+
<node x="11.0" y="355.0">\cebe\openapi\spec\Header</node>
30+
<node x="2988.0" y="270.0">\cebe\openapi\spec\Info</node>
31+
<node x="2878.0" y="270.0">\cebe\openapi\spec\Example</node>
32+
<node x="1950.0" y="270.0">\cebe\openapi\spec\SecurityScheme</node>
33+
<node x="1003.0000000000001" y="270.0">\cebe\openapi\spec\OAuthFlows</node>
34+
<node x="797.0000000000001" y="270.0">\cebe\openapi\spec\Tag</node>
35+
<node x="1851.0" y="85.0">\cebe\openapi\spec\Callback</node>
36+
<node x="2237.0" y="270.0">\cebe\openapi\spec\Contact</node>
37+
</nodes>
38+
<notes />
39+
<edges>
40+
<edge source="\cebe\openapi\spec\License" target="\cebe\openapi\SpecBaseObject">
41+
<point x="0.0" y="-17.5" />
42+
<point x="519.5" y="170.0" />
43+
<point x="1493.7400000000002" y="170.0" />
44+
<point x="-50.75999999999999" y="17.5" />
45+
</edge>
46+
<edge source="\cebe\openapi\spec\MediaType" target="\cebe\openapi\SpecBaseObject">
47+
<point x="1.1368683772161603E-13" y="-17.5" />
48+
<point x="2164.0" y="210.0" />
49+
<point x="1572.7000000000003" y="210.0" />
50+
<point x="28.200000000000045" y="17.5" />
51+
</edge>
52+
<edge source="\cebe\openapi\spec\ServerVariable" target="\cebe\openapi\SpecBaseObject">
53+
<point x="0.0" y="-17.5" />
54+
<point x="1397.5" y="250.0" />
55+
<point x="1538.8600000000001" y="250.0" />
56+
<point x="-5.639999999999873" y="17.5" />
57+
</edge>
58+
<edge source="\cebe\openapi\spec\Server" target="\cebe\openapi\SpecBaseObject">
59+
<point x="0.0" y="-17.5" />
60+
<point x="1773.5" y="240.0" />
61+
<point x="1555.7800000000002" y="240.0" />
62+
<point x="11.279999999999973" y="17.5" />
63+
</edge>
64+
<edge source="\cebe\openapi\spec\Info" target="\cebe\openapi\SpecBaseObject">
65+
<point x="0.0" y="-17.5" />
66+
<point x="3017.5" y="140.0" />
67+
<point x="1612.1800000000003" y="140.0" />
68+
<point x="67.68000000000006" y="17.5" />
69+
</edge>
70+
<edge source="\cebe\openapi\spec\Parameter" target="\cebe\openapi\SpecBaseObject">
71+
<point x="0.0" y="-17.5" />
72+
<point x="52.5" y="140.0" />
73+
<point x="1476.8200000000002" y="140.0" />
74+
<point x="-67.68000000000006" y="17.5" />
75+
</edge>
76+
<edge source="\cebe\openapi\spec\OAuthFlow" target="\cebe\openapi\SpecBaseObject">
77+
<point x="0.0" y="-17.5" />
78+
<point x="929.0000000000001" y="210.0" />
79+
<point x="1516.3000000000002" y="210.0" />
80+
<point x="-28.200000000000045" y="17.5" />
81+
</edge>
82+
<edge source="\cebe\openapi\spec\ExternalDocumentation" target="\cebe\openapi\SpecBaseObject">
83+
<point x="0.0" y="-17.5" />
84+
<point x="362.5" y="160.0" />
85+
<point x="1488.1" y="160.0" />
86+
<point x="-56.40000000000009" y="17.5" />
87+
</edge>
88+
<edge source="\cebe\openapi\spec\OAuthFlows" target="\cebe\openapi\SpecBaseObject">
89+
<point x="0.0" y="-17.5" />
90+
<point x="1060.5" y="220.0" />
91+
<point x="1521.94" y="220.0" />
92+
<point x="-22.559999999999945" y="17.5" />
93+
</edge>
94+
<edge source="\cebe\openapi\spec\Response" target="\cebe\openapi\SpecBaseObject">
95+
<point x="0.0" y="-17.5" />
96+
<point x="1881.5" y="230.0" />
97+
<point x="1561.42" y="230.0" />
98+
<point x="16.920000000000073" y="17.5" />
99+
</edge>
100+
<edge source="\cebe\openapi\spec\Tag" target="\cebe\openapi\SpecBaseObject">
101+
<point x="0.0" y="-17.5" />
102+
<point x="826.0000000000001" y="200.0" />
103+
<point x="1510.6600000000003" y="200.0" />
104+
<point x="-33.83999999999992" y="17.5" />
105+
</edge>
106+
<edge source="\cebe\openapi\spec\Responses" target="\cebe\openapi\SpecObjectInterface">
107+
<point x="0.0" y="-17.5" />
108+
<point x="1779.0" y="60.0" />
109+
<point x="1746.125" y="60.0" />
110+
<point x="21.125" y="17.5" />
111+
</edge>
112+
<edge source="\cebe\openapi\spec\Contact" target="\cebe\openapi\SpecBaseObject">
113+
<point x="0.0" y="-17.5" />
114+
<point x="2280.5" y="200.0" />
115+
<point x="1578.3400000000001" y="200.0" />
116+
<point x="33.83999999999992" y="17.5" />
117+
</edge>
118+
<edge source="\cebe\openapi\spec\Paths" target="\cebe\openapi\SpecObjectInterface">
119+
<point x="0.0" y="-17.5" />
120+
<point x="1671.0" y="60.0" />
121+
<point x="1703.875" y="60.0" />
122+
<point x="-21.125" y="17.5" />
123+
</edge>
124+
<edge source="\cebe\openapi\spec\Components" target="\cebe\openapi\SpecBaseObject">
125+
<point x="0.0" y="-17.5" />
126+
<point x="639.5000000000001" y="180.0" />
127+
<point x="1499.38" y="180.0" />
128+
<point x="-45.11999999999989" y="17.5" />
129+
</edge>
130+
<edge source="\cebe\openapi\spec\Callback" target="\cebe\openapi\SpecObjectInterface">
131+
<point x="0.0" y="-17.5" />
132+
<point x="1896.5" y="60.0" />
133+
<point x="1788.375" y="60.0" />
134+
<point x="63.375" y="17.5" />
135+
</edge>
136+
<edge source="\cebe\openapi\spec\Example" target="\cebe\openapi\SpecBaseObject">
137+
<point x="0.0" y="-17.5" />
138+
<point x="2923.0" y="150.0" />
139+
<point x="1606.54" y="150.0" />
140+
<point x="62.039999999999964" y="17.5" />
141+
</edge>
142+
<edge source="\cebe\openapi\SpecBaseObject" target="\cebe\openapi\SpecObjectInterface">
143+
<point x="0.0" y="-17.5" />
144+
<point x="1544.5" y="60.0" />
145+
<point x="1661.625" y="60.0" />
146+
<point x="-63.375" y="17.5" />
147+
</edge>
148+
<edge source="\cebe\openapi\spec\SecurityScheme" target="\cebe\openapi\SpecBaseObject">
149+
<point x="0.0" y="-17.5" />
150+
<point x="2020.5" y="220.0" />
151+
<point x="1567.06" y="220.0" />
152+
<point x="22.559999999999945" y="17.5" />
153+
</edge>
154+
<edge source="\cebe\openapi\spec\SecurityRequirement" target="\cebe\openapi\SpecBaseObject">
155+
<point x="0.0" y="-17.5" />
156+
<point x="2550.5" y="180.0" />
157+
<point x="1589.6200000000003" y="180.0" />
158+
<point x="45.12000000000012" y="17.5" />
159+
</edge>
160+
<edge source="\cebe\openapi\spec\RequestBody" target="\cebe\openapi\SpecBaseObject">
161+
<point x="0.0" y="-17.5" />
162+
<point x="0.0" y="17.5" />
163+
</edge>
164+
<edge source="\cebe\openapi\spec\Header" target="\cebe\openapi\spec\Parameter">
165+
<point x="0.0" y="-17.5" />
166+
<point x="0.0" y="17.5" />
167+
</edge>
168+
<edge source="\cebe\openapi\spec\Xml" target="\cebe\openapi\SpecBaseObject">
169+
<point x="0.0" y="-17.5" />
170+
<point x="747.5000000000001" y="190.0" />
171+
<point x="1505.02" y="190.0" />
172+
<point x="-39.48000000000002" y="17.5" />
173+
</edge>
174+
<edge source="\cebe\openapi\spec\OpenApi" target="\cebe\openapi\SpecBaseObject">
175+
<point x="0.0" y="-17.5" />
176+
<point x="1669.5" y="250.0" />
177+
<point x="1550.1400000000003" y="250.0" />
178+
<point x="5.6400000000001" y="17.5" />
179+
</edge>
180+
<edge source="\cebe\openapi\spec\Encoding" target="\cebe\openapi\SpecBaseObject">
181+
<point x="0.0" y="-17.5" />
182+
<point x="1265.0" y="240.0" />
183+
<point x="1533.2200000000003" y="240.0" />
184+
<point x="-11.279999999999973" y="17.5" />
185+
</edge>
186+
<edge source="\cebe\openapi\spec\Operation" target="\cebe\openapi\SpecBaseObject">
187+
<point x="0.0" y="-17.5" />
188+
<point x="2394.0" y="190.0" />
189+
<point x="1583.98" y="190.0" />
190+
<point x="39.48000000000002" y="17.5" />
191+
</edge>
192+
<edge source="\cebe\openapi\spec\PathItem" target="\cebe\openapi\SpecBaseObject">
193+
<point x="0.0" y="-17.5" />
194+
<point x="2810.5" y="160.0" />
195+
<point x="1600.9" y="160.0" />
196+
<point x="56.40000000000009" y="17.5" />
197+
</edge>
198+
<edge source="\cebe\openapi\spec\Schema" target="\cebe\openapi\SpecBaseObject">
199+
<point x="0.0" y="-17.5" />
200+
<point x="2700.0" y="170.0" />
201+
<point x="1595.2600000000002" y="170.0" />
202+
<point x="50.75999999999999" y="17.5" />
203+
</edge>
204+
<edge source="\cebe\openapi\spec\Discriminator" target="\cebe\openapi\SpecBaseObject">
205+
<point x="0.0" y="-17.5" />
206+
<point x="186.0" y="150.0" />
207+
<point x="1482.46" y="150.0" />
208+
<point x="-62.039999999999964" y="17.5" />
209+
</edge>
210+
<edge source="\cebe\openapi\spec\Link" target="\cebe\openapi\SpecBaseObject">
211+
<point x="0.0" y="-17.5" />
212+
<point x="1168.5" y="230.0" />
213+
<point x="1527.58" y="230.0" />
214+
<point x="-16.920000000000073" y="17.5" />
215+
</edge>
216+
</edges>
217+
<settings layout="Hierarchic Group" zoom="1.0" x="598.0" y="150.0" />
218+
<SelectedNodes>
219+
<node>\cebe\openapi\spec\Responses</node>
220+
<node>\cebe\openapi\spec\Paths</node>
221+
<node>\cebe\openapi\spec\Callback</node>
222+
</SelectedNodes>
223+
<Categories />
224+
<VISIBILITY>private</VISIBILITY>
225+
</Diagram>
226+

src/spec/Paths.php

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@
77

88
namespace cebe\openapi\spec;
99

10+
use ArrayAccess;
11+
use ArrayIterator;
12+
use cebe\openapi\exceptions\ReadonlyPropertyException;
1013
use cebe\openapi\SpecObjectInterface;
14+
use Countable;
15+
use IteratorAggregate;
16+
use Traversable;
1117

1218
/**
1319
* Holds the relative paths to the individual endpoints and their operations.
@@ -18,7 +24,7 @@
1824
* @link https://github.com/OAI/OpenAPI-Specification/blob/3.0.2/versions/3.0.2.md#pathsObject
1925
*
2026
*/
21-
class Paths implements SpecObjectInterface
27+
class Paths implements SpecObjectInterface, ArrayAccess, Countable, IteratorAggregate
2228
{
2329
/**
2430
* @var PathItem[]
@@ -108,4 +114,71 @@ public function getErrors(): array
108114
}
109115
return array_merge(...$errors);
110116
}
117+
118+
/**
119+
* Whether a offset exists
120+
* @link http://php.net/manual/en/arrayaccess.offsetexists.php
121+
* @param mixed $offset An offset to check for.
122+
* @return boolean true on success or false on failure.
123+
* The return value will be casted to boolean if non-boolean was returned.
124+
*/
125+
public function offsetExists($offset)
126+
{
127+
return $this->hasPath($offset);
128+
}
129+
130+
/**
131+
* Offset to retrieve
132+
* @link http://php.net/manual/en/arrayaccess.offsetget.php
133+
* @param mixed $offset The offset to retrieve.
134+
* @return PathItem Can return all value types.
135+
*/
136+
public function offsetGet($offset)
137+
{
138+
return $this->getPath($offset);
139+
}
140+
141+
/**
142+
* Offset to set
143+
* @link http://php.net/manual/en/arrayaccess.offsetset.php
144+
* @param mixed $offset The offset to assign the value to.
145+
* @param mixed $value The value to set.
146+
* @throws ReadonlyPropertyException because spec objects are read-only.
147+
*/
148+
public function offsetSet($offset, $value)
149+
{
150+
throw new ReadonlyPropertyException('Setting read-only property: ' . \get_class($this) . '::' . $offset);
151+
}
152+
153+
/**
154+
* Offset to unset
155+
* @link http://php.net/manual/en/arrayaccess.offsetunset.php
156+
* @param mixed $offset The offset to unset.
157+
* @throws ReadonlyPropertyException because spec objects are read-only.
158+
*/
159+
public function offsetUnset($offset)
160+
{
161+
throw new ReadonlyPropertyException('Unsetting read-only property: ' . \get_class($this) . '::' . $offset);
162+
}
163+
164+
/**
165+
* Count elements of an object
166+
* @link http://php.net/manual/en/countable.count.php
167+
* @return int The custom count as an integer.
168+
* The return value is cast to an integer.
169+
*/
170+
public function count()
171+
{
172+
return count($this->_paths);
173+
}
174+
175+
/**
176+
* Retrieve an external iterator
177+
* @link http://php.net/manual/en/iteratoraggregate.getiterator.php
178+
* @return Traversable An instance of an object implementing <b>Iterator</b> or <b>Traversable</b>
179+
*/
180+
public function getIterator()
181+
{
182+
return new ArrayIterator($this->_paths);
183+
}
111184
}

src/spec/Responses.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,20 @@
77

88
namespace cebe\openapi\spec;
99

10+
use ArrayAccess;
11+
use ArrayIterator;
1012
use cebe\openapi\exceptions\ReadonlyPropertyException;
1113
use cebe\openapi\SpecObjectInterface;
14+
use Countable;
15+
use IteratorAggregate;
16+
use Traversable;
1217

1318
/**
1419
* A container for the expected responses of an operation.
1520
*
1621
* @link https://github.com/OAI/OpenAPI-Specification/blob/3.0.2/versions/3.0.2.md#responsesObject
1722
*/
18-
class Responses implements SpecObjectInterface, \ArrayAccess
23+
class Responses implements SpecObjectInterface, ArrayAccess, Countable, IteratorAggregate
1924
{
2025
private $_responses = [];
2126
private $_errors = [];
@@ -145,4 +150,25 @@ public function offsetUnset($offset)
145150
{
146151
throw new ReadonlyPropertyException('Unsetting read-only property: ' . \get_class($this) . '::' . $offset);
147152
}
153+
154+
/**
155+
* Count elements of an object
156+
* @link http://php.net/manual/en/countable.count.php
157+
* @return int The custom count as an integer.
158+
* The return value is cast to an integer.
159+
*/
160+
public function count()
161+
{
162+
return count($this->_responses);
163+
}
164+
165+
/**
166+
* Retrieve an external iterator
167+
* @link http://php.net/manual/en/iteratoraggregate.getiterator.php
168+
* @return Traversable An instance of an object implementing <b>Iterator</b> or <b>Traversable</b>
169+
*/
170+
public function getIterator()
171+
{
172+
return new ArrayIterator($this->_responses);
173+
}
148174
}

tests/spec/PathTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,22 @@ public function testRead()
4545
$this->assertTrue($result);
4646

4747
$this->assertTrue($paths->hasPath('/pets'));
48+
$this->assertTrue(isset($paths['/pets']));
4849
$this->assertFalse($paths->hasPath('/dog'));
50+
$this->assertFalse(isset($paths['/dog']));
4951

5052
$this->assertInstanceOf(PathItem::class, $paths->getPath('/pets'));
53+
$this->assertInstanceOf(PathItem::class, $paths['/pets']);
5154
$this->assertInstanceOf(Operation::class, $paths->getPath('/pets')->get);
5255
$this->assertNull($paths->getPath('/dog'));
56+
$this->assertNull($paths['/dog']);
5357

5458
$this->assertCount(1, $paths->getPaths());
59+
$this->assertCount(1, $paths);
60+
foreach($paths as $path => $pathItem) {
61+
$this->assertEquals('/pets', $path);
62+
$this->assertInstanceOf(PathItem::class, $pathItem);
63+
}
5564
}
5665

5766
public function testInvalidPath()

0 commit comments

Comments
 (0)