Skip to content

Commit

Permalink
ArrayAccess and Interator for Paths and ResponseArrayAccess and
Browse files Browse the repository at this point in the history
Interator for Paths and Responsess
  • Loading branch information
cebe committed Nov 16, 2018
1 parent 51653a1 commit 161464f
Show file tree
Hide file tree
Showing 6 changed files with 344 additions and 2 deletions.
Binary file added doc/class-diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
226 changes: 226 additions & 0 deletions doc/class-diagram.uml
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
<?xml version="1.0" encoding="UTF-8"?>
<Diagram>
<ID>PHP</ID>
<OriginalElement>\cebe\openapi\SpecObjectInterface</OriginalElement>
<nodes>
<node x="2464.0" y="270.0">\cebe\openapi\spec\SecurityRequirement</node>
<node x="1640.5" y="0.0">\cebe\openapi\SpecObjectInterface</node>
<node x="1727.0" y="85.0">\cebe\openapi\spec\Responses</node>
<node x="2763.0" y="270.0">\cebe\openapi\spec\PathItem</node>
<node x="718.0000000000001" y="270.0">\cebe\openapi\spec\Xml</node>
<node x="1833.0" y="270.0">\cebe\openapi\spec\Response</node>
<node x="1484.0" y="270.0">\cebe\openapi\spec\RequestBody</node>
<node x="1219.0" y="270.0">\cebe\openapi\spec\Encoding</node>
<node x="267.0" y="270.0">\cebe\openapi\spec\ExternalDocumentation</node>
<node x="1734.0" y="270.0">\cebe\openapi\spec\Server</node>
<node x="2111.0" y="270.0">\cebe\openapi\spec\MediaType</node>
<node x="125.0" y="270.0">\cebe\openapi\spec\Discriminator</node>
<node x="2657.0" y="270.0">\cebe\openapi\spec\Schema</node>
<node x="1474.0" y="85.0">\cebe\openapi\SpecBaseObject</node>
<node x="478.0" y="270.0">\cebe\openapi\spec\License</node>
<node x="875.0000000000001" y="270.0">\cebe\openapi\spec\OAuthFlow</node>
<node x="581.0000000000001" y="270.0">\cebe\openapi\spec\Components</node>
<node x="1331.0" y="270.0">\cebe\openapi\spec\ServerVariable</node>
<node x="0.0" y="270.0">\cebe\openapi\spec\Parameter</node>
<node x="2344.0" y="270.0">\cebe\openapi\spec\Operation</node>
<node x="1625.0" y="270.0">\cebe\openapi\spec\OpenApi</node>
<node x="1635.0" y="85.0">\cebe\openapi\spec\Paths</node>
<node x="1138.0" y="270.0">\cebe\openapi\spec\Link</node>
<node x="11.0" y="355.0">\cebe\openapi\spec\Header</node>
<node x="2988.0" y="270.0">\cebe\openapi\spec\Info</node>
<node x="2878.0" y="270.0">\cebe\openapi\spec\Example</node>
<node x="1950.0" y="270.0">\cebe\openapi\spec\SecurityScheme</node>
<node x="1003.0000000000001" y="270.0">\cebe\openapi\spec\OAuthFlows</node>
<node x="797.0000000000001" y="270.0">\cebe\openapi\spec\Tag</node>
<node x="1851.0" y="85.0">\cebe\openapi\spec\Callback</node>
<node x="2237.0" y="270.0">\cebe\openapi\spec\Contact</node>
</nodes>
<notes />
<edges>
<edge source="\cebe\openapi\spec\License" target="\cebe\openapi\SpecBaseObject">
<point x="0.0" y="-17.5" />
<point x="519.5" y="170.0" />
<point x="1493.7400000000002" y="170.0" />
<point x="-50.75999999999999" y="17.5" />
</edge>
<edge source="\cebe\openapi\spec\MediaType" target="\cebe\openapi\SpecBaseObject">
<point x="1.1368683772161603E-13" y="-17.5" />
<point x="2164.0" y="210.0" />
<point x="1572.7000000000003" y="210.0" />
<point x="28.200000000000045" y="17.5" />
</edge>
<edge source="\cebe\openapi\spec\ServerVariable" target="\cebe\openapi\SpecBaseObject">
<point x="0.0" y="-17.5" />
<point x="1397.5" y="250.0" />
<point x="1538.8600000000001" y="250.0" />
<point x="-5.639999999999873" y="17.5" />
</edge>
<edge source="\cebe\openapi\spec\Server" target="\cebe\openapi\SpecBaseObject">
<point x="0.0" y="-17.5" />
<point x="1773.5" y="240.0" />
<point x="1555.7800000000002" y="240.0" />
<point x="11.279999999999973" y="17.5" />
</edge>
<edge source="\cebe\openapi\spec\Info" target="\cebe\openapi\SpecBaseObject">
<point x="0.0" y="-17.5" />
<point x="3017.5" y="140.0" />
<point x="1612.1800000000003" y="140.0" />
<point x="67.68000000000006" y="17.5" />
</edge>
<edge source="\cebe\openapi\spec\Parameter" target="\cebe\openapi\SpecBaseObject">
<point x="0.0" y="-17.5" />
<point x="52.5" y="140.0" />
<point x="1476.8200000000002" y="140.0" />
<point x="-67.68000000000006" y="17.5" />
</edge>
<edge source="\cebe\openapi\spec\OAuthFlow" target="\cebe\openapi\SpecBaseObject">
<point x="0.0" y="-17.5" />
<point x="929.0000000000001" y="210.0" />
<point x="1516.3000000000002" y="210.0" />
<point x="-28.200000000000045" y="17.5" />
</edge>
<edge source="\cebe\openapi\spec\ExternalDocumentation" target="\cebe\openapi\SpecBaseObject">
<point x="0.0" y="-17.5" />
<point x="362.5" y="160.0" />
<point x="1488.1" y="160.0" />
<point x="-56.40000000000009" y="17.5" />
</edge>
<edge source="\cebe\openapi\spec\OAuthFlows" target="\cebe\openapi\SpecBaseObject">
<point x="0.0" y="-17.5" />
<point x="1060.5" y="220.0" />
<point x="1521.94" y="220.0" />
<point x="-22.559999999999945" y="17.5" />
</edge>
<edge source="\cebe\openapi\spec\Response" target="\cebe\openapi\SpecBaseObject">
<point x="0.0" y="-17.5" />
<point x="1881.5" y="230.0" />
<point x="1561.42" y="230.0" />
<point x="16.920000000000073" y="17.5" />
</edge>
<edge source="\cebe\openapi\spec\Tag" target="\cebe\openapi\SpecBaseObject">
<point x="0.0" y="-17.5" />
<point x="826.0000000000001" y="200.0" />
<point x="1510.6600000000003" y="200.0" />
<point x="-33.83999999999992" y="17.5" />
</edge>
<edge source="\cebe\openapi\spec\Responses" target="\cebe\openapi\SpecObjectInterface">
<point x="0.0" y="-17.5" />
<point x="1779.0" y="60.0" />
<point x="1746.125" y="60.0" />
<point x="21.125" y="17.5" />
</edge>
<edge source="\cebe\openapi\spec\Contact" target="\cebe\openapi\SpecBaseObject">
<point x="0.0" y="-17.5" />
<point x="2280.5" y="200.0" />
<point x="1578.3400000000001" y="200.0" />
<point x="33.83999999999992" y="17.5" />
</edge>
<edge source="\cebe\openapi\spec\Paths" target="\cebe\openapi\SpecObjectInterface">
<point x="0.0" y="-17.5" />
<point x="1671.0" y="60.0" />
<point x="1703.875" y="60.0" />
<point x="-21.125" y="17.5" />
</edge>
<edge source="\cebe\openapi\spec\Components" target="\cebe\openapi\SpecBaseObject">
<point x="0.0" y="-17.5" />
<point x="639.5000000000001" y="180.0" />
<point x="1499.38" y="180.0" />
<point x="-45.11999999999989" y="17.5" />
</edge>
<edge source="\cebe\openapi\spec\Callback" target="\cebe\openapi\SpecObjectInterface">
<point x="0.0" y="-17.5" />
<point x="1896.5" y="60.0" />
<point x="1788.375" y="60.0" />
<point x="63.375" y="17.5" />
</edge>
<edge source="\cebe\openapi\spec\Example" target="\cebe\openapi\SpecBaseObject">
<point x="0.0" y="-17.5" />
<point x="2923.0" y="150.0" />
<point x="1606.54" y="150.0" />
<point x="62.039999999999964" y="17.5" />
</edge>
<edge source="\cebe\openapi\SpecBaseObject" target="\cebe\openapi\SpecObjectInterface">
<point x="0.0" y="-17.5" />
<point x="1544.5" y="60.0" />
<point x="1661.625" y="60.0" />
<point x="-63.375" y="17.5" />
</edge>
<edge source="\cebe\openapi\spec\SecurityScheme" target="\cebe\openapi\SpecBaseObject">
<point x="0.0" y="-17.5" />
<point x="2020.5" y="220.0" />
<point x="1567.06" y="220.0" />
<point x="22.559999999999945" y="17.5" />
</edge>
<edge source="\cebe\openapi\spec\SecurityRequirement" target="\cebe\openapi\SpecBaseObject">
<point x="0.0" y="-17.5" />
<point x="2550.5" y="180.0" />
<point x="1589.6200000000003" y="180.0" />
<point x="45.12000000000012" y="17.5" />
</edge>
<edge source="\cebe\openapi\spec\RequestBody" target="\cebe\openapi\SpecBaseObject">
<point x="0.0" y="-17.5" />
<point x="0.0" y="17.5" />
</edge>
<edge source="\cebe\openapi\spec\Header" target="\cebe\openapi\spec\Parameter">
<point x="0.0" y="-17.5" />
<point x="0.0" y="17.5" />
</edge>
<edge source="\cebe\openapi\spec\Xml" target="\cebe\openapi\SpecBaseObject">
<point x="0.0" y="-17.5" />
<point x="747.5000000000001" y="190.0" />
<point x="1505.02" y="190.0" />
<point x="-39.48000000000002" y="17.5" />
</edge>
<edge source="\cebe\openapi\spec\OpenApi" target="\cebe\openapi\SpecBaseObject">
<point x="0.0" y="-17.5" />
<point x="1669.5" y="250.0" />
<point x="1550.1400000000003" y="250.0" />
<point x="5.6400000000001" y="17.5" />
</edge>
<edge source="\cebe\openapi\spec\Encoding" target="\cebe\openapi\SpecBaseObject">
<point x="0.0" y="-17.5" />
<point x="1265.0" y="240.0" />
<point x="1533.2200000000003" y="240.0" />
<point x="-11.279999999999973" y="17.5" />
</edge>
<edge source="\cebe\openapi\spec\Operation" target="\cebe\openapi\SpecBaseObject">
<point x="0.0" y="-17.5" />
<point x="2394.0" y="190.0" />
<point x="1583.98" y="190.0" />
<point x="39.48000000000002" y="17.5" />
</edge>
<edge source="\cebe\openapi\spec\PathItem" target="\cebe\openapi\SpecBaseObject">
<point x="0.0" y="-17.5" />
<point x="2810.5" y="160.0" />
<point x="1600.9" y="160.0" />
<point x="56.40000000000009" y="17.5" />
</edge>
<edge source="\cebe\openapi\spec\Schema" target="\cebe\openapi\SpecBaseObject">
<point x="0.0" y="-17.5" />
<point x="2700.0" y="170.0" />
<point x="1595.2600000000002" y="170.0" />
<point x="50.75999999999999" y="17.5" />
</edge>
<edge source="\cebe\openapi\spec\Discriminator" target="\cebe\openapi\SpecBaseObject">
<point x="0.0" y="-17.5" />
<point x="186.0" y="150.0" />
<point x="1482.46" y="150.0" />
<point x="-62.039999999999964" y="17.5" />
</edge>
<edge source="\cebe\openapi\spec\Link" target="\cebe\openapi\SpecBaseObject">
<point x="0.0" y="-17.5" />
<point x="1168.5" y="230.0" />
<point x="1527.58" y="230.0" />
<point x="-16.920000000000073" y="17.5" />
</edge>
</edges>
<settings layout="Hierarchic Group" zoom="1.0" x="598.0" y="150.0" />
<SelectedNodes>
<node>\cebe\openapi\spec\Responses</node>
<node>\cebe\openapi\spec\Paths</node>
<node>\cebe\openapi\spec\Callback</node>
</SelectedNodes>
<Categories />
<VISIBILITY>private</VISIBILITY>
</Diagram>

