Skip to content

Commit

Permalink
Merge branch 'master' into feature-factory
Browse files Browse the repository at this point in the history
  • Loading branch information
Art4 committed Aug 28, 2015
2 parents 790f976 + f6723b2 commit ab4e2ab
Show file tree
Hide file tree
Showing 20 changed files with 170 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/Utils/AccessTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,48 @@
*/
trait AccessTrait
{
/**
* Transforms objects to arrays
*
* @param $val
* @return mixed
*/
protected function objectTransform($val)
{
if (!is_object($val))
{
return $val;
}
elseif (is_callable([$val, 'asArray']))
{
return $val->asArray(true);
}
else
{
// Fallback for stdClass objects
return json_decode(json_encode($val), true);
}
}

/**
* Convert this object in an array
*
* @param bool $fullArray If true, objects are transformed into arrays recursively
* @return array
*/
public function asArray()
public function asArray($fullArray = false)
{
$return = array();

foreach($this->getKeys() as $key)
{
$return[$key] = $this->get($key);
$val = $this->get($key);

if ($fullArray) {
$return[$key] = $this->objectTransform($val);
} else {
$return[$key] = $val;
}
}

return $return;
Expand Down
12 changes: 12 additions & 0 deletions tests/integration/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public function testParseSimpleResource()
$this->assertInstanceOf('Art4\JsonApiClient\Attributes', $resource->get('attributes'));
$this->assertTrue($resource->has('relationships'));
$this->assertInstanceOf('Art4\JsonApiClient\RelationshipCollection', $resource->get('relationships'));

// Test full array
$this->assertEquals(json_decode($string, true), $document->asArray(true));
}

/**
Expand All @@ -67,6 +70,9 @@ public function testParseSimpleResourceIdentifier()
$this->assertFalse($resource->has('meta'));
$this->assertSame($resource->get('type'), 'articles');
$this->assertSame($resource->get('id'), '1');

// Test full array
$this->assertEquals(json_decode($string, true), $document->asArray(true));
}

/**
Expand Down Expand Up @@ -124,6 +130,9 @@ public function testParseResourceObject()
$this->assertInstanceOf('Art4\JsonApiClient\Resource\Identifier', $data);
$this->assertSame($data->get('type'), 'people');
$this->assertSame($data->get('id'), '9');

// Test full array
$this->assertEquals(json_decode($string, true), $document->asArray(true));
}

/**
Expand Down Expand Up @@ -300,5 +309,8 @@ public function testParseCompleteResourceObjectWithMultipleRelationships()
$this->assertInstanceOf('Art4\JsonApiClient\Link', $links);
$this->assertTrue($links->has('self'));
$this->assertSame($links->get('self'), 'http://example.com/comments/12');

// Test full array
$this->assertEquals(json_decode($string, true), $document->asArray(true));
}
}
12 changes: 12 additions & 0 deletions tests/unit/AttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ public function testCreateWithObject()
'false' => $attributes->get('false'),
'null' => $attributes->get('null'),
));

// Test full array
$this->assertSame($attributes->asArray(true), array(
'object' => (array) $attributes->get('object'),
'array' => $attributes->get('array'),
'string' => $attributes->get('string'),
'number_int' => $attributes->get('number_int'),
'number_float' => $attributes->get('number_float'),
'true' => $attributes->get('true'),
'false' => $attributes->get('false'),
'null' => $attributes->get('null'),
));
}

/**
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/DocumentLinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ public function testOnlySelfRelatedPaginationPropertiesExists()
'related' => $link->get('related'),
'pagination' => $link->get('pagination'),
));

// Test full array
$this->assertSame($link->asArray(true), array(
'self' => $link->get('self'),
'related' => $link->get('related'),
'pagination' => $link->get('pagination')->asArray(true),
));
}

/**
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public function testCreateWithObjectReturnsSelf()
$this->assertSame($document->asArray(), array(
'meta' => $document->get('meta'),
));

// Test full array
$this->assertSame($document->asArray(true), array(
'meta' => $document->get('meta')->asArray(true),
));
}

/**
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/ErrorCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ public function testCreate()
$collection->get(0),
$collection->get(1),
));

// Test full array
$this->assertSame($collection->asArray(true), array(
$collection->get(0)->asArray(true),
$collection->get(1)->asArray(true),
));
}

/**
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/ErrorLinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public function testOnlyAboutPropertyExists()
$this->assertSame($link->asArray(), array(
'about' => $link->get('about'),
));

// Test full array
$this->assertSame($link->asArray(true), array(
'about' => $link->get('about'),
));
}

/**
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/ErrorSourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ public function testOnlyPointerParameterPropertiesExists()
'pointer' => $source->get('pointer'),
'parameter' => $source->get('parameter'),
));

// Test full array
$this->assertSame($source->asArray(true), array(
'pointer' => $source->get('pointer'),
'parameter' => $source->get('parameter'),
));
}

/**
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/ErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ public function testCreateWithObjectReturnsSelf()
'source' => $error->get('source'),
'meta' => $error->get('meta'),
));

// Test full array
$this->assertSame($error->asArray(true), array(
'id' => 'id',
'links' => $error->get('links')->asArray(true),
'status' => 'status',
'code' => 'code',
'title' => 'title',
'detail' => 'detail',
'source' => $error->get('source')->asArray(true),
'meta' => $error->get('meta')->asArray(true),
));
}

/**
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/JsonapiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ public function testCreateWithObject()
'version' => $jsonapi->get('version'),
'meta' => $jsonapi->get('meta'),
));

// Test full array
$this->assertSame($jsonapi->asArray(true), array(
'version' => $jsonapi->get('version'),
'meta' => $jsonapi->get('meta')->asArray(true),
));
}

/**
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/LinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ public function testCreateWithObject()
'link' => $link->get('link'),
'meta' => $link->get('meta'),
));

// Test full array
$this->assertSame($link->asArray(true), array(
'href' => $link->get('href'),
'linkobj' => $link->get('linkobj')->asArray(true),
'link' => $link->get('link'),
'meta' => $link->get('meta')->asArray(true),
));
}

/**
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/MetaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ public function testCreateWithObject()
'false' => false,
'null' => null,
));

// Test full array
$this->assertSame($meta->asArray(true), array(
'object' => (array) $meta->get('object'),
'array' => array(),
'string' => 'string',
'number_int' => 654,
'number_float' => 654.321,
'true' => true,
'false' => false,
'null' => null,
));
}

/**
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/PaginationLinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ public function testOnlyPaginationPropertiesExists()
'last' => $link->get('last'),
'next' => $link->get('next'),
));

// Test full array
$this->assertSame($link->asArray(true), array(
'last' => $link->get('last'),
'next' => $link->get('next'),
));
}

/**
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/RelationshipCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public function testCreateWithObject()
$this->assertSame($collection->asArray(), array(
'author' => $collection->get('author'),
));

// Test full array
$this->assertSame($collection->asArray(true), array(
'author' => $collection->get('author')->asArray(true),
));
}

/**
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/RelationshipLinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ public function testOnlySelfRelatedPaginationPropertiesExists()
'related' => $link->get('related'),
'pagination' => $link->get('pagination'),
));

// Test full array
$this->assertSame($link->asArray(true), array(
'self' => $link->get('self'),
'related' => $link->get('related'),
'pagination' => $link->get('pagination')->asArray(true),
));
}

/**
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/RelationshipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public function testCreateWithObjectReturnsSelf()
$meta = $relationship->get('meta');

$this->assertSame($relationship->asArray(), array('meta' => $meta));

// Test full array
$this->assertSame($relationship->asArray(true), array('meta' => $meta->asArray(true)));
}

/**
Expand Down Expand Up @@ -79,6 +82,9 @@ public function testCreateWithLinksObject()
$this->assertInstanceOf('Art4\JsonApiClient\RelationshipLink', $links);

$this->assertSame($relationship->asArray(), array('links' => $links));

// Test full array
$this->assertSame($relationship->asArray(true), array('links' => $links->asArray(true)));
}

/**
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/Resource/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ public function testCreateWithIdentifier()
$collection->get(1),
$collection->get(2),
));

$this->assertSame($collection->asArray(true), array(
$collection->get(0)->asArray(true),
$collection->get(1)->asArray(true),
$collection->get(2)->asArray(true),
));
}

/**
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/Resource/IdentifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public function testCreateWithObject()
'type' => $identifier->get('type'),
'id' => $identifier->get('id'),
));

$this->assertSame($identifier->asArray(true), array(
'type' => $identifier->get('type'),
'id' => $identifier->get('id'),
));
}

/**
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/Resource/ItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,14 @@ public function testCreateWithFullObject()
'relationships' => $resource->get('relationships'),
'links' => $resource->get('links'),
));

// Test full array
$this->assertSame($resource->asArray(true), array(
'type' => $resource->get('type'),
'id' => $resource->get('id'),
'attributes' => $resource->get('attributes')->asArray(true),
'relationships' => $resource->get('relationships')->asArray(true),
'links' => $resource->get('links')->asArray(true),
));
}
}
3 changes: 3 additions & 0 deletions tests/unit/Resource/NullResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,8 @@ public function testCreateWithDataProvider($input)
$this->assertFalse($resource->isCollection());

$this->assertSame($resource->asArray(), null);

// Test full array
$this->assertSame($resource->asArray(true), null);
}
}

0 comments on commit ab4e2ab

Please sign in to comment.