diff --git a/examples/compound_doc.php b/examples/compound_doc.php index 61842ef..ddb6fd9 100644 --- a/examples/compound_doc.php +++ b/examples/compound_doc.php @@ -7,8 +7,9 @@ use JsonApiPhp\JsonApi\Link\NextLink; use JsonApiPhp\JsonApi\Link\RelatedLink; use JsonApiPhp\JsonApi\Link\SelfLink; -use JsonApiPhp\JsonApi\PaginatedResourceCollection; +use JsonApiPhp\JsonApi\PaginatedCollection; use JsonApiPhp\JsonApi\Pagination; +use JsonApiPhp\JsonApi\ResourceCollection; use JsonApiPhp\JsonApi\ResourceIdentifier; use JsonApiPhp\JsonApi\ResourceIdentifierCollection; use JsonApiPhp\JsonApi\ResourceObject; @@ -39,34 +40,36 @@ '12', new Attribute('body', 'I like XML better'), new SelfLink('http://example.com/comments/12'), - new ToOne('author', $dan->toIdentifier()) + new ToOne('author', $dan->identifier()) ); $document = new CompoundDocument( - new PaginatedResourceCollection( + new PaginatedCollection( new Pagination( new NextLink('http://example.com/articles?page[offset]=2'), new LastLink('http://example.com/articles?page[offset]=10') ), - new ResourceObject( - 'articles', - '1', - new Attribute('title', 'JSON API paints my bikeshed!'), - new SelfLink('http://example.com/articles/1'), - new ToOne( - 'author', - $dan->toIdentifier(), - new SelfLink('http://example.com/articles/1/relationships/author'), - new RelatedLink('http://example.com/articles/1/author') - ), - new ToMany( - 'comments', - new ResourceIdentifierCollection( - $comment05->toIdentifier(), - $comment12->toIdentifier() + new ResourceCollection( + new ResourceObject( + 'articles', + '1', + new Attribute('title', 'JSON API paints my bikeshed!'), + new SelfLink('http://example.com/articles/1'), + new ToOne( + 'author', + $dan->identifier(), + new SelfLink('http://example.com/articles/1/relationships/author'), + new RelatedLink('http://example.com/articles/1/author') ), - new SelfLink('http://example.com/articles/1/relationships/comments'), - new RelatedLink('http://example.com/articles/1/comments') + new ToMany( + 'comments', + new ResourceIdentifierCollection( + $comment05->identifier(), + $comment12->identifier() + ), + new SelfLink('http://example.com/articles/1/relationships/comments'), + new RelatedLink('http://example.com/articles/1/comments') + ) ) ) ), diff --git a/src/Attribute.php b/src/Attribute.php index fb2984f..69c6621 100644 --- a/src/Attribute.php +++ b/src/Attribute.php @@ -5,6 +5,9 @@ use JsonApiPhp\JsonApi\Internal\ResourceField; use JsonApiPhp\JsonApi\Internal\ResourceFieldTrait; +/** + * @see http://jsonapi.org/format/#document-resource-object-attributes + */ final class Attribute implements ResourceField { use ResourceFieldTrait; @@ -17,7 +20,7 @@ public function __construct(string $name, $val) $this->val = $val; } - public function attachTo(object $o) + public function attachTo(object $o): void { child($o, 'attributes')->{$this->name} = $this->val; } diff --git a/src/CompoundDocument.php b/src/CompoundDocument.php index 05bc119..7e84887 100644 --- a/src/CompoundDocument.php +++ b/src/CompoundDocument.php @@ -5,6 +5,10 @@ use JsonApiPhp\JsonApi\Internal\DataDocumentMember; use JsonApiPhp\JsonApi\Internal\PrimaryData; +/** + * A Document with the "included" member + * @see http://jsonapi.org/format/#document-compound-documents + */ final class CompoundDocument implements \JsonSerializable { private $doc; diff --git a/src/DataDocument.php b/src/DataDocument.php index 68fa2cd..d8b986d 100644 --- a/src/DataDocument.php +++ b/src/DataDocument.php @@ -5,6 +5,10 @@ use JsonApiPhp\JsonApi\Internal\DataDocumentMember; use JsonApiPhp\JsonApi\Internal\PrimaryData; +/** + * A Document containing the "data" member + * @see http://jsonapi.org/format/#document-top-level + */ final class DataDocument implements \JsonSerializable { private $value; diff --git a/src/Error.php b/src/Error.php index 65b3071..65a012d 100644 --- a/src/Error.php +++ b/src/Error.php @@ -5,6 +5,10 @@ use JsonApiPhp\JsonApi\Internal\ErrorDocumentMember; use JsonApiPhp\JsonApi\Internal\ErrorMember; +/** + * An Error Object + * @see + */ final class Error implements ErrorDocumentMember { private $error; @@ -17,7 +21,7 @@ public function __construct(ErrorMember ...$members) } } - public function attachTo(object $o) + public function attachTo(object $o): void { $o->errors[] = $this->error; } diff --git a/src/ErrorDocument.php b/src/ErrorDocument.php index 382d5a6..e4a52e5 100644 --- a/src/ErrorDocument.php +++ b/src/ErrorDocument.php @@ -4,21 +4,25 @@ use JsonApiPhp\JsonApi\Internal\ErrorDocumentMember; +/** + * A Document containing an array of Error objects + * @see http://jsonapi.org/format/#document-top-level + */ final class ErrorDocument implements \JsonSerializable { - private $doc; + private $obj; public function __construct(Error $error, ErrorDocumentMember ...$members) { - $this->doc = (object) []; - $error->attachTo($this->doc); + $this->obj = (object) []; + $error->attachTo($this->obj); foreach ($members as $member) { - $member->attachTo($this->doc); + $member->attachTo($this->obj); } } public function jsonSerialize() { - return $this->doc; + return $this->obj; } } diff --git a/src/Included.php b/src/Included.php index 30da0c0..04c8d1c 100644 --- a/src/Included.php +++ b/src/Included.php @@ -3,7 +3,6 @@ namespace JsonApiPhp\JsonApi; use JsonApiPhp\JsonApi\Internal\Attachable; -use JsonApiPhp\JsonApi\Internal\IdentifierRegistry; use JsonApiPhp\JsonApi\Internal\PrimaryData; final class Included implements Attachable @@ -13,34 +12,33 @@ final class Included implements Attachable */ private $resources = []; - private $ids; + private $identifiers = []; public function __construct(ResourceObject ...$resources) { - $this->ids = new IdentifierRegistry(); foreach ($resources as $resource) { - $string_id = $resource->key(); - if (isset($this->resources[$string_id])) { - throw new \LogicException("Resource $string_id is already included"); + $key = $resource->key(); + if (isset($this->resources[$key])) { + throw new \LogicException("Resource $resource is already included"); } - $this->resources[$string_id] = $resource; - $resource->registerIn($this->ids); + $this->resources[$key] = $resource; + $resource->registerIn($this->identifiers); } } public function validateLinkage(PrimaryData $data): void { - $dataRegistry = new IdentifierRegistry(); - $data->registerIn($dataRegistry); + $registry = []; + $data->registerIn($registry); foreach ($this->resources as $resource) { - if ($dataRegistry->has($resource->key()) || $this->ids->has($resource->key())) { + if (isset($registry[$resource->key()]) || isset($this->identifiers[$resource->key()])) { continue; } - throw new \LogicException('Full linkage required for '.$resource->key()); + throw new \LogicException('Full linkage required for '.$resource); } } - public function attachTo(object $o) + public function attachTo(object $o): void { foreach ($this->resources as $resource) { $resource->attachAsIncludedTo($o); diff --git a/src/Internal/Attachable.php b/src/Internal/Attachable.php index fbbe24c..cccd352 100644 --- a/src/Internal/Attachable.php +++ b/src/Internal/Attachable.php @@ -7,5 +7,5 @@ */ interface Attachable { - public function attachTo(object $o); + public function attachTo(object $o): void; } diff --git a/src/Internal/Collection.php b/src/Internal/Collection.php new file mode 100644 index 0000000..e0e73d0 --- /dev/null +++ b/src/Internal/Collection.php @@ -0,0 +1,7 @@ +ids[$id] = true; - } - - public function has(string $id): bool - { - return isset($this->ids[$id]); - } - - public function merge(IdentifierRegistry $registry) - { - $this->ids = array_merge($this->ids, $registry->ids); - } -} diff --git a/src/Internal/IdentityTrait.php b/src/Internal/IdentityTrait.php deleted file mode 100644 index b46ecd4..0000000 --- a/src/Internal/IdentityTrait.php +++ /dev/null @@ -1,29 +0,0 @@ -type}:{$this->id}"; - } -} diff --git a/src/Internal/LinkTrait.php b/src/Internal/LinkTrait.php index 9d957bb..006c61a 100644 --- a/src/Internal/LinkTrait.php +++ b/src/Internal/LinkTrait.php @@ -1,5 +1,4 @@ -jsonapi = (object) [ + $this->obj = (object) [ 'version' => $version, ]; if ($meta) { - $meta->attachTo($this->jsonapi); + $meta->attachTo($this->obj); } } - public function attachTo(object $o) + public function attachTo(object $o): void { - $o->jsonapi = $this->jsonapi; + $o->jsonapi = $this->obj; } } diff --git a/src/Link/AboutLink.php b/src/Link/AboutLink.php index 3da5849..31fe133 100644 --- a/src/Link/AboutLink.php +++ b/src/Link/AboutLink.php @@ -10,7 +10,7 @@ final class AboutLink implements ErrorMember { use LinkTrait; - public function attachTo(object $o) + public function attachTo(object $o): void { child($o, 'links')->about = $this->link; } diff --git a/src/Link/FirstLink.php b/src/Link/FirstLink.php index 12b10ed..f41ab3c 100644 --- a/src/Link/FirstLink.php +++ b/src/Link/FirstLink.php @@ -10,7 +10,7 @@ final class FirstLink implements PaginationLink { use LinkTrait; - public function attachTo(object $o) + public function attachTo(object $o): void { child($o, 'links')->first = $this->link; } diff --git a/src/Link/LastLink.php b/src/Link/LastLink.php index b745f4a..b174cb2 100644 --- a/src/Link/LastLink.php +++ b/src/Link/LastLink.php @@ -10,7 +10,7 @@ final class LastLink implements PaginationLink { use LinkTrait; - public function attachTo(object $o) + public function attachTo(object $o): void { child($o, 'links')->last = $this->link; } diff --git a/src/Link/NextLink.php b/src/Link/NextLink.php index 1c60fb9..e1cce27 100644 --- a/src/Link/NextLink.php +++ b/src/Link/NextLink.php @@ -10,7 +10,7 @@ final class NextLink implements PaginationLink { use LinkTrait; - public function attachTo(object $o) + public function attachTo(object $o): void { child($o, 'links')->next = $this->link; } diff --git a/src/Link/PrevLink.php b/src/Link/PrevLink.php index bbc0b6b..99a61c3 100644 --- a/src/Link/PrevLink.php +++ b/src/Link/PrevLink.php @@ -10,7 +10,7 @@ final class PrevLink implements PaginationLink { use LinkTrait; - public function attachTo(object $o) + public function attachTo(object $o): void { child($o, 'links')->prev = $this->link; } diff --git a/src/Link/RelatedLink.php b/src/Link/RelatedLink.php index ca80378..c0005ff 100644 --- a/src/Link/RelatedLink.php +++ b/src/Link/RelatedLink.php @@ -3,14 +3,14 @@ namespace JsonApiPhp\JsonApi\Link; use JsonApiPhp\JsonApi\Internal\LinkTrait; -use JsonApiPhp\JsonApi\Internal\RelationshipMember; +use JsonApiPhp\JsonApi\Internal\ToOneMember; use function JsonApiPhp\JsonApi\child; -final class RelatedLink implements RelationshipMember +final class RelatedLink implements ToOneMember { use LinkTrait; - public function attachTo(object $o) + public function attachTo(object $o): void { child($o, 'links')->related = $this->link; } diff --git a/src/Link/SelfLink.php b/src/Link/SelfLink.php index 3817dd3..17d96cd 100644 --- a/src/Link/SelfLink.php +++ b/src/Link/SelfLink.php @@ -4,11 +4,11 @@ use JsonApiPhp\JsonApi\Internal\DataDocumentMember; use JsonApiPhp\JsonApi\Internal\LinkTrait; -use JsonApiPhp\JsonApi\Internal\RelationshipMember; use JsonApiPhp\JsonApi\Internal\ResourceMember; +use JsonApiPhp\JsonApi\Internal\ToOneMember; use function JsonApiPhp\JsonApi\child; -final class SelfLink implements DataDocumentMember, ResourceMember, RelationshipMember +final class SelfLink implements DataDocumentMember, ResourceMember, ToOneMember { use LinkTrait; diff --git a/src/Meta.php b/src/Meta.php index b8252cb..e186648 100644 --- a/src/Meta.php +++ b/src/Meta.php @@ -6,10 +6,10 @@ use JsonApiPhp\JsonApi\Internal\ErrorDocumentMember; use JsonApiPhp\JsonApi\Internal\ErrorMember; use JsonApiPhp\JsonApi\Internal\MetaDocumentMember; -use JsonApiPhp\JsonApi\Internal\RelationshipMember; use JsonApiPhp\JsonApi\Internal\ResourceMember; +use JsonApiPhp\JsonApi\Internal\ToOneMember; -final class Meta implements ErrorMember, ErrorDocumentMember, MetaDocumentMember, DataDocumentMember, ResourceMember, RelationshipMember +final class Meta implements ErrorMember, ErrorDocumentMember, MetaDocumentMember, DataDocumentMember, ResourceMember, ToOneMember { /** * @var string @@ -26,7 +26,7 @@ public function __construct(string $key, $value) $this->value = $value; } - public function attachTo(object $o) + public function attachTo(object $o): void { child($o, 'meta')->{$this->key} = $this->value; } diff --git a/src/NullData.php b/src/NullData.php index 768102d..26dd8a5 100644 --- a/src/NullData.php +++ b/src/NullData.php @@ -2,17 +2,16 @@ namespace JsonApiPhp\JsonApi; -use JsonApiPhp\JsonApi\Internal\IdentifierRegistry; use JsonApiPhp\JsonApi\Internal\PrimaryData; final class NullData implements PrimaryData { - public function attachTo(object $o) + public function attachTo(object $o): void { $o->data = null; } - public function registerIn(IdentifierRegistry $registry) + public function registerIn(array &$registry): void { } } diff --git a/src/PaginatedCollection.php b/src/PaginatedCollection.php new file mode 100644 index 0000000..8b8e0bd --- /dev/null +++ b/src/PaginatedCollection.php @@ -0,0 +1,39 @@ +pagination = $pagination; + $this->collection = $collection; + } + + public function attachTo(object $o): void + { + $this->collection->attachTo($o); + $this->pagination->attachTo($o); + } + + /** + * @internal + * @param array $registry + */ + public function registerIn(array &$registry): void + { + $this->collection->registerIn($registry); + } +} diff --git a/src/PaginatedResourceCollection.php b/src/PaginatedResourceCollection.php deleted file mode 100644 index cb2a633..0000000 --- a/src/PaginatedResourceCollection.php +++ /dev/null @@ -1,24 +0,0 @@ -pagination = $pagination; - } - - public function attachTo(object $o): void - { - parent::attachTo($o); - $this->pagination->attachTo($o); - } -} diff --git a/src/PaginatedResourceIdentifierCollection.php b/src/PaginatedResourceIdentifierCollection.php deleted file mode 100644 index ea4e432..0000000 --- a/src/PaginatedResourceIdentifierCollection.php +++ /dev/null @@ -1,24 +0,0 @@ -pagination = $pagination; - } - - public function attachTo(object $o) - { - parent::attachTo($o); - $this->pagination->attachTo($o); - } -} diff --git a/src/Pagination.php b/src/Pagination.php index ba8ab41..aa4f9c3 100644 --- a/src/Pagination.php +++ b/src/Pagination.php @@ -3,10 +3,10 @@ namespace JsonApiPhp\JsonApi; -use JsonApiPhp\JsonApi\Internal\Attachable; use JsonApiPhp\JsonApi\Internal\PaginationLink; +use JsonApiPhp\JsonApi\Internal\ToManyMember; -class Pagination implements Attachable +class Pagination implements ToManyMember { /** * @var PaginationLink[] @@ -18,7 +18,7 @@ public function __construct(PaginationLink ...$links) $this->links = $links; } - public function attachTo(object $o) + public function attachTo(object $o): void { foreach ($this->links as $link) { $link->attachTo($o); diff --git a/src/ResourceCollection.php b/src/ResourceCollection.php index e29ee55..77ac09f 100644 --- a/src/ResourceCollection.php +++ b/src/ResourceCollection.php @@ -2,10 +2,10 @@ namespace JsonApiPhp\JsonApi; -use JsonApiPhp\JsonApi\Internal\IdentifierRegistry; +use JsonApiPhp\JsonApi\Internal\Collection; use JsonApiPhp\JsonApi\Internal\PrimaryData; -class ResourceCollection implements PrimaryData +class ResourceCollection implements PrimaryData, Collection { /** * @var ResourceObject[] @@ -17,7 +17,7 @@ public function __construct(ResourceObject ...$resources) $this->resources = $resources; } - public function attachTo(object $o) + public function attachTo(object $o): void { $o->data = []; foreach ($this->resources as $resource) { @@ -25,7 +25,7 @@ public function attachTo(object $o) } } - public function registerIn(IdentifierRegistry $registry) + public function registerIn(array &$registry): void { foreach ($this->resources as $resource) { $resource->registerIn($registry); diff --git a/src/ResourceIdentifier.php b/src/ResourceIdentifier.php index 7d170b0..221e096 100644 --- a/src/ResourceIdentifier.php +++ b/src/ResourceIdentifier.php @@ -2,14 +2,19 @@ namespace JsonApiPhp\JsonApi; -use JsonApiPhp\JsonApi\Internal\IdentifierRegistry; -use JsonApiPhp\JsonApi\Internal\IdentityTrait; use JsonApiPhp\JsonApi\Internal\PrimaryData; final class ResourceIdentifier implements PrimaryData { - use IdentityTrait; private $obj; + /** + * @var string + */ + private $type; + /** + * @var string + */ + private $id; public function __construct(string $type, string $id, Meta $meta = null) { @@ -28,7 +33,7 @@ public function __construct(string $type, string $id, Meta $meta = null) $this->id = $id; } - public function attachTo(object $o) + public function attachTo(object $o): void { $o->data = $this->obj; } @@ -38,8 +43,8 @@ public function attachToCollection(object $o): void $o->data[] = $this->obj; } - public function registerIn(IdentifierRegistry $registry) + public function registerIn(array &$registry): void { - $registry->add($this->key()); + $registry[compositeKey($this->type, $this->id)] = true; } } diff --git a/src/ResourceIdentifierCollection.php b/src/ResourceIdentifierCollection.php index 1786c1d..d13eb11 100644 --- a/src/ResourceIdentifierCollection.php +++ b/src/ResourceIdentifierCollection.php @@ -2,10 +2,10 @@ namespace JsonApiPhp\JsonApi; -use JsonApiPhp\JsonApi\Internal\IdentifierRegistry; +use JsonApiPhp\JsonApi\Internal\Collection; use JsonApiPhp\JsonApi\Internal\PrimaryData; -class ResourceIdentifierCollection implements PrimaryData +class ResourceIdentifierCollection implements PrimaryData, Collection { /** * @var ResourceIdentifier[] @@ -17,7 +17,7 @@ public function __construct(ResourceIdentifier ...$identifiers) $this->identifiers = $identifiers; } - public function attachTo(object $o) + public function attachTo(object $o): void { $o->data = []; foreach ($this->identifiers as $identifier) { @@ -25,7 +25,7 @@ public function attachTo(object $o) } } - public function registerIn(IdentifierRegistry $registry) + public function registerIn(array &$registry): void { foreach ($this->identifiers as $identifier) { $identifier->registerIn($registry); diff --git a/src/ResourceObject.php b/src/ResourceObject.php index 02701d2..de0a478 100644 --- a/src/ResourceObject.php +++ b/src/ResourceObject.php @@ -3,26 +3,28 @@ namespace JsonApiPhp\JsonApi; use JsonApiPhp\JsonApi\Internal\Identifier; -use JsonApiPhp\JsonApi\Internal\IdentifierRegistry; -use JsonApiPhp\JsonApi\Internal\IdentityTrait; use JsonApiPhp\JsonApi\Internal\PrimaryData; use JsonApiPhp\JsonApi\Internal\ResourceField; use JsonApiPhp\JsonApi\Internal\ResourceMember; final class ResourceObject implements PrimaryData { - use IdentityTrait; private $obj; - private $registry; + private $registry = []; + /** + * @var string + */ + private $type; + /** + * @var string + */ + private $id; public function __construct(string $type, string $id, ResourceMember ...$members) { if (isValidName($type) === false) { throw new \DomainException("Invalid type value: $type"); } - $this->type = $type; - $this->id = $id; - $this->registry = new IdentifierRegistry(); $this->obj = (object) ['type' => $type, 'id' => $id]; $fields = []; foreach ($members as $member) { @@ -38,19 +40,26 @@ public function __construct(string $type, string $id, ResourceMember ...$members } $member->attachTo($this->obj); } + $this->type = $type; + $this->id = $id; } - public function toIdentifier(): ResourceIdentifier + public function identifier(): ResourceIdentifier { return new ResourceIdentifier($this->type, $this->id); } - public function registerIn(IdentifierRegistry $registry) + public function key(): string + { + return compositeKey($this->type, $this->id); + } + + public function registerIn(array &$registry): void { - $registry->merge($this->registry); + $registry = array_merge($registry, $this->registry); } - public function attachTo(object $o) + public function attachTo(object $o): void { $o->data = $this->obj; } @@ -64,4 +73,9 @@ public function attachToCollection(object $o): void { $o->data[] = $this->obj; } + + public function __toString(): string + { + return $this->key(); + } } diff --git a/src/ToMany.php b/src/ToMany.php index d8b2ba3..94b873b 100644 --- a/src/ToMany.php +++ b/src/ToMany.php @@ -3,16 +3,15 @@ namespace JsonApiPhp\JsonApi; use JsonApiPhp\JsonApi\Internal\Identifier; -use JsonApiPhp\JsonApi\Internal\IdentifierRegistry; -use JsonApiPhp\JsonApi\Internal\RelationshipMember; use JsonApiPhp\JsonApi\Internal\ResourceField; use JsonApiPhp\JsonApi\Internal\ResourceFieldTrait; +use JsonApiPhp\JsonApi\Internal\ToOneMember; final class ToMany implements Identifier, ResourceField { use ResourceFieldTrait; /** - * @var RelationshipMember[] + * @var ToOneMember[] */ private $members; /** @@ -20,7 +19,7 @@ final class ToMany implements Identifier, ResourceField */ private $collection; - public function __construct(string $name, ResourceIdentifierCollection $collection, RelationshipMember ...$members) + public function __construct(string $name, ResourceIdentifierCollection $collection, ToOneMember ...$members) { $this->validateFieldName($name); $this->name = $name; @@ -28,7 +27,7 @@ public function __construct(string $name, ResourceIdentifierCollection $collecti $this->collection = $collection; } - public function attachTo(object $o) + public function attachTo(object $o): void { $rel = child(child($o, 'relationships'), $this->name); $rel->data = []; @@ -38,7 +37,7 @@ public function attachTo(object $o) } } - public function registerIn(IdentifierRegistry $registry) + public function registerIn(array &$registry): void { $this->collection->registerIn($registry); } diff --git a/src/ToNull.php b/src/ToNull.php index c163d67..0efaef0 100644 --- a/src/ToNull.php +++ b/src/ToNull.php @@ -2,26 +2,26 @@ namespace JsonApiPhp\JsonApi; -use JsonApiPhp\JsonApi\Internal\RelationshipMember; use JsonApiPhp\JsonApi\Internal\ResourceField; use JsonApiPhp\JsonApi\Internal\ResourceFieldTrait; +use JsonApiPhp\JsonApi\Internal\ToOneMember; final class ToNull implements ResourceField { use ResourceFieldTrait; /** - * @var RelationshipMember[] + * @var ToOneMember[] */ private $members; - public function __construct(string $name, RelationshipMember ...$members) + public function __construct(string $name, ToOneMember ...$members) { $this->validateFieldName($name); $this->name = $name; $this->members = $members; } - public function attachTo(object $o) + public function attachTo(object $o): void { $obj = combine(...$this->members); $obj->data = null; diff --git a/src/ToOne.php b/src/ToOne.php index b696710..c6d32da 100644 --- a/src/ToOne.php +++ b/src/ToOne.php @@ -3,10 +3,9 @@ namespace JsonApiPhp\JsonApi; use JsonApiPhp\JsonApi\Internal\Identifier; -use JsonApiPhp\JsonApi\Internal\IdentifierRegistry; -use JsonApiPhp\JsonApi\Internal\RelationshipMember; use JsonApiPhp\JsonApi\Internal\ResourceField; use JsonApiPhp\JsonApi\Internal\ResourceFieldTrait; +use JsonApiPhp\JsonApi\Internal\ToOneMember; final class ToOne implements Identifier, ResourceField { @@ -19,7 +18,7 @@ final class ToOne implements Identifier, ResourceField private $obj; - public function __construct(string $name, ResourceIdentifier $identifier, RelationshipMember ...$members) + public function __construct(string $name, ResourceIdentifier $identifier, ToOneMember ...$members) { $this->validateFieldName($name); $this->name = $name; @@ -27,12 +26,12 @@ public function __construct(string $name, ResourceIdentifier $identifier, Relati $this->identifier = $identifier; } - public function attachTo(object $o) + public function attachTo(object $o): void { child($o, 'relationships')->{$this->name} = $this->obj; } - public function registerIn(IdentifierRegistry $registry) + public function registerIn(array &$registry): void { $this->identifier->registerIn($registry); } diff --git a/src/functions.php b/src/functions.php index 458cb53..d7c89a4 100644 --- a/src/functions.php +++ b/src/functions.php @@ -25,3 +25,8 @@ function isValidName(string $name): bool { return preg_match('/^(?=[^-_ ])[a-zA-Z0-9\x{0080}-\x{FFFF}-_ ]*(?<=[^-_ ])$/u', $name) === 1; } + +function compositeKey(string $type, string $id): string +{ + return "{$type}:{$id}"; +} diff --git a/test/CompoundDocumentTest.php b/test/CompoundDocumentTest.php index 01ede9c..3d62ff9 100644 --- a/test/CompoundDocumentTest.php +++ b/test/CompoundDocumentTest.php @@ -10,7 +10,7 @@ use JsonApiPhp\JsonApi\Link\RelatedLink; use JsonApiPhp\JsonApi\Link\SelfLink; use JsonApiPhp\JsonApi\NullData; -use JsonApiPhp\JsonApi\PaginatedResourceCollection; +use JsonApiPhp\JsonApi\PaginatedCollection; use JsonApiPhp\JsonApi\Pagination; use JsonApiPhp\JsonApi\ResourceCollection; use JsonApiPhp\JsonApi\ResourceIdentifier; @@ -45,34 +45,36 @@ public function testOfficialDocsExample() '12', new Attribute('body', 'I like XML better'), new SelfLink('http://example.com/comments/12'), - new ToOne('author', $dan->toIdentifier()) + new ToOne('author', $dan->identifier()) ); $document = new CompoundDocument( - new PaginatedResourceCollection( + new PaginatedCollection( new Pagination( new NextLink('http://example.com/articles?page[offset]=2'), new LastLink('http://example.com/articles?page[offset]=10') ), - new ResourceObject( - 'articles', - '1', - new Attribute('title', 'JSON API paints my bikeshed!'), - new SelfLink('http://example.com/articles/1'), - new ToOne( - 'author', - $dan->toIdentifier(), - new SelfLink('http://example.com/articles/1/relationships/author'), - new RelatedLink('http://example.com/articles/1/author') - ), - new ToMany( - 'comments', - new ResourceIdentifierCollection( - $comment05->toIdentifier(), - $comment12->toIdentifier() + new ResourceCollection( + new ResourceObject( + 'articles', + '1', + new Attribute('title', 'JSON API paints my bikeshed!'), + new SelfLink('http://example.com/articles/1'), + new ToOne( + 'author', + $dan->identifier(), + new SelfLink('http://example.com/articles/1/relationships/author'), + new RelatedLink('http://example.com/articles/1/author') ), - new SelfLink('http://example.com/articles/1/relationships/comments'), - new RelatedLink('http://example.com/articles/1/comments') + new ToMany( + 'comments', + new ResourceIdentifierCollection( + $comment05->identifier(), + $comment12->identifier() + ), + new SelfLink('http://example.com/articles/1/relationships/comments'), + new RelatedLink('http://example.com/articles/1/comments') + ) ) ) ), @@ -225,7 +227,7 @@ public function testIncludedResourceMayBeIdentifiedByLinkageInPrimaryData() $article = new ResourceObject( 'articles', '1', - new ToOne('author', $author->toIdentifier()) + new ToOne('author', $author->identifier()) ); $doc = new CompoundDocument($article, new Included($author)); $this->assertNotEmpty($doc); @@ -238,12 +240,12 @@ public function testIncludedResourceMayBeIdentifiedByAnotherLinkedResource() 'books', '2', new Attribute('name', 'Domain Driven Design'), - new ToOne('author', $writer->toIdentifier()) + new ToOne('author', $writer->identifier()) ); $cart = new ResourceObject( 'shopping-carts', '1', - new ToMany('contents', new ResourceIdentifierCollection($book->toIdentifier())) + new ToMany('contents', new ResourceIdentifierCollection($book->identifier())) ); $doc = new CompoundDocument($cart, new Included($book, $writer)); $this->assertNotEmpty($doc); @@ -257,6 +259,6 @@ public function testIncludedResourceMayBeIdentifiedByAnotherLinkedResource() public function testCanNotBeManyIncludedResourcesWithEqualIdentifiers() { $apple = new ResourceObject('apples', '1'); - new CompoundDocument($apple->toIdentifier(), new Included($apple, $apple)); + new CompoundDocument($apple->identifier(), new Included($apple, $apple)); } } diff --git a/test/PaginationTest.php b/test/PaginationTest.php index e00ab53..17ee668 100644 --- a/test/PaginationTest.php +++ b/test/PaginationTest.php @@ -10,10 +10,11 @@ use JsonApiPhp\JsonApi\Link\LastLink; use JsonApiPhp\JsonApi\Link\NextLink; use JsonApiPhp\JsonApi\Link\PrevLink; -use JsonApiPhp\JsonApi\PaginatedResourceCollection; -use JsonApiPhp\JsonApi\PaginatedResourceIdentifierCollection; +use JsonApiPhp\JsonApi\PaginatedCollection; use JsonApiPhp\JsonApi\Pagination; +use JsonApiPhp\JsonApi\ResourceCollection; use JsonApiPhp\JsonApi\ResourceIdentifier; +use JsonApiPhp\JsonApi\ResourceIdentifierCollection; use JsonApiPhp\JsonApi\ResourceObject; use JsonApiPhp\JsonApi\ToMany; @@ -37,15 +38,17 @@ public function testPaginatedResourceCollection() } ', new DataDocument( - new PaginatedResourceCollection( + new PaginatedCollection( new Pagination( new FirstLink('http://example.com/fruits?page=first'), new PrevLink('http://example.com/fruits?page=3'), new NextLink('http://example.com/fruits?page=5'), new LastLink('http://example.com/fruits?page=last') ), - new ResourceObject('apples', '1'), - new ResourceObject('apples', '2') + new ResourceCollection( + new ResourceObject('apples', '1'), + new ResourceObject('apples', '2') + ) ) ) ); @@ -86,15 +89,15 @@ public function testPaginatedResourceIdentifierCollection() '1', new ToMany( 'fruits', - new PaginatedResourceIdentifierCollection( - new Pagination( - new FirstLink('http://example.com/basket/1/fruits?page=first'), - new PrevLink('http://example.com/basket/1/fruits?page=3'), - new NextLink('http://example.com/basket/1/fruits?page=5'), - new LastLink('http://example.com/basket/1/fruits?page=last') - ), + new ResourceIdentifierCollection( new ResourceIdentifier('apples', '1'), new ResourceIdentifier('apples', '2') + ), + new Pagination( + new FirstLink('http://example.com/basket/1/fruits?page=first'), + new PrevLink('http://example.com/basket/1/fruits?page=3'), + new NextLink('http://example.com/basket/1/fruits?page=5'), + new LastLink('http://example.com/basket/1/fruits?page=last') ) ) ), diff --git a/test/benchmarks/compound10k.php b/test/benchmarks/compound10k.php index 8f2fad4..c1a0ae2 100644 --- a/test/benchmarks/compound10k.php +++ b/test/benchmarks/compound10k.php @@ -19,8 +19,9 @@ use JsonApiPhp\JsonApi\Link\RelatedLink; use JsonApiPhp\JsonApi\Link\SelfLink; use JsonApiPhp\JsonApi\Meta; -use JsonApiPhp\JsonApi\PaginatedResourceCollection; +use JsonApiPhp\JsonApi\PaginatedCollection; use JsonApiPhp\JsonApi\Pagination; +use JsonApiPhp\JsonApi\ResourceCollection; use JsonApiPhp\JsonApi\ResourceIdentifier; use JsonApiPhp\JsonApi\ResourceIdentifierCollection; use JsonApiPhp\JsonApi\ResourceObject; @@ -54,34 +55,36 @@ '12', new Attribute('body', 'I like XML better'), new SelfLink('http://example.com/comments/12'), - new ToOne('author', $dan->toIdentifier()) + new ToOne('author', $dan->identifier()) ); - $data_document = new CompoundDocument( - new PaginatedResourceCollection( + $document = new CompoundDocument( + new PaginatedCollection( new Pagination( new NextLink('http://example.com/articles?page[offset]=2'), new LastLink('http://example.com/articles?page[offset]=10') ), - new ResourceObject( - 'articles', - '1', - new Attribute('title', 'JSON API paints my bikeshed!'), - new SelfLink('http://example.com/articles/1'), - new ToOne( - 'author', - $dan->toIdentifier(), - new SelfLink('http://example.com/articles/1/relationships/author'), - new RelatedLink('http://example.com/articles/1/author') - ), - new ToMany( - 'comments', - new ResourceIdentifierCollection( - $comment05->toIdentifier(), - $comment12->toIdentifier() + new ResourceCollection( + new ResourceObject( + 'articles', + '1', + new Attribute('title', 'JSON API paints my bikeshed!'), + new SelfLink('http://example.com/articles/1'), + new ToOne( + 'author', + $dan->identifier(), + new SelfLink('http://example.com/articles/1/relationships/author'), + new RelatedLink('http://example.com/articles/1/author') ), - new SelfLink('http://example.com/articles/1/relationships/comments'), - new RelatedLink('http://example.com/articles/1/comments') + new ToMany( + 'comments', + new ResourceIdentifierCollection( + $comment05->identifier(), + $comment12->identifier() + ), + new SelfLink('http://example.com/articles/1/relationships/comments'), + new RelatedLink('http://example.com/articles/1/comments') + ) ) ) ), @@ -89,11 +92,11 @@ new SelfLink('http://example.com/articles') ); - $data_doc_json = json_encode($data_document); + $data_doc_json = json_encode($document); } for ($count = 0; $count < 1000; $count++) { - $error_doc = new ErrorDocument( + $error = new ErrorDocument( new Error( new Id('1'), new AboutLink('/errors/not_found'), @@ -109,7 +112,7 @@ new JsonApi() ); - $error_doc_json = json_encode($error_doc); + $error_doc_json = json_encode($error); } echo $data_doc_json;