From dd5584cbcbcea2b570cf6c6772858fdc288ae0c9 Mon Sep 17 00:00:00 2001 From: Sergey Braga Date: Mon, 9 Sep 2019 14:06:17 +0300 Subject: [PATCH 1/4] Fix missing data if it was passed later using attachTo method --- src/Error.php | 7 ++----- src/JsonApi.php | 10 ++++------ 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/src/Error.php b/src/Error.php index 35b7b8d..33b54aa 100644 --- a/src/Error.php +++ b/src/Error.php @@ -11,13 +11,10 @@ */ final class Error implements ErrorDocumentMember { - private $error; - public function __construct(ErrorMember ...$members) { - $this->error = (object) []; foreach ($members as $member) { - $member->attachTo($this->error); + $member->attachTo($this); } } @@ -26,6 +23,6 @@ public function __construct(ErrorMember ...$members) */ public function attachTo($o): void { - $o->errors[] = $this->error; + $o->errors[] = $this; } } diff --git a/src/JsonApi.php b/src/JsonApi.php index c895bb6..3a76ad8 100644 --- a/src/JsonApi.php +++ b/src/JsonApi.php @@ -8,15 +8,13 @@ final class JsonApi implements MetaDocumentMember, DataDocumentMember, ErrorDocumentMember { - private $obj; + public $version; public function __construct(string $version = '1.0', Meta $meta = null) { - $this->obj = (object) [ - 'version' => $version, - ]; + $this->version = $version; if ($meta) { - $meta->attachTo($this->obj); + $meta->attachTo($this); } } @@ -25,6 +23,6 @@ public function __construct(string $version = '1.0', Meta $meta = null) */ public function attachTo($o): void { - $o->jsonapi = $this->obj; + $o->jsonapi = $this; } } From 62c66695e6e19589bc8767963c4aabe46cbefc28 Mon Sep 17 00:00:00 2001 From: Sergey Braga Date: Mon, 9 Sep 2019 18:42:45 +0300 Subject: [PATCH 2/4] Fix code style --- examples/compound_doc.php | 2 +- test/CompoundDocumentTest.php | 2 +- test/benchmarks/compound10k.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/compound_doc.php b/examples/compound_doc.php index ddb6fd9..c9feae0 100644 --- a/examples/compound_doc.php +++ b/examples/compound_doc.php @@ -33,8 +33,8 @@ new Attribute('body', 'First!'), new SelfLink('http://example.com/comments/5'), new ToOne('author', new ResourceIdentifier('people', '2')) - ); + $comment12 = new ResourceObject( 'comments', '12', diff --git a/test/CompoundDocumentTest.php b/test/CompoundDocumentTest.php index 7441200..bb013ce 100644 --- a/test/CompoundDocumentTest.php +++ b/test/CompoundDocumentTest.php @@ -38,8 +38,8 @@ public function testOfficialDocsExample() new Attribute('body', 'First!'), new SelfLink('http://example.com/comments/5'), new ToOne('author', new ResourceIdentifier('people', '2')) - ); + $comment12 = new ResourceObject( 'comments', '12', diff --git a/test/benchmarks/compound10k.php b/test/benchmarks/compound10k.php index c1a0ae2..513518b 100644 --- a/test/benchmarks/compound10k.php +++ b/test/benchmarks/compound10k.php @@ -48,8 +48,8 @@ new Attribute('body', 'First!'), new SelfLink('http://example.com/comments/5'), new ToOne('author', new ResourceIdentifier('people', '2')) - ); + $comment12 = new ResourceObject( 'comments', '12', From 5d68025ef111e1d10c05a9fb4bb050a97e5c2120 Mon Sep 17 00:00:00 2001 From: Sergey Braga Date: Mon, 9 Sep 2019 19:39:04 +0300 Subject: [PATCH 3/4] Remove unnecessary argument Error --- src/ErrorDocument.php | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/src/ErrorDocument.php b/src/ErrorDocument.php index e4a52e5..2dfb70a 100644 --- a/src/ErrorDocument.php +++ b/src/ErrorDocument.php @@ -8,21 +8,12 @@ * A Document containing an array of Error objects * @see http://jsonapi.org/format/#document-top-level */ -final class ErrorDocument implements \JsonSerializable +final class ErrorDocument { - private $obj; - - public function __construct(Error $error, ErrorDocumentMember ...$members) + public function __construct(ErrorDocumentMember ...$members) { - $this->obj = (object) []; - $error->attachTo($this->obj); foreach ($members as $member) { - $member->attachTo($this->obj); + $member->attachTo($this); } } - - public function jsonSerialize() - { - return $this->obj; - } } From 9534e42296def3d1531756eed161dfc9f73ddd89 Mon Sep 17 00:00:00 2001 From: Sergey Braga Date: Tue, 10 Sep 2019 09:33:16 +0300 Subject: [PATCH 4/4] Revert dd5584cb and mark attachTo as internal --- src/Attribute.php | 1 + src/EmptyRelationship.php | 1 + src/Error.php | 8 ++++++-- src/Error/Code.php | 1 + src/Error/Detail.php | 1 + src/Error/Id.php | 1 + src/Error/SourceParameter.php | 1 + src/Error/SourcePointer.php | 1 + src/Error/Status.php | 1 + src/Error/Title.php | 1 + src/ErrorDocument.php | 12 ++++++++++-- src/Included.php | 1 + src/Internal/Attachable.php | 1 + src/JsonApi.php | 11 +++++++---- src/Link/AboutLink.php | 1 + src/Link/FirstLink.php | 1 + src/Link/LastLink.php | 1 + src/Link/NextLink.php | 1 + src/Link/PrevLink.php | 1 + src/Link/RelatedLink.php | 1 + src/Link/SelfLink.php | 1 + src/Meta.php | 4 ++++ src/NullData.php | 1 + src/PaginatedCollection.php | 1 + src/Pagination.php | 1 + src/ResourceCollection.php | 1 + src/ResourceIdentifier.php | 2 ++ src/ResourceIdentifierCollection.php | 1 + src/ResourceObject.php | 2 ++ src/ToMany.php | 1 + src/ToNull.php | 1 + src/ToOne.php | 1 + 32 files changed, 57 insertions(+), 8 deletions(-) diff --git a/src/Attribute.php b/src/Attribute.php index ba1f50b..d45834c 100644 --- a/src/Attribute.php +++ b/src/Attribute.php @@ -22,6 +22,7 @@ public function __construct(string $name, $val) /** * @param object $o + * @internal */ public function attachTo($o): void { diff --git a/src/EmptyRelationship.php b/src/EmptyRelationship.php index 1482792..659a9df 100644 --- a/src/EmptyRelationship.php +++ b/src/EmptyRelationship.php @@ -23,6 +23,7 @@ public function __construct(string $name, RelationshipMember $member, Relationsh /** * @param object $o + * @internal */ public function attachTo($o): void { diff --git a/src/Error.php b/src/Error.php index 33b54aa..ba0e3a0 100644 --- a/src/Error.php +++ b/src/Error.php @@ -11,18 +11,22 @@ */ final class Error implements ErrorDocumentMember { + private $error; + public function __construct(ErrorMember ...$members) { + $this->error = (object) []; foreach ($members as $member) { - $member->attachTo($this); + $member->attachTo($this->error); } } /** * @param object $o + * @internal */ public function attachTo($o): void { - $o->errors[] = $this; + $o->errors[] = $this->error; } } diff --git a/src/Error/Code.php b/src/Error/Code.php index 6705037..a03b827 100644 --- a/src/Error/Code.php +++ b/src/Error/Code.php @@ -21,6 +21,7 @@ public function __construct(string $code) /** * @param object $o + * @internal */ public function attachTo($o): void { diff --git a/src/Error/Detail.php b/src/Error/Detail.php index dfeb86d..f4d169c 100644 --- a/src/Error/Detail.php +++ b/src/Error/Detail.php @@ -21,6 +21,7 @@ public function __construct(string $detail) /** * @param object $o + * @internal */ public function attachTo($o): void { diff --git a/src/Error/Id.php b/src/Error/Id.php index 40eb903..11c5b10 100644 --- a/src/Error/Id.php +++ b/src/Error/Id.php @@ -21,6 +21,7 @@ public function __construct(string $id) /** * @param object $o + * @internal */ public function attachTo($o): void { diff --git a/src/Error/SourceParameter.php b/src/Error/SourceParameter.php index f3bc37a..3e48461 100644 --- a/src/Error/SourceParameter.php +++ b/src/Error/SourceParameter.php @@ -22,6 +22,7 @@ public function __construct(string $parameter) /** * @param object $o + * @internal */ public function attachTo($o): void { diff --git a/src/Error/SourcePointer.php b/src/Error/SourcePointer.php index 0af0b5b..d6cecaa 100644 --- a/src/Error/SourcePointer.php +++ b/src/Error/SourcePointer.php @@ -19,6 +19,7 @@ public function __construct(string $pointer) /** * @param object $o + * @internal */ public function attachTo($o): void { diff --git a/src/Error/Status.php b/src/Error/Status.php index 0d396f6..6be53aa 100644 --- a/src/Error/Status.php +++ b/src/Error/Status.php @@ -21,6 +21,7 @@ public function __construct(string $status) /** * @param object $o + * @internal */ public function attachTo($o): void { diff --git a/src/Error/Title.php b/src/Error/Title.php index 1c18f2c..2226f53 100644 --- a/src/Error/Title.php +++ b/src/Error/Title.php @@ -22,6 +22,7 @@ public function __construct(string $title) /** * @param object $o + * @internal */ public function attachTo($o): void { diff --git a/src/ErrorDocument.php b/src/ErrorDocument.php index 2dfb70a..4d4fe73 100644 --- a/src/ErrorDocument.php +++ b/src/ErrorDocument.php @@ -8,12 +8,20 @@ * A Document containing an array of Error objects * @see http://jsonapi.org/format/#document-top-level */ -final class ErrorDocument +final class ErrorDocument implements \JsonSerializable { + private $obj; + public function __construct(ErrorDocumentMember ...$members) { + $this->obj = (object) []; foreach ($members as $member) { - $member->attachTo($this); + $member->attachTo($this->obj); } } + + public function jsonSerialize() + { + return $this->obj; + } } diff --git a/src/Included.php b/src/Included.php index 220270b..5b2dbd4 100644 --- a/src/Included.php +++ b/src/Included.php @@ -40,6 +40,7 @@ public function validateLinkage(PrimaryData $data): void /** * @param object $o + * @internal */ public function attachTo($o): void { diff --git a/src/Internal/Attachable.php b/src/Internal/Attachable.php index edd2de2..0178295 100644 --- a/src/Internal/Attachable.php +++ b/src/Internal/Attachable.php @@ -9,6 +9,7 @@ interface Attachable { /** * @param object $o + * @internal */ public function attachTo($o): void; } diff --git a/src/JsonApi.php b/src/JsonApi.php index 3a76ad8..3c1e08b 100644 --- a/src/JsonApi.php +++ b/src/JsonApi.php @@ -8,21 +8,24 @@ final class JsonApi implements MetaDocumentMember, DataDocumentMember, ErrorDocumentMember { - public $version; + private $obj; public function __construct(string $version = '1.0', Meta $meta = null) { - $this->version = $version; + $this->obj = (object) [ + 'version' => $version, + ]; if ($meta) { - $meta->attachTo($this); + $meta->attachTo($this->obj); } } /** * @param object $o + * @internal */ public function attachTo($o): void { - $o->jsonapi = $this; + $o->jsonapi = $this->obj; } } diff --git a/src/Link/AboutLink.php b/src/Link/AboutLink.php index 6035330..e317ca8 100644 --- a/src/Link/AboutLink.php +++ b/src/Link/AboutLink.php @@ -12,6 +12,7 @@ final class AboutLink implements ErrorMember /** * @param object $o + * @internal */ public function attachTo($o): void { diff --git a/src/Link/FirstLink.php b/src/Link/FirstLink.php index 755a12b..af13251 100644 --- a/src/Link/FirstLink.php +++ b/src/Link/FirstLink.php @@ -12,6 +12,7 @@ final class FirstLink implements PaginationLink /** * @param object $o + * @internal */ public function attachTo($o): void { diff --git a/src/Link/LastLink.php b/src/Link/LastLink.php index c11b2e0..037b228 100644 --- a/src/Link/LastLink.php +++ b/src/Link/LastLink.php @@ -12,6 +12,7 @@ final class LastLink implements PaginationLink /** * @param object $o + * @internal */ public function attachTo($o): void { diff --git a/src/Link/NextLink.php b/src/Link/NextLink.php index 39f5641..c2ba366 100644 --- a/src/Link/NextLink.php +++ b/src/Link/NextLink.php @@ -12,6 +12,7 @@ final class NextLink implements PaginationLink /** * @param object $o + * @internal */ public function attachTo($o): void { diff --git a/src/Link/PrevLink.php b/src/Link/PrevLink.php index f38d94b..b612450 100644 --- a/src/Link/PrevLink.php +++ b/src/Link/PrevLink.php @@ -12,6 +12,7 @@ final class PrevLink implements PaginationLink /** * @param object $o + * @internal */ public function attachTo($o): void { diff --git a/src/Link/RelatedLink.php b/src/Link/RelatedLink.php index 7434423..c1924ee 100644 --- a/src/Link/RelatedLink.php +++ b/src/Link/RelatedLink.php @@ -12,6 +12,7 @@ final class RelatedLink implements RelationshipMember /** * @param object $o + * @internal */ public function attachTo($o): void { diff --git a/src/Link/SelfLink.php b/src/Link/SelfLink.php index 5a9a31f..fde232b 100644 --- a/src/Link/SelfLink.php +++ b/src/Link/SelfLink.php @@ -14,6 +14,7 @@ final class SelfLink implements DataDocumentMember, ResourceMember, Relationship /** * @param object $o + * @internal */ public function attachTo($o): void { diff --git a/src/Meta.php b/src/Meta.php index 5b9a2c3..758c3bb 100644 --- a/src/Meta.php +++ b/src/Meta.php @@ -26,6 +26,10 @@ public function __construct(string $key, $value) $this->value = $value; } + /** + * @param object $o + * @internal + */ public function attachTo($o): void { child($o, 'meta')->{$this->key} = $this->value; diff --git a/src/NullData.php b/src/NullData.php index 03cd15e..bbb3dbc 100644 --- a/src/NullData.php +++ b/src/NullData.php @@ -8,6 +8,7 @@ final class NullData implements PrimaryData { /** * @param object $o + * @internal */ public function attachTo($o): void { diff --git a/src/PaginatedCollection.php b/src/PaginatedCollection.php index c4a51da..15e3cc7 100644 --- a/src/PaginatedCollection.php +++ b/src/PaginatedCollection.php @@ -24,6 +24,7 @@ public function __construct(Pagination $pagination, Collection $collection) /** * @param object $o + * @internal */ public function attachTo($o): void { diff --git a/src/Pagination.php b/src/Pagination.php index 42f2ef7..674f375 100644 --- a/src/Pagination.php +++ b/src/Pagination.php @@ -20,6 +20,7 @@ public function __construct(PaginationLink ...$links) /** * @param object $o + * @internal */ public function attachTo($o): void { diff --git a/src/ResourceCollection.php b/src/ResourceCollection.php index e2f0c4b..f337ad5 100644 --- a/src/ResourceCollection.php +++ b/src/ResourceCollection.php @@ -19,6 +19,7 @@ public function __construct(ResourceObject ...$resources) /** * @param object $o + * @internal */ public function attachTo($o): void { diff --git a/src/ResourceIdentifier.php b/src/ResourceIdentifier.php index 6967856..2974492 100644 --- a/src/ResourceIdentifier.php +++ b/src/ResourceIdentifier.php @@ -35,6 +35,7 @@ public function __construct(string $type, string $id, Meta $meta = null) /** * @param object $o + * @internal */ public function attachTo($o): void { @@ -43,6 +44,7 @@ public function attachTo($o): void /** * @param object $o + * @internal */ public function attachToCollection($o): void { diff --git a/src/ResourceIdentifierCollection.php b/src/ResourceIdentifierCollection.php index 5e87189..84b8887 100644 --- a/src/ResourceIdentifierCollection.php +++ b/src/ResourceIdentifierCollection.php @@ -19,6 +19,7 @@ public function __construct(ResourceIdentifier ...$identifiers) /** * @param object $o + * @internal */ public function attachTo($o): void { diff --git a/src/ResourceObject.php b/src/ResourceObject.php index bab4f25..20ce548 100644 --- a/src/ResourceObject.php +++ b/src/ResourceObject.php @@ -61,6 +61,7 @@ public function registerIn(array &$registry): void /** * @param object $o + * @internal */ public function attachTo($o): void { @@ -77,6 +78,7 @@ public function attachAsIncludedTo($o): void /** * @param object $o + * @internal */ public function attachToCollection($o): void { diff --git a/src/ToMany.php b/src/ToMany.php index 0811006..c0a7278 100644 --- a/src/ToMany.php +++ b/src/ToMany.php @@ -29,6 +29,7 @@ public function __construct(string $name, ResourceIdentifierCollection $collecti /** * @param object $o + * @internal */ public function attachTo($o): void { diff --git a/src/ToNull.php b/src/ToNull.php index 4e30861..8b2d3f1 100644 --- a/src/ToNull.php +++ b/src/ToNull.php @@ -23,6 +23,7 @@ public function __construct(string $name, ToOneMember ...$members) /** * @param object $o + * @internal */ public function attachTo($o): void { diff --git a/src/ToOne.php b/src/ToOne.php index dcd2e7a..dbbd7ac 100644 --- a/src/ToOne.php +++ b/src/ToOne.php @@ -28,6 +28,7 @@ public function __construct(string $name, ResourceIdentifier $identifier, ToOneM /** * @param object $o + * @internal */ public function attachTo($o): void {