Skip to content

Commit 42d32f3

Browse files
authored
Simplification, Formatting, Performance (#83)
1 parent 0ffb320 commit 42d32f3

40 files changed

+277
-276
lines changed

examples/compound_doc.php

+24-21
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
use JsonApiPhp\JsonApi\Link\NextLink;
88
use JsonApiPhp\JsonApi\Link\RelatedLink;
99
use JsonApiPhp\JsonApi\Link\SelfLink;
10-
use JsonApiPhp\JsonApi\PaginatedResourceCollection;
10+
use JsonApiPhp\JsonApi\PaginatedCollection;
1111
use JsonApiPhp\JsonApi\Pagination;
12+
use JsonApiPhp\JsonApi\ResourceCollection;
1213
use JsonApiPhp\JsonApi\ResourceIdentifier;
1314
use JsonApiPhp\JsonApi\ResourceIdentifierCollection;
1415
use JsonApiPhp\JsonApi\ResourceObject;
@@ -39,34 +40,36 @@
3940
'12',
4041
new Attribute('body', 'I like XML better'),
4142
new SelfLink('http://example.com/comments/12'),
42-
new ToOne('author', $dan->toIdentifier())
43+
new ToOne('author', $dan->identifier())
4344
);
4445

4546
$document = new CompoundDocument(
46-
new PaginatedResourceCollection(
47+
new PaginatedCollection(
4748
new Pagination(
4849
new NextLink('http://example.com/articles?page[offset]=2'),
4950
new LastLink('http://example.com/articles?page[offset]=10')
5051
),
51-
new ResourceObject(
52-
'articles',
53-
'1',
54-
new Attribute('title', 'JSON API paints my bikeshed!'),
55-
new SelfLink('http://example.com/articles/1'),
56-
new ToOne(
57-
'author',
58-
$dan->toIdentifier(),
59-
new SelfLink('http://example.com/articles/1/relationships/author'),
60-
new RelatedLink('http://example.com/articles/1/author')
61-
),
62-
new ToMany(
63-
'comments',
64-
new ResourceIdentifierCollection(
65-
$comment05->toIdentifier(),
66-
$comment12->toIdentifier()
52+
new ResourceCollection(
53+
new ResourceObject(
54+
'articles',
55+
'1',
56+
new Attribute('title', 'JSON API paints my bikeshed!'),
57+
new SelfLink('http://example.com/articles/1'),
58+
new ToOne(
59+
'author',
60+
$dan->identifier(),
61+
new SelfLink('http://example.com/articles/1/relationships/author'),
62+
new RelatedLink('http://example.com/articles/1/author')
6763
),
68-
new SelfLink('http://example.com/articles/1/relationships/comments'),
69-
new RelatedLink('http://example.com/articles/1/comments')
64+
new ToMany(
65+
'comments',
66+
new ResourceIdentifierCollection(
67+
$comment05->identifier(),
68+
$comment12->identifier()
69+
),
70+
new SelfLink('http://example.com/articles/1/relationships/comments'),
71+
new RelatedLink('http://example.com/articles/1/comments')
72+
)
7073
)
7174
)
7275
),

src/Attribute.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
use JsonApiPhp\JsonApi\Internal\ResourceField;
66
use JsonApiPhp\JsonApi\Internal\ResourceFieldTrait;
77

8+
/**
9+
* @see http://jsonapi.org/format/#document-resource-object-attributes
10+
*/
811
final class Attribute implements ResourceField
912
{
1013
use ResourceFieldTrait;
@@ -17,7 +20,7 @@ public function __construct(string $name, $val)
1720
$this->val = $val;
1821
}
1922

20-
public function attachTo(object $o)
23+
public function attachTo(object $o): void
2124
{
2225
child($o, 'attributes')->{$this->name} = $this->val;
2326
}

src/CompoundDocument.php

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
use JsonApiPhp\JsonApi\Internal\DataDocumentMember;
66
use JsonApiPhp\JsonApi\Internal\PrimaryData;
77

8+
/**
9+
* A Document with the "included" member
10+
* @see http://jsonapi.org/format/#document-compound-documents
11+
*/
812
final class CompoundDocument implements \JsonSerializable
913
{
1014
private $doc;

src/DataDocument.php

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
use JsonApiPhp\JsonApi\Internal\DataDocumentMember;
66
use JsonApiPhp\JsonApi\Internal\PrimaryData;
77

8+
/**
9+
* A Document containing the "data" member
10+
* @see http://jsonapi.org/format/#document-top-level
11+
*/
812
final class DataDocument implements \JsonSerializable
913
{
1014
private $value;

src/Error.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
use JsonApiPhp\JsonApi\Internal\ErrorDocumentMember;
66
use JsonApiPhp\JsonApi\Internal\ErrorMember;
77

8+
/**
9+
* An Error Object
10+
* @see
11+
*/
812
final class Error implements ErrorDocumentMember
913
{
1014
private $error;
@@ -17,7 +21,7 @@ public function __construct(ErrorMember ...$members)
1721
}
1822
}
1923

20-
public function attachTo(object $o)
24+
public function attachTo(object $o): void
2125
{
2226
$o->errors[] = $this->error;
2327
}

src/ErrorDocument.php

+9-5
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,25 @@
44

55
use JsonApiPhp\JsonApi\Internal\ErrorDocumentMember;
66

7+
/**
8+
* A Document containing an array of Error objects
9+
* @see http://jsonapi.org/format/#document-top-level
10+
*/
711
final class ErrorDocument implements \JsonSerializable
812
{
9-
private $doc;
13+
private $obj;
1014

1115
public function __construct(Error $error, ErrorDocumentMember ...$members)
1216
{
13-
$this->doc = (object) [];
14-
$error->attachTo($this->doc);
17+
$this->obj = (object) [];
18+
$error->attachTo($this->obj);
1519
foreach ($members as $member) {
16-
$member->attachTo($this->doc);
20+
$member->attachTo($this->obj);
1721
}
1822
}
1923

2024
public function jsonSerialize()
2125
{
22-
return $this->doc;
26+
return $this->obj;
2327
}
2428
}

src/Included.php

+11-13
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace JsonApiPhp\JsonApi;
44

55
use JsonApiPhp\JsonApi\Internal\Attachable;
6-
use JsonApiPhp\JsonApi\Internal\IdentifierRegistry;
76
use JsonApiPhp\JsonApi\Internal\PrimaryData;
87

98
final class Included implements Attachable
@@ -13,34 +12,33 @@ final class Included implements Attachable
1312
*/
1413
private $resources = [];
1514

16-
private $ids;
15+
private $identifiers = [];
1716

1817
public function __construct(ResourceObject ...$resources)
1918
{
20-
$this->ids = new IdentifierRegistry();
2119
foreach ($resources as $resource) {
22-
$string_id = $resource->key();
23-
if (isset($this->resources[$string_id])) {
24-
throw new \LogicException("Resource $string_id is already included");
20+
$key = $resource->key();
21+
if (isset($this->resources[$key])) {
22+
throw new \LogicException("Resource $resource is already included");
2523
}
26-
$this->resources[$string_id] = $resource;
27-
$resource->registerIn($this->ids);
24+
$this->resources[$key] = $resource;
25+
$resource->registerIn($this->identifiers);
2826
}
2927
}
3028

3129
public function validateLinkage(PrimaryData $data): void
3230
{
33-
$dataRegistry = new IdentifierRegistry();
34-
$data->registerIn($dataRegistry);
31+
$registry = [];
32+
$data->registerIn($registry);
3533
foreach ($this->resources as $resource) {
36-
if ($dataRegistry->has($resource->key()) || $this->ids->has($resource->key())) {
34+
if (isset($registry[$resource->key()]) || isset($this->identifiers[$resource->key()])) {
3735
continue;
3836
}
39-
throw new \LogicException('Full linkage required for '.$resource->key());
37+
throw new \LogicException('Full linkage required for '.$resource);
4038
}
4139
}
4240

43-
public function attachTo(object $o)
41+
public function attachTo(object $o): void
4442
{
4543
foreach ($this->resources as $resource) {
4644
$resource->attachAsIncludedTo($o);

src/Internal/Attachable.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
*/
88
interface Attachable
99
{
10-
public function attachTo(object $o);
10+
public function attachTo(object $o): void;
1111
}

src/Internal/Collection.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace JsonApiPhp\JsonApi\Internal;
4+
5+
interface Collection extends PrimaryData
6+
{
7+
}

src/Internal/Identifier.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,9 @@
77
*/
88
interface Identifier
99
{
10-
public function registerIn(IdentifierRegistry $registry);
10+
/**
11+
* @internal
12+
* @param array $registry
13+
*/
14+
public function registerIn(array &$registry): void;
1115
}

src/Internal/IdentifierRegistry.php

-27
This file was deleted.

src/Internal/IdentityTrait.php

-29
This file was deleted.

src/Internal/LinkTrait.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
<?php
2-
declare(strict_types=1);
1+
<?php declare(strict_types=1);
32

43
namespace JsonApiPhp\JsonApi\Internal;
54

src/Internal/ToManyMember.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace JsonApiPhp\JsonApi\Internal;
4+
5+
interface ToManyMember extends ToOneMember
6+
{
7+
}

src/Internal/RelationshipMember.php renamed to src/Internal/ToOneMember.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
/**
66
* @internal
77
*/
8-
interface RelationshipMember extends Attachable
8+
interface ToOneMember extends Attachable
99
{
1010
}

src/JsonApi.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@
88

99
final class JsonApi implements MetaDocumentMember, DataDocumentMember, ErrorDocumentMember
1010
{
11-
private $jsonapi;
11+
private $obj;
1212

1313
public function __construct(string $version = '1.0', Meta $meta = null)
1414
{
15-
$this->jsonapi = (object) [
15+
$this->obj = (object) [
1616
'version' => $version,
1717
];
1818
if ($meta) {
19-
$meta->attachTo($this->jsonapi);
19+
$meta->attachTo($this->obj);
2020
}
2121
}
2222

23-
public function attachTo(object $o)
23+
public function attachTo(object $o): void
2424
{
25-
$o->jsonapi = $this->jsonapi;
25+
$o->jsonapi = $this->obj;
2626
}
2727
}

src/Link/AboutLink.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ final class AboutLink implements ErrorMember
1010
{
1111
use LinkTrait;
1212

13-
public function attachTo(object $o)
13+
public function attachTo(object $o): void
1414
{
1515
child($o, 'links')->about = $this->link;
1616
}

src/Link/FirstLink.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ final class FirstLink implements PaginationLink
1010
{
1111
use LinkTrait;
1212

13-
public function attachTo(object $o)
13+
public function attachTo(object $o): void
1414
{
1515
child($o, 'links')->first = $this->link;
1616
}

src/Link/LastLink.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ final class LastLink implements PaginationLink
1010
{
1111
use LinkTrait;
1212

13-
public function attachTo(object $o)
13+
public function attachTo(object $o): void
1414
{
1515
child($o, 'links')->last = $this->link;
1616
}

src/Link/NextLink.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ final class NextLink implements PaginationLink
1010
{
1111
use LinkTrait;
1212

13-
public function attachTo(object $o)
13+
public function attachTo(object $o): void
1414
{
1515
child($o, 'links')->next = $this->link;
1616
}

src/Link/PrevLink.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ final class PrevLink implements PaginationLink
1010
{
1111
use LinkTrait;
1212

13-
public function attachTo(object $o)
13+
public function attachTo(object $o): void
1414
{
1515
child($o, 'links')->prev = $this->link;
1616
}

0 commit comments

Comments
 (0)