From 4dacff1c586d50fea5533778bce18beac7f49bea Mon Sep 17 00:00:00 2001 From: Alexey Karapetov Date: Fri, 2 Mar 2018 21:26:05 -0800 Subject: [PATCH 1/5] Naming --- src/Attribute.php | 2 +- src/Error.php | 2 +- src/Included.php | 16 ++++++++-------- src/Internal/Attachable.php | 2 +- src/Internal/IdentityTrait.php | 3 +-- src/Internal/LinkTrait.php | 3 +-- src/JsonApi.php | 2 +- src/Link/AboutLink.php | 2 +- src/Link/FirstLink.php | 2 +- src/Link/LastLink.php | 2 +- src/Link/NextLink.php | 2 +- src/Link/PrevLink.php | 2 +- src/Link/RelatedLink.php | 2 +- src/Meta.php | 2 +- src/NullData.php | 2 +- src/PaginatedResourceIdentifierCollection.php | 2 +- src/Pagination.php | 2 +- src/ResourceCollection.php | 2 +- src/ResourceIdentifier.php | 2 +- src/ResourceIdentifierCollection.php | 2 +- src/ResourceObject.php | 2 +- src/ToMany.php | 2 +- src/ToNull.php | 2 +- src/ToOne.php | 2 +- 24 files changed, 31 insertions(+), 33 deletions(-) diff --git a/src/Attribute.php b/src/Attribute.php index fb2984f..f43e9d0 100644 --- a/src/Attribute.php +++ b/src/Attribute.php @@ -17,7 +17,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/Error.php b/src/Error.php index 65b3071..b7733a0 100644 --- a/src/Error.php +++ b/src/Error.php @@ -17,7 +17,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/Included.php b/src/Included.php index 30da0c0..02fbe26 100644 --- a/src/Included.php +++ b/src/Included.php @@ -19,28 +19,28 @@ 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 $key is already included"); } - $this->resources[$string_id] = $resource; + $this->resources[$key] = $resource; $resource->registerIn($this->ids); } } public function validateLinkage(PrimaryData $data): void { - $dataRegistry = new IdentifierRegistry(); - $data->registerIn($dataRegistry); + $registry = new IdentifierRegistry(); + $data->registerIn($registry); foreach ($this->resources as $resource) { - if ($dataRegistry->has($resource->key()) || $this->ids->has($resource->key())) { + if ($registry->has($resource->key()) || $this->ids->has($resource->key())) { continue; } throw new \LogicException('Full linkage required for '.$resource->key()); } } - 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/IdentityTrait.php b/src/Internal/IdentityTrait.php index b46ecd4..4e471d3 100644 --- a/src/Internal/IdentityTrait.php +++ b/src/Internal/IdentityTrait.php @@ -1,5 +1,4 @@ -jsonapi = $this->jsonapi; } 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..ecb85ea 100644 --- a/src/Link/RelatedLink.php +++ b/src/Link/RelatedLink.php @@ -10,7 +10,7 @@ final class RelatedLink implements RelationshipMember { use LinkTrait; - public function attachTo(object $o) + public function attachTo(object $o): void { child($o, 'links')->related = $this->link; } diff --git a/src/Meta.php b/src/Meta.php index b8252cb..8baf4a8 100644 --- a/src/Meta.php +++ b/src/Meta.php @@ -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..7ae2f3f 100644 --- a/src/NullData.php +++ b/src/NullData.php @@ -7,7 +7,7 @@ final class NullData implements PrimaryData { - public function attachTo(object $o) + public function attachTo(object $o): void { $o->data = null; } diff --git a/src/PaginatedResourceIdentifierCollection.php b/src/PaginatedResourceIdentifierCollection.php index ea4e432..9ac0603 100644 --- a/src/PaginatedResourceIdentifierCollection.php +++ b/src/PaginatedResourceIdentifierCollection.php @@ -16,7 +16,7 @@ public function __construct(Pagination $pagination, ResourceIdentifier ...$ident $this->pagination = $pagination; } - public function attachTo(object $o) + public function attachTo(object $o): void { parent::attachTo($o); $this->pagination->attachTo($o); diff --git a/src/Pagination.php b/src/Pagination.php index ba8ab41..5288ffb 100644 --- a/src/Pagination.php +++ b/src/Pagination.php @@ -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..bcf50d1 100644 --- a/src/ResourceCollection.php +++ b/src/ResourceCollection.php @@ -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) { diff --git a/src/ResourceIdentifier.php b/src/ResourceIdentifier.php index 7d170b0..17fd659 100644 --- a/src/ResourceIdentifier.php +++ b/src/ResourceIdentifier.php @@ -28,7 +28,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; } diff --git a/src/ResourceIdentifierCollection.php b/src/ResourceIdentifierCollection.php index 1786c1d..17ac469 100644 --- a/src/ResourceIdentifierCollection.php +++ b/src/ResourceIdentifierCollection.php @@ -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) { diff --git a/src/ResourceObject.php b/src/ResourceObject.php index 02701d2..fba5be4 100644 --- a/src/ResourceObject.php +++ b/src/ResourceObject.php @@ -50,7 +50,7 @@ public function registerIn(IdentifierRegistry $registry) $registry->merge($this->registry); } - public function attachTo(object $o) + public function attachTo(object $o): void { $o->data = $this->obj; } diff --git a/src/ToMany.php b/src/ToMany.php index d8b2ba3..227c8aa 100644 --- a/src/ToMany.php +++ b/src/ToMany.php @@ -28,7 +28,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 = []; diff --git a/src/ToNull.php b/src/ToNull.php index c163d67..9af2079 100644 --- a/src/ToNull.php +++ b/src/ToNull.php @@ -21,7 +21,7 @@ public function __construct(string $name, RelationshipMember ...$members) $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..16184d8 100644 --- a/src/ToOne.php +++ b/src/ToOne.php @@ -27,7 +27,7 @@ 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; } From 422a44722add595c1337e2171bab9f6cb3d48ef7 Mon Sep 17 00:00:00 2001 From: Alexey Karapetov Date: Fri, 2 Mar 2018 22:56:43 -0800 Subject: [PATCH 2/5] Performance improvements --- examples/compound_doc.php | 8 +++---- src/Attribute.php | 3 +++ src/CompoundDocument.php | 4 ++++ src/DataDocument.php | 4 ++++ src/Error.php | 4 ++++ src/ErrorDocument.php | 14 +++++++---- src/Included.php | 14 +++++------ src/Internal/Identifier.php | 6 ++++- src/Internal/IdentifierRegistry.php | 27 --------------------- src/Internal/IdentityTrait.php | 28 ---------------------- src/JsonApi.php | 8 +++---- src/Meta.php | 3 ++- src/NullData.php | 3 +-- src/PaginatedResourceCollection.php | 2 +- src/ResourceCollection.php | 3 +-- src/ResourceIdentifier.php | 16 +++++++++---- src/ResourceIdentifierCollection.php | 3 +-- src/ResourceObject.php | 35 ++++++++++++++++++++-------- src/ToMany.php | 3 +-- src/ToOne.php | 3 +-- src/functions.php | 5 ++++ test/CompoundDocumentTest.php | 16 ++++++------- test/benchmarks/compound10k.php | 8 +++---- 23 files changed, 104 insertions(+), 116 deletions(-) delete mode 100644 src/Internal/IdentifierRegistry.php delete mode 100644 src/Internal/IdentityTrait.php diff --git a/examples/compound_doc.php b/examples/compound_doc.php index 61842ef..e5f6672 100644 --- a/examples/compound_doc.php +++ b/examples/compound_doc.php @@ -39,7 +39,7 @@ '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( @@ -55,15 +55,15 @@ new SelfLink('http://example.com/articles/1'), new ToOne( 'author', - $dan->toIdentifier(), + $dan->identifier(), 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() + $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 f43e9d0..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; 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 b7733a0..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; 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 02fbe26..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,30 +12,29 @@ final class Included implements Attachable */ private $resources = []; - private $ids; + private $identifiers = []; public function __construct(ResourceObject ...$resources) { - $this->ids = new IdentifierRegistry(); foreach ($resources as $resource) { $key = $resource->key(); if (isset($this->resources[$key])) { - throw new \LogicException("Resource $key is already included"); + throw new \LogicException("Resource $resource is already included"); } $this->resources[$key] = $resource; - $resource->registerIn($this->ids); + $resource->registerIn($this->identifiers); } } public function validateLinkage(PrimaryData $data): void { - $registry = new IdentifierRegistry(); + $registry = []; $data->registerIn($registry); foreach ($this->resources as $resource) { - if ($registry->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); } } diff --git a/src/Internal/Identifier.php b/src/Internal/Identifier.php index 01885b9..fa435a1 100644 --- a/src/Internal/Identifier.php +++ b/src/Internal/Identifier.php @@ -7,5 +7,9 @@ */ interface Identifier { - public function registerIn(IdentifierRegistry $registry); + /** + * @internal + * @param array $registry + */ + public function registerIn(array &$registry): void; } diff --git a/src/Internal/IdentifierRegistry.php b/src/Internal/IdentifierRegistry.php deleted file mode 100644 index e79e474..0000000 --- a/src/Internal/IdentifierRegistry.php +++ /dev/null @@ -1,27 +0,0 @@ -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 4e471d3..0000000 --- a/src/Internal/IdentityTrait.php +++ /dev/null @@ -1,28 +0,0 @@ -type}:{$this->id}"; - } -} diff --git a/src/JsonApi.php b/src/JsonApi.php index d81cb9c..c0f2220 100644 --- a/src/JsonApi.php +++ b/src/JsonApi.php @@ -8,20 +8,20 @@ final class JsonApi implements MetaDocumentMember, DataDocumentMember, ErrorDocumentMember { - private $jsonapi; + private $obj; public function __construct(string $version = '1.0', Meta $meta = null) { - $this->jsonapi = (object) [ + $this->obj = (object) [ 'version' => $version, ]; if ($meta) { - $meta->attachTo($this->jsonapi); + $meta->attachTo($this->obj); } } public function attachTo(object $o): void { - $o->jsonapi = $this->jsonapi; + $o->jsonapi = $this->obj; } } diff --git a/src/Meta.php b/src/Meta.php index 8baf4a8..4ac58b0 100644 --- a/src/Meta.php +++ b/src/Meta.php @@ -9,7 +9,8 @@ use JsonApiPhp\JsonApi\Internal\RelationshipMember; use JsonApiPhp\JsonApi\Internal\ResourceMember; -final class Meta implements ErrorMember, ErrorDocumentMember, MetaDocumentMember, DataDocumentMember, ResourceMember, RelationshipMember +final class Meta + implements ErrorMember, ErrorDocumentMember, MetaDocumentMember, DataDocumentMember, ResourceMember, RelationshipMember { /** * @var string diff --git a/src/NullData.php b/src/NullData.php index 7ae2f3f..26dd8a5 100644 --- a/src/NullData.php +++ b/src/NullData.php @@ -2,7 +2,6 @@ namespace JsonApiPhp\JsonApi; -use JsonApiPhp\JsonApi\Internal\IdentifierRegistry; use JsonApiPhp\JsonApi\Internal\PrimaryData; final class NullData implements PrimaryData @@ -12,7 +11,7 @@ public function attachTo(object $o): void $o->data = null; } - public function registerIn(IdentifierRegistry $registry) + public function registerIn(array &$registry): void { } } diff --git a/src/PaginatedResourceCollection.php b/src/PaginatedResourceCollection.php index cb2a633..8dd4350 100644 --- a/src/PaginatedResourceCollection.php +++ b/src/PaginatedResourceCollection.php @@ -3,7 +3,7 @@ namespace JsonApiPhp\JsonApi; -class PaginatedResourceCollection extends ResourceCollection +final class PaginatedResourceCollection extends ResourceCollection { /** * @var Pagination diff --git a/src/ResourceCollection.php b/src/ResourceCollection.php index bcf50d1..c1339f5 100644 --- a/src/ResourceCollection.php +++ b/src/ResourceCollection.php @@ -2,7 +2,6 @@ namespace JsonApiPhp\JsonApi; -use JsonApiPhp\JsonApi\Internal\IdentifierRegistry; use JsonApiPhp\JsonApi\Internal\PrimaryData; class ResourceCollection implements PrimaryData @@ -25,7 +24,7 @@ public function attachTo(object $o): void } } - 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 17fd659..14a768b 100644 --- a/src/ResourceIdentifier.php +++ b/src/ResourceIdentifier.php @@ -2,14 +2,20 @@ namespace JsonApiPhp\JsonApi; -use JsonApiPhp\JsonApi\Internal\IdentifierRegistry; -use JsonApiPhp\JsonApi\Internal\IdentityTrait; +use JsonApiPhp\JsonApi\Internal\Identifiable; 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) { @@ -38,8 +44,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 17ac469..6d03a55 100644 --- a/src/ResourceIdentifierCollection.php +++ b/src/ResourceIdentifierCollection.php @@ -2,7 +2,6 @@ namespace JsonApiPhp\JsonApi; -use JsonApiPhp\JsonApi\Internal\IdentifierRegistry; use JsonApiPhp\JsonApi\Internal\PrimaryData; class ResourceIdentifierCollection implements PrimaryData @@ -25,7 +24,7 @@ public function attachTo(object $o): void } } - 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 fba5be4..900af7a 100644 --- a/src/ResourceObject.php +++ b/src/ResourceObject.php @@ -2,27 +2,30 @@ namespace JsonApiPhp\JsonApi; +use JsonApiPhp\JsonApi\Internal\Identifiable; 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,16 +41,23 @@ 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): void @@ -64,4 +74,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 227c8aa..13a3e0b 100644 --- a/src/ToMany.php +++ b/src/ToMany.php @@ -3,7 +3,6 @@ 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; @@ -38,7 +37,7 @@ public function attachTo(object $o): void } } - public function registerIn(IdentifierRegistry $registry) + public function registerIn(array &$registry): void { $this->collection->registerIn($registry); } diff --git a/src/ToOne.php b/src/ToOne.php index 16184d8..6cfcbaa 100644 --- a/src/ToOne.php +++ b/src/ToOne.php @@ -3,7 +3,6 @@ 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; @@ -32,7 +31,7 @@ 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..4640df2 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}"; +} \ No newline at end of file diff --git a/test/CompoundDocumentTest.php b/test/CompoundDocumentTest.php index 01ede9c..b509b8a 100644 --- a/test/CompoundDocumentTest.php +++ b/test/CompoundDocumentTest.php @@ -45,7 +45,7 @@ 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( @@ -61,15 +61,15 @@ public function testOfficialDocsExample() new SelfLink('http://example.com/articles/1'), new ToOne( 'author', - $dan->toIdentifier(), + $dan->identifier(), 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() + $comment05->identifier(), + $comment12->identifier() ), new SelfLink('http://example.com/articles/1/relationships/comments'), new RelatedLink('http://example.com/articles/1/comments') @@ -225,7 +225,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 +238,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 +257,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/benchmarks/compound10k.php b/test/benchmarks/compound10k.php index 8f2fad4..dc3aac8 100644 --- a/test/benchmarks/compound10k.php +++ b/test/benchmarks/compound10k.php @@ -54,7 +54,7 @@ '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( @@ -70,15 +70,15 @@ new SelfLink('http://example.com/articles/1'), new ToOne( 'author', - $dan->toIdentifier(), + $dan->identifier(), 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() + $comment05->identifier(), + $comment12->identifier() ), new SelfLink('http://example.com/articles/1/relationships/comments'), new RelatedLink('http://example.com/articles/1/comments') From 13e7d61ec3fcf9f81e8d7bca269f876c639990fc Mon Sep 17 00:00:00 2001 From: Alexey Karapetov Date: Fri, 2 Mar 2018 22:57:12 -0800 Subject: [PATCH 3/5] Performance improvements --- src/Meta.php | 3 +-- src/ResourceIdentifier.php | 1 - src/ResourceObject.php | 1 - src/functions.php | 2 +- 4 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/Meta.php b/src/Meta.php index 4ac58b0..8baf4a8 100644 --- a/src/Meta.php +++ b/src/Meta.php @@ -9,8 +9,7 @@ use JsonApiPhp\JsonApi\Internal\RelationshipMember; use JsonApiPhp\JsonApi\Internal\ResourceMember; -final class Meta - implements ErrorMember, ErrorDocumentMember, MetaDocumentMember, DataDocumentMember, ResourceMember, RelationshipMember +final class Meta implements ErrorMember, ErrorDocumentMember, MetaDocumentMember, DataDocumentMember, ResourceMember, RelationshipMember { /** * @var string diff --git a/src/ResourceIdentifier.php b/src/ResourceIdentifier.php index 14a768b..221e096 100644 --- a/src/ResourceIdentifier.php +++ b/src/ResourceIdentifier.php @@ -2,7 +2,6 @@ namespace JsonApiPhp\JsonApi; -use JsonApiPhp\JsonApi\Internal\Identifiable; use JsonApiPhp\JsonApi\Internal\PrimaryData; final class ResourceIdentifier implements PrimaryData diff --git a/src/ResourceObject.php b/src/ResourceObject.php index 900af7a..de0a478 100644 --- a/src/ResourceObject.php +++ b/src/ResourceObject.php @@ -2,7 +2,6 @@ namespace JsonApiPhp\JsonApi; -use JsonApiPhp\JsonApi\Internal\Identifiable; use JsonApiPhp\JsonApi\Internal\Identifier; use JsonApiPhp\JsonApi\Internal\PrimaryData; use JsonApiPhp\JsonApi\Internal\ResourceField; diff --git a/src/functions.php b/src/functions.php index 4640df2..d7c89a4 100644 --- a/src/functions.php +++ b/src/functions.php @@ -29,4 +29,4 @@ function isValidName(string $name): bool function compositeKey(string $type, string $id): string { return "{$type}:{$id}"; -} \ No newline at end of file +} From b345885c51d7e30524193c9797ce3ae782d1fb66 Mon Sep 17 00:00:00 2001 From: Alexey Karapetov Date: Fri, 2 Mar 2018 23:08:39 -0800 Subject: [PATCH 4/5] Performance improvements --- examples/compound_doc.php | 43 +++++++++++++++-------------- src/Internal/Collection.php | 7 +++++ src/PaginatedCollection.php | 39 ++++++++++++++++++++++++++ src/ResourceCollection.php | 3 +- test/CompoundDocumentTest.php | 42 ++++++++++++++-------------- test/PaginationTest.php | 11 +++++--- test/benchmarks/compound10k.php | 49 +++++++++++++++++---------------- 7 files changed, 125 insertions(+), 69 deletions(-) create mode 100644 src/Internal/Collection.php create mode 100644 src/PaginatedCollection.php diff --git a/examples/compound_doc.php b/examples/compound_doc.php index e5f6672..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; @@ -43,30 +44,32 @@ ); $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->identifier(), - new SelfLink('http://example.com/articles/1/relationships/author'), - new RelatedLink('http://example.com/articles/1/author') - ), - new ToMany( - 'comments', - new ResourceIdentifierCollection( - $comment05->identifier(), - $comment12->identifier() + 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/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 @@ +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/ResourceCollection.php b/src/ResourceCollection.php index c1339f5..77ac09f 100644 --- a/src/ResourceCollection.php +++ b/src/ResourceCollection.php @@ -2,9 +2,10 @@ namespace JsonApiPhp\JsonApi; +use JsonApiPhp\JsonApi\Internal\Collection; use JsonApiPhp\JsonApi\Internal\PrimaryData; -class ResourceCollection implements PrimaryData +class ResourceCollection implements PrimaryData, Collection { /** * @var ResourceObject[] diff --git a/test/CompoundDocumentTest.php b/test/CompoundDocumentTest.php index b509b8a..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; @@ -49,30 +49,32 @@ public function testOfficialDocsExample() ); $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->identifier(), - new SelfLink('http://example.com/articles/1/relationships/author'), - new RelatedLink('http://example.com/articles/1/author') - ), - new ToMany( - 'comments', - new ResourceIdentifierCollection( - $comment05->identifier(), - $comment12->identifier() + 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/test/PaginationTest.php b/test/PaginationTest.php index e00ab53..319e011 100644 --- a/test/PaginationTest.php +++ b/test/PaginationTest.php @@ -10,9 +10,10 @@ use JsonApiPhp\JsonApi\Link\LastLink; use JsonApiPhp\JsonApi\Link\NextLink; use JsonApiPhp\JsonApi\Link\PrevLink; -use JsonApiPhp\JsonApi\PaginatedResourceCollection; +use JsonApiPhp\JsonApi\PaginatedCollection; use JsonApiPhp\JsonApi\PaginatedResourceIdentifierCollection; use JsonApiPhp\JsonApi\Pagination; +use JsonApiPhp\JsonApi\ResourceCollection; use JsonApiPhp\JsonApi\ResourceIdentifier; 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') + ) ) ) ); diff --git a/test/benchmarks/compound10k.php b/test/benchmarks/compound10k.php index dc3aac8..048b5bd 100644 --- a/test/benchmarks/compound10k.php +++ b/test/benchmarks/compound10k.php @@ -19,7 +19,6 @@ use JsonApiPhp\JsonApi\Link\RelatedLink; use JsonApiPhp\JsonApi\Link\SelfLink; use JsonApiPhp\JsonApi\Meta; -use JsonApiPhp\JsonApi\PaginatedResourceCollection; use JsonApiPhp\JsonApi\Pagination; use JsonApiPhp\JsonApi\ResourceIdentifier; use JsonApiPhp\JsonApi\ResourceIdentifierCollection; @@ -57,31 +56,33 @@ 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->identifier(), - new SelfLink('http://example.com/articles/1/relationships/author'), - new RelatedLink('http://example.com/articles/1/author') - ), - new ToMany( - 'comments', - new ResourceIdentifierCollection( - $comment05->identifier(), - $comment12->identifier() + 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 +90,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 +110,7 @@ new JsonApi() ); - $error_doc_json = json_encode($error_doc); + $error_doc_json = json_encode($error); } echo $data_doc_json; From a993ee30c1fd4c0dd373d0ae001952bbfb8d0206 Mon Sep 17 00:00:00 2001 From: Alexey Karapetov Date: Fri, 2 Mar 2018 23:55:55 -0800 Subject: [PATCH 5/5] Performance improvements --- src/Internal/ToManyMember.php | 7 ++++++ ...RelationshipMember.php => ToOneMember.php} | 2 +- src/Link/RelatedLink.php | 4 ++-- src/Link/SelfLink.php | 4 ++-- src/Meta.php | 4 ++-- src/PaginatedCollection.php | 2 +- src/PaginatedResourceCollection.php | 24 ------------------- src/PaginatedResourceIdentifierCollection.php | 24 ------------------- src/Pagination.php | 4 ++-- src/ResourceIdentifierCollection.php | 3 ++- src/ToMany.php | 6 ++--- src/ToNull.php | 6 ++--- src/ToOne.php | 4 ++-- test/PaginationTest.php | 16 ++++++------- test/benchmarks/compound10k.php | 2 ++ 15 files changed, 37 insertions(+), 75 deletions(-) create mode 100644 src/Internal/ToManyMember.php rename src/Internal/{RelationshipMember.php => ToOneMember.php} (66%) delete mode 100644 src/PaginatedResourceCollection.php delete mode 100644 src/PaginatedResourceIdentifierCollection.php diff --git a/src/Internal/ToManyMember.php b/src/Internal/ToManyMember.php new file mode 100644 index 0000000..52dd2f7 --- /dev/null +++ b/src/Internal/ToManyMember.php @@ -0,0 +1,7 @@ +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 9ac0603..0000000 --- a/src/PaginatedResourceIdentifierCollection.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/Pagination.php b/src/Pagination.php index 5288ffb..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[] diff --git a/src/ResourceIdentifierCollection.php b/src/ResourceIdentifierCollection.php index 6d03a55..d13eb11 100644 --- a/src/ResourceIdentifierCollection.php +++ b/src/ResourceIdentifierCollection.php @@ -2,9 +2,10 @@ namespace JsonApiPhp\JsonApi; +use JsonApiPhp\JsonApi\Internal\Collection; use JsonApiPhp\JsonApi\Internal\PrimaryData; -class ResourceIdentifierCollection implements PrimaryData +class ResourceIdentifierCollection implements PrimaryData, Collection { /** * @var ResourceIdentifier[] diff --git a/src/ToMany.php b/src/ToMany.php index 13a3e0b..94b873b 100644 --- a/src/ToMany.php +++ b/src/ToMany.php @@ -3,15 +3,15 @@ namespace JsonApiPhp\JsonApi; use JsonApiPhp\JsonApi\Internal\Identifier; -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; /** @@ -19,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; diff --git a/src/ToNull.php b/src/ToNull.php index 9af2079..0efaef0 100644 --- a/src/ToNull.php +++ b/src/ToNull.php @@ -2,19 +2,19 @@ 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; diff --git a/src/ToOne.php b/src/ToOne.php index 6cfcbaa..c6d32da 100644 --- a/src/ToOne.php +++ b/src/ToOne.php @@ -3,9 +3,9 @@ namespace JsonApiPhp\JsonApi; use JsonApiPhp\JsonApi\Internal\Identifier; -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 { @@ -18,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; diff --git a/test/PaginationTest.php b/test/PaginationTest.php index 319e011..17ee668 100644 --- a/test/PaginationTest.php +++ b/test/PaginationTest.php @@ -11,10 +11,10 @@ use JsonApiPhp\JsonApi\Link\NextLink; use JsonApiPhp\JsonApi\Link\PrevLink; use JsonApiPhp\JsonApi\PaginatedCollection; -use JsonApiPhp\JsonApi\PaginatedResourceIdentifierCollection; use JsonApiPhp\JsonApi\Pagination; use JsonApiPhp\JsonApi\ResourceCollection; use JsonApiPhp\JsonApi\ResourceIdentifier; +use JsonApiPhp\JsonApi\ResourceIdentifierCollection; use JsonApiPhp\JsonApi\ResourceObject; use JsonApiPhp\JsonApi\ToMany; @@ -89,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 048b5bd..c1a0ae2 100644 --- a/test/benchmarks/compound10k.php +++ b/test/benchmarks/compound10k.php @@ -19,7 +19,9 @@ use JsonApiPhp\JsonApi\Link\RelatedLink; use JsonApiPhp\JsonApi\Link\SelfLink; use JsonApiPhp\JsonApi\Meta; +use JsonApiPhp\JsonApi\PaginatedCollection; use JsonApiPhp\JsonApi\Pagination; +use JsonApiPhp\JsonApi\ResourceCollection; use JsonApiPhp\JsonApi\ResourceIdentifier; use JsonApiPhp\JsonApi\ResourceIdentifierCollection; use JsonApiPhp\JsonApi\ResourceObject;