Skip to content

Commit

Permalink
Updated to phootwork/collection v1.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
gossi committed Jun 17, 2016
1 parent 89d2555 commit f8b47eb
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 36 deletions.
15 changes: 14 additions & 1 deletion src/AbstractModel.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace gossi\swagger;

use phootwork\collection\Collection;

abstract class AbstractModel {

protected function export() {
Expand All @@ -26,7 +28,9 @@ protected function export() {
$prop->setAccessible(true);
$val = $prop->getValue($this);

if (method_exists($val, 'toArray')) {
if ($val instanceof Collection) {
$val = $this->exportRecursiveArray($val->toArray());
} else if (method_exists($val, 'toArray')) {
$val = $val->toArray();
}
}
Expand All @@ -42,4 +46,13 @@ protected function export() {

return $out;
}

protected function exportRecursiveArray($array) {
return array_map(function ($v) {
if (is_object($v) && method_exists($v, 'toArray')) {
return $v->toArray();
}
return $v;
}, $array);
}
}
13 changes: 8 additions & 5 deletions src/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@ private function parse($contents) {
}

public function toArray() {
return array_merge($this->operations->toArray(), $this->getExtensions()->toArray());
return array_merge(
$this->exportRecursiveArray($this->operations->toArray()),
$this->exportRecursiveArray($this->getExtensions()->toArray())
);
}

/**
* Returns this path
*
*
* @return string
*/
public function getPath() {
Expand All @@ -49,7 +52,7 @@ public function getPath() {

/**
* Gets the operation for the given method, creates one if none exists
*
*
* @param string $method
* @return Operation
*/
Expand All @@ -62,7 +65,7 @@ public function getOperation($method) {
}

/**
*
*
* @param string $method
* @return bool
*/
Expand All @@ -72,7 +75,7 @@ public function hasOperation($method) {

/**
* Removes an operation for the given method
*
*
* @param string $method
*/
public function removeOperation($method) {
Expand Down
15 changes: 8 additions & 7 deletions src/collections/Definitions.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
use phootwork\collection\CollectionUtils;
use phootwork\collection\Map;
use phootwork\lang\Arrayable;
use gossi\swagger\AbstractModel;

class Definitions implements Arrayable, \Iterator {
class Definitions extends AbstractModel implements Arrayable, \Iterator {

/** @var Map */
private $definitions;
Expand All @@ -25,7 +26,7 @@ private function parse($contents) {
}

public function toArray() {
return $this->definitions->toArray();
return $this->exportRecursiveArray($this->definitions->toArray());
}

public function size() {
Expand All @@ -34,7 +35,7 @@ public function size() {

/**
* Returns the schema for the given field
*
*
* @param string $name
* @return Schema
*/
Expand All @@ -47,7 +48,7 @@ public function get($name) {

/**
* Sets the field
*
*
* @param string name
* @param Schema $schame
*/
Expand All @@ -57,7 +58,7 @@ public function set($name, Schema $schema) {

/**
* Removes the given field
*
*
* @param string $name
*/
public function remove($name) {
Expand All @@ -66,7 +67,7 @@ public function remove($name) {

/**
* Returns definitions has a schema with the given name
*
*
* @param string $name
* @return bool
*/
Expand All @@ -76,7 +77,7 @@ public function has($name) {

/**
* Returns whether the given schema exists
*
*
* @param Schema $schema
* @return bool
*/
Expand Down
13 changes: 7 additions & 6 deletions src/collections/Headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
use phootwork\collection\CollectionUtils;
use phootwork\collection\Map;
use phootwork\lang\Arrayable;
use gossi\swagger\AbstractModel;

class Headers implements Arrayable, \Iterator {
class Headers extends AbstractModel implements Arrayable, \Iterator {

/** @var Map */
private $headers;
Expand All @@ -26,7 +27,7 @@ private function parse($contents) {
}

public function toArray() {
return $this->headers->toArray();
return $this->exportRecursiveArray($this->headers->toArray());
}

public function size() {
Expand All @@ -35,7 +36,7 @@ public function size() {

/**
* Returns whether a header with the given name exists
*
*
* @param string $header
* @return bool
*/
Expand All @@ -55,7 +56,7 @@ public function contains(Header $header) {

/**
* Returns the header info for the given code
*
*
* @param string $header
* @return Header
*/
Expand All @@ -65,7 +66,7 @@ public function get($header) {

/**
* Sets the header
*
*
* @param Header $header
*/
public function add(Header $header) {
Expand All @@ -74,7 +75,7 @@ public function add(Header $header) {

/**
* Removes the given header
*
*
* @param string $header
*/
public function remove($header) {
Expand Down
5 changes: 3 additions & 2 deletions src/collections/Parameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
use phootwork\collection\ArrayList;
use phootwork\collection\CollectionUtils;
use phootwork\lang\Arrayable;
use gossi\swagger\AbstractModel;

class Parameters implements Arrayable, \Iterator {
class Parameters extends AbstractModel implements Arrayable, \Iterator {

use RefPart;

Expand Down Expand Up @@ -36,7 +37,7 @@ public function toArray() {
return ['$ref' => $this->getRef()];
}

return $this->parameters->toArray();
return $this->exportRecursiveArray($this->parameters->toArray());
}

public function size() {
Expand Down
18 changes: 10 additions & 8 deletions src/collections/Paths.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
use phootwork\collection\Map;
use phootwork\lang\Arrayable;
use phootwork\lang\Text;
use gossi\swagger\AbstractModel;

class Paths implements Arrayable, \Iterator {
class Paths extends AbstractModel implements Arrayable, \Iterator {

use ExtensionPart;

Expand All @@ -35,9 +36,10 @@ private function parse($contents) {
}

public function toArray() {
$paths = clone $this->paths;
// $paths = clone $this->paths;
// $paths->setAll($this->getExtensions());
return $paths->toArray();
// return $paths->toArray();
return $this->exportRecursiveArray($this->paths->toArray());
}

public function size() {
Expand All @@ -46,7 +48,7 @@ public function size() {

/**
* Returns whether a path with the given name exists
*
*
* @param string $path
* @return bool
*/
Expand All @@ -56,7 +58,7 @@ public function has($path) {

/**
* Returns whether the given path exists
*
*
* @param Path $path
* @return bool
*/
Expand All @@ -66,7 +68,7 @@ public function contains(Path $path) {

/**
* Returns the path info for the given path
*
*
* @param string $path
* @return Path
*/
Expand All @@ -79,7 +81,7 @@ public function get($path) {

/**
* Sets the path
*
*
* @param Path $path
* @return $this
*/
Expand All @@ -90,7 +92,7 @@ public function add(Path $path) {

/**
* Removes the given path
*
*
* @param string $path
*/
public function remove($path) {
Expand Down
16 changes: 9 additions & 7 deletions src/collections/Responses.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
use phootwork\collection\Map;
use phootwork\lang\Arrayable;
use phootwork\lang\Text;
use gossi\swagger\AbstractModel;

class Responses implements Arrayable, \Iterator {
class Responses extends AbstractModel implements Arrayable, \Iterator {

use ExtensionPart;

Expand Down Expand Up @@ -37,7 +38,8 @@ private function parse($contents) {
public function toArray() {
$responses = clone $this->responses;
$responses->setAll($this->getExtensions());
return $responses->toArray();

return $this->exportRecursiveArray($responses->toArray());
}

public function size() {
Expand All @@ -46,7 +48,7 @@ public function size() {

/**
* Returns whether the given response exists
*
*
* @param string $code
* @return bool
*/
Expand All @@ -56,7 +58,7 @@ public function has($code) {

/**
* Returns whether the given response exists
*
*
* @param Response $response
* @return bool
*/
Expand All @@ -66,7 +68,7 @@ public function contains(Response $response) {

/**
* Returns the reponse info for the given code
*
*
* @param string $code
* @return Response
*/
Expand All @@ -80,7 +82,7 @@ public function get($code) {

/**
* Sets the response
*
*
* @param Response $code
*/
public function add(Response $response) {
Expand All @@ -89,7 +91,7 @@ public function add(Response $response) {

/**
* Removes the given repsonse
*
*
* @param string $code
*/
public function remove($code) {
Expand Down
9 changes: 9 additions & 0 deletions tests/CollectionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public function testDefinitions() {
$this->assertSame($user, $definitions->get('User'));
$this->assertTrue($definitions->contains($user));

$this->assertTrue(is_array($definitions->toArray()['User']));

$definitions->remove('User');
$this->assertEquals(0, $definitions->size());
$this->assertFalse($definitions->has('User'));
Expand All @@ -52,6 +54,8 @@ public function testPaths() {
$this->assertSame($pets, $paths->get('/pets'));
$this->assertTrue($paths->contains($pets));

$this->assertTrue(is_array($paths->toArray()['/pets']));

$paths->remove('/pets');
$this->assertEquals(0, $paths->size());
$this->assertFalse($paths->has('/pets'));
Expand Down Expand Up @@ -94,6 +98,9 @@ public function testParameters() {
$this->assertEquals('bar', $parameter->getName());
$this->assertEquals('query', $parameter->getIn());

$this->assertTrue(is_array($parameters->toArray()[0]));
$this->assertTrue(is_array($parameters->toArray()[1]));

$parameters->remove($id);
$parameters->remove($id2);
$parameters->remove($parameter);
Expand Down Expand Up @@ -121,6 +128,8 @@ public function testResponses() {
$this->assertSame($ok, $responses->get('200'));
$this->assertTrue($responses->contains($ok));

$this->assertTrue(is_array($responses->toArray()['200']));

$responses->remove('200');
$this->assertEquals(0, $responses->size());
$this->assertFalse($responses->has('200'));
Expand Down

0 comments on commit f8b47eb

Please sign in to comment.