Skip to content

Commit

Permalink
Deprecated ArrayMeta (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
f3ath authored Aug 31, 2017
1 parent 7d9aa50 commit 8b98f7d
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 22 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Implementation of [JSON API](http://jsonapi.org) in PHP 7
This library is an attempt to express business rules of JSON API specification in a set of PHP 7 classes.

A simple example to illustrate the general idea. This (slightly modified) JSON representation from
A simple example to illustrate the general idea. This JSON representation from
[the documentation](http://jsonapi.org/format/#document-resource-objects)

```json
Expand All @@ -19,8 +19,8 @@ A simple example to illustrate the general idea. This (slightly modified) JSON r
"id": "9"
},
"links": {
"self": "\/articles\/1\/relationships\/author",
"related": "\/articles\/1\/author"
"self": "/articles/1/relationships/author",
"related": "/articles/1/author"
}
}
}
Expand All @@ -29,6 +29,7 @@ A simple example to illustrate the general idea. This (slightly modified) JSON r
```
can be built with the following php code:
```php
<?php
$articles = new ResourceObject('articles', '1');
$author = Relationship::fromLinkage(
Linkage::fromSingleIdentifier(
Expand All @@ -40,7 +41,7 @@ $author->setLink('related', '/articles/1/author');
$articles->setRelationship('author', $author);
$articles->setAttribute('title', 'Rails is Omakase');
$doc = Document::fromResource($articles);
echo json_encode($doc, JSON_PRETTY_PRINT);
echo json_encode($doc, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
```

Please refer to [the tests](./test) for the full API documentation:
Expand Down
3 changes: 3 additions & 0 deletions src/Document/ArrayMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

namespace JsonApiPhp\JsonApi\Document;

/**
* @deprecated to be removed in 1.0, use Meta::fromArray()
*/
class ArrayMeta extends Meta
{
public function __construct(array $array)
Expand Down
5 changes: 5 additions & 0 deletions src/Document/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public function __construct(\stdClass $data)
$this->data = $data;
}

public static function fromArray(array $array): self
{
return new self((object) $array);
}

public function jsonSerialize()
{
return $this->data;
Expand Down
4 changes: 2 additions & 2 deletions test/Document/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
namespace JsonApiPhp\JsonApi\Test\Document;

use JsonApiPhp\JsonApi\Document;
use JsonApiPhp\JsonApi\Document\ArrayMeta;
use JsonApiPhp\JsonApi\Document\Error;
use JsonApiPhp\JsonApi\Document\Meta;
use JsonApiPhp\JsonApi\Document\Resource\NullResource;
use JsonApiPhp\JsonApi\Document\Resource\ResourceIdentifier;
use JsonApiPhp\JsonApi\Test\BaseTestCase;
Expand All @@ -24,7 +24,7 @@ public function testCanCreateFromMeta()
{
$this->assertEqualsAsJson(
['meta' => ['foo' => 'bar']],
Document::fromMeta(new ArrayMeta(['foo' => 'bar']))
Document::fromMeta(Meta::fromArray(['foo' => 'bar']))
);
}

Expand Down
2 changes: 1 addition & 1 deletion test/Document/ErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function testErrorWithFullSetOfProperties()
$e->setDetail('Nothing is found');
$e->setSourcePointer('/data');
$e->setSourceParameter('test_param');
$e->setMeta(new ArrayMeta(['foo' => 'bar']));
$e->setMeta(Meta::fromArray(['foo' => 'bar']));

$this->assertEqualsAsJson(
[
Expand Down
4 changes: 2 additions & 2 deletions test/Document/MetaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@

namespace JsonApiPhp\JsonApi\Test\Document;

use JsonApiPhp\JsonApi\Document\ArrayMeta;
use JsonApiPhp\JsonApi\Document\Meta;
use PHPUnit\Framework\TestCase;

class MetaTest extends TestCase
{
public function testPhpArraysAreConvertedToObjects()
{
$this->assertEquals('{"0":"foo"}', json_encode(new ArrayMeta(['foo'])));
$this->assertEquals('{"0":"foo"}', json_encode(Meta::fromArray(['foo'])));
}
}
3 changes: 1 addition & 2 deletions test/Document/Resource/Relationship/RelationshipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace JsonApiPhp\JsonApi\Test\Document\Resource\Relationship;

use JsonApiPhp\JsonApi\Document\ArrayMeta;
use JsonApiPhp\JsonApi\Document\Meta;
use JsonApiPhp\JsonApi\Document\Resource\Relationship\Linkage;
use JsonApiPhp\JsonApi\Document\Resource\Relationship\Relationship;
Expand Down Expand Up @@ -91,7 +90,7 @@ public function testCanCreateFromMeta()
'a' => 'b',
],
],
Relationship::fromMeta(new ArrayMeta(['a' => 'b']))
Relationship::fromMeta(Meta::fromArray(['a' => 'b']))
);
}
}
8 changes: 4 additions & 4 deletions test/Document/Resource/ResourceFieldsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace JsonApiPhp\JsonApi\Test\Document\Resource;

use JsonApiPhp\JsonApi\Document\ArrayMeta;
use JsonApiPhp\JsonApi\Document\Meta;
use JsonApiPhp\JsonApi\Document\Resource\Relationship\Relationship;
use JsonApiPhp\JsonApi\Document\Resource\ResourceObject;
use PHPUnit\Framework\TestCase;
Expand All @@ -37,7 +37,7 @@ public function testCanNotSetRelationshipIfAttributeExists()
{
$res = new ResourceObject('books', '1');
$res->setAttribute('foo', 'bar');
$res->setRelationship('foo', Relationship::fromMeta(new ArrayMeta(['a' => 'b'])));
$res->setRelationship('foo', Relationship::fromMeta(Meta::fromArray(['a' => 'b'])));
}

/**
Expand All @@ -47,7 +47,7 @@ public function testCanNotSetRelationshipIfAttributeExists()
public function testCanNotSetAttributeIfRelationshipExists()
{
$res = new ResourceObject('books', '1');
$res->setRelationship('foo', Relationship::fromMeta(new ArrayMeta(['a' => 'b'])));
$res->setRelationship('foo', Relationship::fromMeta(Meta::fromArray(['a' => 'b'])));
$res->setAttribute('foo', 'bar');
}

Expand All @@ -72,7 +72,7 @@ public function testAttributeCanNotHaveReservedNames(string $name)
public function testRelationshipCanNotHaveReservedNames(string $name)
{
$res = new ResourceObject('books', 'abc');
$res->setRelationship($name, Relationship::fromMeta(new ArrayMeta(['a' => 'b'])));
$res->setRelationship($name, Relationship::fromMeta(Meta::fromArray(['a' => 'b'])));
}

public function invalidAttributeNames(): array
Expand Down
8 changes: 4 additions & 4 deletions test/Document/Resource/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace JsonApiPhp\JsonApi\Test\Document\Resource;

use JsonApiPhp\JsonApi\Document\ArrayMeta;
use JsonApiPhp\JsonApi\Document\Meta;
use JsonApiPhp\JsonApi\Document\Resource\Relationship\Relationship;
use JsonApiPhp\JsonApi\Document\Resource\ResourceIdentifier;
use JsonApiPhp\JsonApi\Document\Resource\ResourceObject;
Expand Down Expand Up @@ -58,7 +58,7 @@ public function resourceProvider()
'foo' => 'bar',
],
],
new ResourceIdentifier('books', '42abc', new ArrayMeta(['foo' => 'bar'])),
new ResourceIdentifier('books', '42abc', Meta::fromArray(['foo' => 'bar'])),
],
[
[
Expand All @@ -83,10 +83,10 @@ public function resourceProvider()
],
(function () {
$resource = new ResourceObject('books', '42abc');
$resource->setMeta(new ArrayMeta(['foo' => 'bar']));
$resource->setMeta(Meta::fromArray(['foo' => 'bar']));
$resource->setAttribute('attr', 'val');
$resource->setLink('self', 'http://localhost');
$resource->setRelationship('author', Relationship::fromMeta(new ArrayMeta(['a' => 'b'])));
$resource->setRelationship('author', Relationship::fromMeta(Meta::fromArray(['a' => 'b'])));
return $resource;
})(),
],
Expand Down
6 changes: 3 additions & 3 deletions test/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public function testFromTheReadmeFile()
"id": "9"
},
"links": {
"self": "\/articles\/1\/relationships\/author",
"related": "\/articles\/1\/author"
"self": "/articles/1/relationships/author",
"related": "/articles/1/author"
}
}
}
Expand All @@ -60,7 +60,7 @@ public function testFromTheReadmeFile()

$this->assertEquals(
$json,
json_encode($doc, JSON_PRETTY_PRINT)
json_encode($doc, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
);
}
}

0 comments on commit 8b98f7d

Please sign in to comment.