75 changes: 74 additions & 1 deletion src/spec/Paths.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@

namespace cebe\openapi\spec;

use ArrayAccess;
use ArrayIterator;
use cebe\openapi\exceptions\ReadonlyPropertyException;
use cebe\openapi\SpecObjectInterface;
use Countable;
use IteratorAggregate;
use Traversable;

/**
* Holds the relative paths to the individual endpoints and their operations.
Expand All @@ -18,7 +24,7 @@
* @link https://github.com/OAI/OpenAPI-Specification/blob/3.0.2/versions/3.0.2.md#pathsObject
*
*/
class Paths implements SpecObjectInterface
class Paths implements SpecObjectInterface, ArrayAccess, Countable, IteratorAggregate
{
/**
* @var PathItem[]
Expand Down Expand Up @@ -108,4 +114,71 @@ public function getErrors(): array
}
return array_merge(...$errors);
}

/**
* Whether a offset exists
* @link http://php.net/manual/en/arrayaccess.offsetexists.php
* @param mixed $offset An offset to check for.
* @return boolean true on success or false on failure.
* The return value will be casted to boolean if non-boolean was returned.
*/
public function offsetExists($offset)
{
return $this->hasPath($offset);
}

/**
* Offset to retrieve
* @link http://php.net/manual/en/arrayaccess.offsetget.php
* @param mixed $offset The offset to retrieve.
* @return PathItem Can return all value types.
*/
public function offsetGet($offset)
{
return $this->getPath($offset);
}

/**
* Offset to set
* @link http://php.net/manual/en/arrayaccess.offsetset.php
* @param mixed $offset The offset to assign the value to.
* @param mixed $value The value to set.
* @throws ReadonlyPropertyException because spec objects are read-only.
*/
public function offsetSet($offset, $value)
{
throw new ReadonlyPropertyException('Setting read-only property: ' . \get_class($this) . '::' . $offset);
}

/**
* Offset to unset
* @link http://php.net/manual/en/arrayaccess.offsetunset.php
* @param mixed $offset The offset to unset.
* @throws ReadonlyPropertyException because spec objects are read-only.
*/
public function offsetUnset($offset)
{
throw new ReadonlyPropertyException('Unsetting read-only property: ' . \get_class($this) . '::' . $offset);
}

/**
* Count elements of an object
* @link http://php.net/manual/en/countable.count.php
* @return int The custom count as an integer.
* The return value is cast to an integer.
*/
public function count()
{
return count($this->_paths);
}

/**
* Retrieve an external iterator
* @link http://php.net/manual/en/iteratoraggregate.getiterator.php
* @return Traversable An instance of an object implementing <b>Iterator</b> or <b>Traversable</b>
*/
public function getIterator()
{
return new ArrayIterator($this->_paths);
}
}
28 changes: 27 additions & 1 deletion src/spec/Responses.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@

namespace cebe\openapi\spec;

use ArrayAccess;
use ArrayIterator;
use cebe\openapi\exceptions\ReadonlyPropertyException;
use cebe\openapi\SpecObjectInterface;
use Countable;
use IteratorAggregate;
use Traversable;

/**
* A container for the expected responses of an operation.
*
* @link https://github.com/OAI/OpenAPI-Specification/blob/3.0.2/versions/3.0.2.md#responsesObject
*/
class Responses implements SpecObjectInterface, \ArrayAccess
class Responses implements SpecObjectInterface, ArrayAccess, Countable, IteratorAggregate
{
private $_responses = [];
private $_errors = [];
Expand Down Expand Up @@ -145,4 +150,25 @@ public function offsetUnset($offset)
{
throw new ReadonlyPropertyException('Unsetting read-only property: ' . \get_class($this) . '::' . $offset);
}

/**
* Count elements of an object
* @link http://php.net/manual/en/countable.count.php
* @return int The custom count as an integer.
* The return value is cast to an integer.
*/
public function count()
{
return count($this->_responses);
}

/**
* Retrieve an external iterator
* @link http://php.net/manual/en/iteratoraggregate.getiterator.php
* @return Traversable An instance of an object implementing <b>Iterator</b> or <b>Traversable</b>
*/
public function getIterator()
{
return new ArrayIterator($this->_responses);
}
}
9 changes: 9 additions & 0 deletions tests/spec/PathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,22 @@ public function testRead()
$this->assertTrue($result);

$this->assertTrue($paths->hasPath('/pets'));
$this->assertTrue(isset($paths['/pets']));
$this->assertFalse($paths->hasPath('/dog'));
$this->assertFalse(isset($paths['/dog']));

$this->assertInstanceOf(PathItem::class, $paths->getPath('/pets'));
$this->assertInstanceOf(PathItem::class, $paths['/pets']);
$this->assertInstanceOf(Operation::class, $paths->getPath('/pets')->get);
$this->assertNull($paths->getPath('/dog'));
$this->assertNull($paths['/dog']);

$this->assertCount(1, $paths->getPaths());
$this->assertCount(1, $paths);
foreach($paths as $path => $pathItem) {
$this->assertEquals('/pets', $path);
$this->assertInstanceOf(PathItem::class, $pathItem);
}
}

public function testInvalidPath()
Expand Down
Loading

0 comments on commit 161464f

Please sign in to comment.