From efb9bf44deb7bde8e57094e8cdcd215e51a659e2 Mon Sep 17 00:00:00 2001 From: f3ath Date: Tue, 26 Jul 2022 17:22:07 -0700 Subject: [PATCH 1/2] v3 --- .php-cs-fixer.php | 16 ++++ .php_cs.dist | 53 ------------- .travis.yml | 14 ---- CHANGELOG.md | 11 ++- composer.json | 8 +- examples/compound_doc.php | 4 +- examples/simple_doc.php | 4 +- phpunit.xml.dist | 28 +++---- src/Attribute.php | 16 ++-- src/CompoundDocument.php | 18 ++--- src/DataDocument.php | 17 ++--- src/EmptyRelationship.php | 20 ++--- src/Error.php | 17 ++--- src/Error/Code.php | 19 ++--- src/Error/Detail.php | 19 ++--- src/Error/Id.php | 19 ++--- src/Error/SourceParameter.php | 20 ++--- src/Error/SourcePointer.php | 17 ++--- src/Error/Status.php | 19 ++--- src/Error/Title.php | 19 ++--- src/ErrorDocument.php | 19 +++-- src/Included.php | 36 +++------ src/Internal/Attachable.php | 9 ++- src/Internal/BaseResource.php | 37 ++++----- src/Internal/Collection.php | 7 +- src/Internal/DataDocumentMember.php | 7 +- src/Internal/ErrorDocumentMember.php | 7 +- src/Internal/ErrorMember.php | 7 +- src/Internal/Identifier.php | 12 +-- src/Internal/LinkTrait.php | 17 ++--- src/Internal/MetaDocumentMember.php | 7 +- src/Internal/PaginationLink.php | 4 +- src/Internal/PrimaryData.php | 7 +- src/Internal/RelationshipMember.php | 7 +- src/Internal/ResourceField.php | 4 +- src/Internal/ResourceFieldTrait.php | 23 +++--- src/Internal/ResourceMember.php | 7 +- src/Internal/ToManyMember.php | 7 +- src/Internal/ToOneMember.php | 7 +- src/JsonApi.php | 21 +++-- src/Link/AboutLink.php | 13 ++-- src/Link/FirstLink.php | 13 ++-- src/Link/LastLink.php | 13 ++-- src/Link/NextLink.php | 13 ++-- src/Link/PrevLink.php | 13 ++-- src/Link/RelatedLink.php | 13 ++-- src/Link/SelfLink.php | 13 ++-- src/Meta.php | 49 ++++++------ src/MetaDocument.php | 17 ++--- src/NewResourceObject.php | 10 +-- src/NullData.php | 14 ++-- src/PaginatedCollection.php | 33 ++------ src/Pagination.php | 12 ++- src/ResourceCollection.php | 22 ++---- src/ResourceIdentifier.php | 39 +++------- src/ResourceIdentifierCollection.php | 22 ++---- src/ResourceObject.php | 39 +++------- src/ToMany.php | 30 +++----- src/ToNull.php | 16 ++-- src/ToOne.php | 26 ++----- src/functions.php | 20 +++-- test/BaseTestCase.php | 10 +-- test/CompoundDocumentTest.php | 76 ++----------------- .../ManyResourceIdentifiersTest.php | 13 ++-- test/DataDocument/ManyResourceObjectsTest.php | 13 ++-- test/DataDocument/NullDataTest.php | 10 +-- .../SingleResourceIdentifierTest.php | 13 ++-- .../DataDocument/SingleResourceObjectTest.php | 13 ++-- test/ErrorDocumentTest.php | 13 ++-- test/ExamplesTest.php | 10 +-- test/JsonApiTest.php | 10 +-- test/LinkObjectTest.php | 10 +-- test/MetaDocumentTest.php | 10 +-- test/MetaTest.php | 10 +-- test/NewResourceObjectTest.php | 28 +++---- test/PaginationTest.php | 13 ++-- test/ResourceIdentifierTest.php | 7 +- test/ResourceObjectTest.php | 49 +++++------- test/benchmarks/compound10k.php | 4 +- 79 files changed, 536 insertions(+), 826 deletions(-) create mode 100644 .php-cs-fixer.php delete mode 100644 .php_cs.dist delete mode 100644 .travis.yml diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php new file mode 100644 index 0000000..acd1414 --- /dev/null +++ b/.php-cs-fixer.php @@ -0,0 +1,16 @@ +exclude('somedir') + ->notPath('src/Symfony/Component/Translation/Tests/fixtures/resources.php') + ->in(__DIR__); + +$config = new PhpCsFixer\Config(); +return $config->setRules([ + '@PSR12' => true, + 'array_syntax' => ['syntax' => 'short'], + 'braces' => [ + 'allow_single_line_closure' => true, + 'position_after_functions_and_oop_constructs' => 'same'], +]) + ->setFinder($finder); \ No newline at end of file diff --git a/.php_cs.dist b/.php_cs.dist deleted file mode 100644 index 06b82a3..0000000 --- a/.php_cs.dist +++ /dev/null @@ -1,53 +0,0 @@ -files() - ->name('*.php') - ->in(__DIR__ . '/examples') - ->in(__DIR__ . '/src') - ->in(__DIR__ . '/test'); -return PhpCsFixer\Config::create() - ->setUsingCache(true) - ->setRiskyAllowed(true) - ->setRules([ - '@PSR2' => true, - 'array_syntax' => ['syntax' => 'short'], - 'binary_operator_spaces' => true, - 'cast_spaces' => true, - 'concat_space' => true, - 'declare_strict_types' => true, - 'include' => true, - 'is_null' => true, - 'lowercase_cast' => true, - 'mb_str_functions' => true, - 'method_separation' => true, - 'native_function_casing' => true, - 'no_blank_lines_after_class_opening' => true, - 'no_blank_lines_after_phpdoc' => true, - 'no_empty_statement' => true, - 'no_extra_consecutive_blank_lines' => true, - 'no_leading_import_slash' => true, - 'no_leading_namespace_whitespace' => true, - 'no_trailing_comma_in_singleline_array' => true, - 'no_unused_imports' => true, - 'no_whitespace_in_blank_line' => true, - 'object_operator_without_whitespace' => true, - 'ordered_imports' => true, - 'phpdoc_align' => true, - 'phpdoc_indent' => true, - 'phpdoc_no_access' => true, - 'phpdoc_no_package' => true, - 'phpdoc_order' => true, - 'phpdoc_scalar' => true, - 'phpdoc_trim' => true, - 'phpdoc_types' => true, - 'psr0' => true, - 'short_scalar_cast' => true, - 'single_blank_line_before_namespace' => true, - 'single_quote' => true, - 'standardize_not_equals' => true, - 'strict_comparison' => true, - 'ternary_operator_spaces' => true, - 'trailing_comma_in_multiline_array' => true, - 'whitespace_after_comma_in_array' => true, - ]) - ->setFinder($finder); diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index b2a600d..0000000 --- a/.travis.yml +++ /dev/null @@ -1,14 +0,0 @@ -language: php -php: - - '7.1' - - '7.2' - - '7.3' - - '7.4' - -before_script: - - composer install - - mkdir build/logs -p - -script: - - ./vendor/bin/php-cs-fixer fix -v --dry-run - - ./vendor/bin/phpunit --coverage-clover build/logs/clover.xml diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b5bc97..22fd9fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +## [3.0.0] - 2022-07-26 +### Changed +- The package is migrated to PHP 8.1 + +### Removed +- Support for PHP 7 and older versions +- Compound document validation logic is dropped + ## [2.2.0] - 2020-10-12 ### Added - `NewResourceObject` to allow omitting `id` in resources to-be-created (#108) @@ -30,7 +38,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Added - v2 initial release -[Unreleased]: https://github.com/json-api-php/json-api/compare/2.2.0...HEAD +[Unreleased]: https://github.com/json-api-php/json-api/compare/3.0.0...HEAD +[3.0.0]: https://github.com/json-api-php/json-api/compare/2.2.2...3.0.0 [2.2.0]: https://github.com/json-api-php/json-api/compare/2.1.2...2.2.0 [2.1.2]: https://github.com/json-api-php/json-api/compare/2.1.1...2.1.2 [2.1.1]: https://github.com/json-api-php/json-api/compare/2.1.0...2.1.1 diff --git a/composer.json b/composer.json index ffcd77e..5a7e2b9 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "json-api-php/json-api", - "description": "An attempt to express JSON API specs (jsonapi.org) in object-oriented way as a set of PHP 7 classes", + "description": "JSON API specs (jsonapi.org) as a set of PHP classes", "type": "library", "prefer-stable": true, "license": "MIT", @@ -11,12 +11,12 @@ } ], "require": { - "php": ">=7.1", + "php": ">=8.1", "ext-json": "*" }, "require-dev": { - "phpunit/phpunit": "^7.0||^8.0", - "friendsofphp/php-cs-fixer": "^2.13" + "phpunit/phpunit": "^9.0", + "friendsofphp/php-cs-fixer": "^3.9" }, "autoload": { "psr-4": { diff --git a/examples/compound_doc.php b/examples/compound_doc.php index c9feae0..1d0cbbb 100644 --- a/examples/compound_doc.php +++ b/examples/compound_doc.php @@ -1,4 +1,6 @@ - - - - - ./test - - - - - ./src/ - - + + + + + ./src/ + + + + + ./test + + diff --git a/src/Attribute.php b/src/Attribute.php index d45834c..8276746 100644 --- a/src/Attribute.php +++ b/src/Attribute.php @@ -1,4 +1,6 @@ -validateFieldName($name); $this->name = $name; $this->val = $val; @@ -24,8 +25,7 @@ public function __construct(string $name, $val) * @param object $o * @internal */ - public function attachTo($o): void - { + public function attachTo(object $o): void { child($o, 'attributes')->{$this->name} = $this->val; } } diff --git a/src/CompoundDocument.php b/src/CompoundDocument.php index a01c84d..7a15492 100644 --- a/src/CompoundDocument.php +++ b/src/CompoundDocument.php @@ -1,27 +1,25 @@ -validateLinkage($data); + public function __construct(PrimaryData $data, Included $included, DataDocumentMember ...$members) { $this->doc = combine($data, $included, ...$members); } - #[\ReturnTypeWillChange] - public function jsonSerialize() - { + public function jsonSerialize(): object { return $this->doc; } } diff --git a/src/DataDocument.php b/src/DataDocument.php index 3678afa..20d0e7c 100644 --- a/src/DataDocument.php +++ b/src/DataDocument.php @@ -1,26 +1,25 @@ -value = combine($data, ...$members); } - #[\ReturnTypeWillChange] - public function jsonSerialize() - { + public function jsonSerialize(): object { return $this->value; } } diff --git a/src/EmptyRelationship.php b/src/EmptyRelationship.php index 659a9df..2c123bb 100644 --- a/src/EmptyRelationship.php +++ b/src/EmptyRelationship.php @@ -1,4 +1,6 @@ -name = $name; + public function __construct( + private readonly string $name, + RelationshipMember $member, + RelationshipMember ...$members + ) { $this->obj = combine($member, ...$members); } @@ -25,8 +28,7 @@ public function __construct(string $name, RelationshipMember $member, Relationsh * @param object $o * @internal */ - public function attachTo($o): void - { + public function attachTo(object $o): void { child($o, 'relationships')->{$this->name} = $this->obj; } } diff --git a/src/Error.php b/src/Error.php index ba0e3a0..8a222fe 100644 --- a/src/Error.php +++ b/src/Error.php @@ -1,4 +1,6 @@ -error = (object) []; + public function __construct(ErrorMember ...$members) { + $this->error = (object)[]; foreach ($members as $member) { $member->attachTo($this->error); } @@ -25,8 +25,7 @@ public function __construct(ErrorMember ...$members) * @param object $o * @internal */ - public function attachTo($o): void - { + public function attachTo(object $o): void { $o->errors[] = $this->error; } } diff --git a/src/Error/Code.php b/src/Error/Code.php index a03b827..40455dd 100644 --- a/src/Error/Code.php +++ b/src/Error/Code.php @@ -1,30 +1,23 @@ -code = $code; + public function __construct(private readonly string $code) { } /** * @param object $o * @internal */ - public function attachTo($o): void - { + public function attachTo(object $o): void { $o->code = $this->code; } } diff --git a/src/Error/Detail.php b/src/Error/Detail.php index f4d169c..217c43d 100644 --- a/src/Error/Detail.php +++ b/src/Error/Detail.php @@ -1,30 +1,23 @@ -detail = $detail; + public function __construct(private readonly string $detail) { } /** * @param object $o * @internal */ - public function attachTo($o): void - { + public function attachTo(object $o): void { $o->detail = $this->detail; } } diff --git a/src/Error/Id.php b/src/Error/Id.php index 11c5b10..371884f 100644 --- a/src/Error/Id.php +++ b/src/Error/Id.php @@ -1,30 +1,23 @@ -id = $id; + public function __construct(private readonly string $id) { } /** * @param object $o * @internal */ - public function attachTo($o): void - { + public function attachTo(object $o): void { $o->id = $this->id; } } diff --git a/src/Error/SourceParameter.php b/src/Error/SourceParameter.php index 3e48461..d344d64 100644 --- a/src/Error/SourceParameter.php +++ b/src/Error/SourceParameter.php @@ -1,31 +1,25 @@ -parameter = $parameter; + public function __construct(private readonly string $parameter) { } /** * @param object $o * @internal */ - public function attachTo($o): void - { + public function attachTo(object $o): void { child($o, 'source')->parameter = $this->parameter; } } diff --git a/src/Error/SourcePointer.php b/src/Error/SourcePointer.php index d6cecaa..c33d0b1 100644 --- a/src/Error/SourcePointer.php +++ b/src/Error/SourcePointer.php @@ -1,28 +1,25 @@ -pointer = $pointer; + public function __construct(private readonly string $pointer) { } /** * @param object $o * @internal */ - public function attachTo($o): void - { + public function attachTo(object $o): void { child($o, 'source')->pointer = $this->pointer; } } diff --git a/src/Error/Status.php b/src/Error/Status.php index 6be53aa..2083373 100644 --- a/src/Error/Status.php +++ b/src/Error/Status.php @@ -1,30 +1,23 @@ -status = $status; + public function __construct(private readonly string $status) { } /** * @param object $o * @internal */ - public function attachTo($o): void - { + public function attachTo(object $o): void { $o->status = $this->status; } } diff --git a/src/Error/Title.php b/src/Error/Title.php index 2226f53..1352eb5 100644 --- a/src/Error/Title.php +++ b/src/Error/Title.php @@ -1,31 +1,24 @@ -title = $title; + public function __construct(private readonly string $title) { } /** * @param object $o * @internal */ - public function attachTo($o): void - { + public function attachTo(object $o): void { $o->title = $this->title; } } diff --git a/src/ErrorDocument.php b/src/ErrorDocument.php index 554c8d5..4cb70b5 100644 --- a/src/ErrorDocument.php +++ b/src/ErrorDocument.php @@ -1,28 +1,27 @@ -obj = (object) []; + public function __construct(ErrorDocumentMember ...$members) { + $this->obj = (object)[]; foreach ($members as $member) { $member->attachTo($this->obj); } } - #[\ReturnTypeWillChange] - public function jsonSerialize() - { + public function jsonSerialize(): object { return $this->obj; } } diff --git a/src/Included.php b/src/Included.php index 5b2dbd4..96a1edb 100644 --- a/src/Included.php +++ b/src/Included.php @@ -1,40 +1,29 @@ -key(); if (isset($this->resources[$key])) { - throw new \LogicException("Resource $resource is already included"); + throw new LogicException("Resource $resource is already included"); } $this->resources[$key] = $resource; - $resource->registerIn($this->identifiers); - } - } - - public function validateLinkage(PrimaryData $data): void - { - $registry = []; - $data->registerIn($registry); - foreach ($this->resources as $resource) { - if (isset($registry[$resource->key()]) || isset($this->identifiers[$resource->key()])) { - continue; - } - throw new \LogicException('Full linkage required for '.$resource); } } @@ -42,8 +31,7 @@ public function validateLinkage(PrimaryData $data): void * @param object $o * @internal */ - public function attachTo($o): void - { + 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 f11bbba..01de494 100644 --- a/src/Internal/Attachable.php +++ b/src/Internal/Attachable.php @@ -1,16 +1,17 @@ -obj = (object) ['type' => $type]; - $this->type = $type; + $this->obj = (object)['type' => $type]; $this->addMembers(...$members); } @@ -32,17 +30,13 @@ public function __construct(string $type, ResourceMember ...$members) * @param ResourceMember ...$members * @internal */ - protected function addMembers(ResourceMember ...$members): void - { + protected function addMembers(ResourceMember ...$members): void { $fields = []; foreach ($members as $member) { - if ($member instanceof Identifier) { - $member->registerIn($this->registry); - } if ($member instanceof ResourceField) { $name = $member->name(); if (isset($fields[$name])) { - throw new \LogicException("Field '$name' already exists'"); + throw new LogicException("Field '$name' already exists'"); } $fields[$name] = true; } @@ -54,8 +48,7 @@ protected function addMembers(ResourceMember ...$members): void * @param object $o * @internal */ - public function attachTo($o): void - { + public function attachTo(object $o): void { $o->data = $this->obj; } } diff --git a/src/Internal/Collection.php b/src/Internal/Collection.php index e0e73d0..cd8b8d6 100644 --- a/src/Internal/Collection.php +++ b/src/Internal/Collection.php @@ -1,7 +1,8 @@ -link = (object) ['href' => $url]; + $this->link = (object)['href' => $url]; foreach ($metas as $meta) { $meta->attachTo($this->link); } diff --git a/src/Internal/MetaDocumentMember.php b/src/Internal/MetaDocumentMember.php index 27aded5..e204a70 100644 --- a/src/Internal/MetaDocumentMember.php +++ b/src/Internal/MetaDocumentMember.php @@ -1,10 +1,11 @@ -name; } } diff --git a/src/Internal/ResourceMember.php b/src/Internal/ResourceMember.php index b20e3b0..ba4918d 100644 --- a/src/Internal/ResourceMember.php +++ b/src/Internal/ResourceMember.php @@ -1,10 +1,11 @@ -obj = (object) [ + public function __construct(string $version = '1.0', Meta $meta = null) { + $this->obj = (object)[ 'version' => $version, ]; - if ($meta) { - $meta->attachTo($this->obj); - } + $meta?->attachTo($this->obj); } /** * @param object $o * @internal */ - public function attachTo($o): void - { + public function attachTo(object $o): void { $o->jsonapi = $this->obj; } } diff --git a/src/Link/AboutLink.php b/src/Link/AboutLink.php index e317ca8..fdafefd 100644 --- a/src/Link/AboutLink.php +++ b/src/Link/AboutLink.php @@ -1,21 +1,22 @@ -about = $this->link; } } diff --git a/src/Link/FirstLink.php b/src/Link/FirstLink.php index af13251..dfc5b6a 100644 --- a/src/Link/FirstLink.php +++ b/src/Link/FirstLink.php @@ -1,21 +1,22 @@ -first = $this->link; } } diff --git a/src/Link/LastLink.php b/src/Link/LastLink.php index 037b228..a2215b8 100644 --- a/src/Link/LastLink.php +++ b/src/Link/LastLink.php @@ -1,21 +1,22 @@ -last = $this->link; } } diff --git a/src/Link/NextLink.php b/src/Link/NextLink.php index c2ba366..082fbcc 100644 --- a/src/Link/NextLink.php +++ b/src/Link/NextLink.php @@ -1,21 +1,22 @@ -next = $this->link; } } diff --git a/src/Link/PrevLink.php b/src/Link/PrevLink.php index b612450..d38d025 100644 --- a/src/Link/PrevLink.php +++ b/src/Link/PrevLink.php @@ -1,21 +1,22 @@ -prev = $this->link; } } diff --git a/src/Link/RelatedLink.php b/src/Link/RelatedLink.php index 1def33c..501c2ec 100644 --- a/src/Link/RelatedLink.php +++ b/src/Link/RelatedLink.php @@ -1,22 +1,23 @@ -related = $this->link; } } diff --git a/src/Link/SelfLink.php b/src/Link/SelfLink.php index fde232b..0168cb1 100644 --- a/src/Link/SelfLink.php +++ b/src/Link/SelfLink.php @@ -1,23 +1,24 @@ -self = $this->link; } } diff --git a/src/Meta.php b/src/Meta.php index 758c3bb..ebec4dc 100644 --- a/src/Meta.php +++ b/src/Meta.php @@ -1,7 +1,10 @@ -key = $key; - $this->value = $value; - } - /** - * @param object $o - * @internal - */ - public function attachTo($o): void - { - child($o, 'meta')->{$this->key} = $this->value; + /** + * @param object $o + * @internal + */ + public function attachTo(object $o): void { + child($o, 'meta')->{$this->key} = $this->value; + } } -} diff --git a/src/MetaDocument.php b/src/MetaDocument.php index 45199c9..14f0334 100644 --- a/src/MetaDocument.php +++ b/src/MetaDocument.php @@ -1,21 +1,20 @@ -doc = combine($meta, ...$members); } - #[\ReturnTypeWillChange] - public function jsonSerialize() - { + public function jsonSerialize(): object { return $this->doc; } } diff --git a/src/NewResourceObject.php b/src/NewResourceObject.php index 0f54e5b..44364f1 100644 --- a/src/NewResourceObject.php +++ b/src/NewResourceObject.php @@ -1,4 +1,6 @@ -data = null; } - - public function registerIn(array &$registry): void - { - } } diff --git a/src/PaginatedCollection.php b/src/PaginatedCollection.php index e59c66f..ffc9050 100644 --- a/src/PaginatedCollection.php +++ b/src/PaginatedCollection.php @@ -1,43 +1,22 @@ -pagination = $pagination; - $this->collection = $collection; +final class PaginatedCollection implements PrimaryData { + public function __construct(private readonly Pagination $pagination, private readonly Collection $collection) { } /** * @param object $o * @internal */ - public function attachTo($o): void - { + public function attachTo(object $o): void { $this->collection->attachTo($o); $this->pagination->attachTo($o); } - - /** - * @param array $registry - * @internal - */ - public function registerIn(array &$registry): void - { - $this->collection->registerIn($registry); - } } diff --git a/src/Pagination.php b/src/Pagination.php index 674f375..dc2c642 100644 --- a/src/Pagination.php +++ b/src/Pagination.php @@ -1,4 +1,5 @@ links = $links; } @@ -22,8 +21,7 @@ public function __construct(PaginationLink ...$links) * @param object $o * @internal */ - public function attachTo($o): void - { + 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 f337ad5..592dd21 100644 --- a/src/ResourceCollection.php +++ b/src/ResourceCollection.php @@ -1,19 +1,19 @@ -resources = $resources; } @@ -21,18 +21,10 @@ public function __construct(ResourceObject ...$resources) * @param object $o * @internal */ - public function attachTo($o): void - { + public function attachTo(object $o): void { $o->data = []; foreach ($this->resources as $resource) { $resource->attachToCollection($o); } } - - 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 dff34f8..cf1cf88 100644 --- a/src/ResourceIdentifier.php +++ b/src/ResourceIdentifier.php @@ -1,44 +1,33 @@ -obj = (object) [ + $this->obj = (object)[ 'type' => $type, 'id' => $id, ]; foreach ($metas as $meta) { $meta->attachTo($this->obj); } - $this->type = $type; - $this->id = $id; } /** * @param object $o * @internal */ - public function attachTo($o): void - { + public function attachTo(object $o): void { $o->data = $this->obj; } @@ -46,13 +35,7 @@ public function attachTo($o): void * @param object $o * @internal */ - public function attachToCollection($o): void - { + public function attachToCollection(object $o): void { $o->data[] = $this->obj; } - - public function registerIn(array &$registry): void - { - $registry[compositeKey($this->type, $this->id)] = true; - } } diff --git a/src/ResourceIdentifierCollection.php b/src/ResourceIdentifierCollection.php index 84b8887..794789e 100644 --- a/src/ResourceIdentifierCollection.php +++ b/src/ResourceIdentifierCollection.php @@ -1,19 +1,19 @@ -identifiers = $identifiers; } @@ -21,18 +21,10 @@ public function __construct(ResourceIdentifier ...$identifiers) * @param object $o * @internal */ - public function attachTo($o): void - { + public function attachTo(object $o): void { $o->data = []; foreach ($this->identifiers as $identifier) { $identifier->attachToCollection($o); } } - - 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 416d423..f65d575 100644 --- a/src/ResourceObject.php +++ b/src/ResourceObject.php @@ -1,4 +1,6 @@ -obj->id = $id; $this->type = $type; - $this->id = $id; } - public function identifier(): ResourceIdentifier - { + public function identifier(): ResourceIdentifier { return new ResourceIdentifier($this->type, $this->id); } - public function key(): string - { + public function key(): string { return compositeKey($this->type, $this->id); } - public function registerIn(array &$registry): void - { - $registry = array_merge($registry, $this->registry); - } - /** * @param object $o * @internal */ - public function attachTo($o): void - { + public function attachTo(object $o): void { $o->data = $this->obj; } /** * @param object $o */ - public function attachAsIncludedTo($o): void - { + public function attachAsIncludedTo(object $o): void { $o->included[] = $this->obj; } @@ -57,13 +42,11 @@ public function attachAsIncludedTo($o): void * @param object $o * @internal */ - public function attachToCollection($o): void - { + public function attachToCollection(object $o): void { $o->data[] = $this->obj; } - public function __toString(): string - { + public function __toString(): string { return $this->key(); } } diff --git a/src/ToMany.php b/src/ToMany.php index c0a7278..afe4987 100644 --- a/src/ToMany.php +++ b/src/ToMany.php @@ -1,4 +1,6 @@ -validateFieldName($name); $this->name = $name; $this->members = $members; - $this->collection = $collection; } /** * @param object $o * @internal */ - public function attachTo($o): void - { + public function attachTo(object $o): void { $rel = child(child($o, 'relationships'), $this->name); $rel->data = []; $this->collection->attachTo($rel); @@ -40,9 +39,4 @@ public function attachTo($o): void $member->attachTo($rel); } } - - public function registerIn(array &$registry): void - { - $this->collection->registerIn($registry); - } } diff --git a/src/ToNull.php b/src/ToNull.php index 8b2d3f1..cbaec93 100644 --- a/src/ToNull.php +++ b/src/ToNull.php @@ -1,4 +1,6 @@ -validateFieldName($name); $this->name = $name; $this->members = $members; @@ -25,8 +26,7 @@ public function __construct(string $name, ToOneMember ...$members) * @param object $o * @internal */ - public function attachTo($o): void - { + public function attachTo(object $o): void { $obj = combine(...$this->members); $obj->data = null; child($o, 'relationships')->{$this->name} = $obj; diff --git a/src/ToOne.php b/src/ToOne.php index dbbd7ac..eb86853 100644 --- a/src/ToOne.php +++ b/src/ToOne.php @@ -1,4 +1,6 @@ -validateFieldName($name); $this->name = $name; $this->obj = combine($identifier, ...$members); - $this->identifier = $identifier; } /** * @param object $o * @internal */ - public function attachTo($o): void - { + public function attachTo($o): void { child($o, 'relationships')->{$this->name} = $this->obj; } - - public function registerIn(array &$registry): void - { - $this->identifier->registerIn($registry); - } } diff --git a/src/functions.php b/src/functions.php index df50e2d..5a08291 100644 --- a/src/functions.php +++ b/src/functions.php @@ -1,32 +1,30 @@ -attachTo($obj); } return $obj; } -function child($o, string $name) -{ +function child($o, string $name): mixed { if (!isset($o->{$name})) { - $o->{$name} = (object) []; + $o->{$name} = (object)[]; } return $o->{$name}; } -function isValidName(string $name): bool -{ +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 -{ +function compositeKey(string $type, string $id): string { return "{$type}:{$id}"; } diff --git a/test/BaseTestCase.php b/test/BaseTestCase.php index eca3598..e649b2c 100644 --- a/test/BaseTestCase.php +++ b/test/BaseTestCase.php @@ -1,13 +1,13 @@ -expectException(\LogicException::class); - $this->expectExceptionMessage('Full linkage required for apples:1'); - $create_doc(); - } - - public function documentsWithoutFullLinkage(): array - { - $included = new Included(new ResourceObject('apples', '1')); - return [ - [ - function () use ($included) { - return new CompoundDocument(new NullData(), $included); - }, - ], - [ - function () use ($included) { - return new CompoundDocument(new ResourceCollection(), $included); - }, - ], - [ - function () use ($included) { - return new CompoundDocument(new ResourceIdentifier('oranges', '1'), $included); - }, - ], - [ - function () use ($included) { - return new CompoundDocument( - new ResourceIdentifierCollection( - new ResourceIdentifier('oranges', '1'), - new ResourceIdentifier('oranges', '1') - ), - $included - ); - }, - ], - [ - function () use ($included) { - return new CompoundDocument( - new ResourceCollection(new ResourceObject('oranges', '1'), new ResourceObject('oranges', '1')), - $included - ); - }, - ], - ]; - } - - public function testIncludedResourceMayBeIdentifiedByLinkageInPrimaryData() - { + public function testIncludedResourceMayBeIdentifiedByLinkageInPrimaryData() { $author = new ResourceObject('people', '9'); $article = new ResourceObject( 'articles', @@ -233,8 +175,7 @@ public function testIncludedResourceMayBeIdentifiedByLinkageInPrimaryData() $this->assertNotEmpty($doc); } - public function testIncludedResourceMayBeIdentifiedByAnotherLinkedResource() - { + public function testIncludedResourceMayBeIdentifiedByAnotherLinkedResource() { $writer = new ResourceObject('writers', '3', new Attribute('name', 'Eric Evans')); $book = new ResourceObject( 'books', @@ -254,8 +195,7 @@ public function testIncludedResourceMayBeIdentifiedByAnotherLinkedResource() /** * A compound document MUST NOT include more than one resource object for each type and id pair. */ - public function testCanNotBeManyIncludedResourcesWithEqualIdentifiers() - { + public function testCanNotBeManyIncludedResourcesWithEqualIdentifiers() { $this->expectException('LogicException'); $this->expectExceptionMessage('Resource apples:1 is already included'); $apple = new ResourceObject('apples', '1'); diff --git a/test/DataDocument/ManyResourceIdentifiersTest.php b/test/DataDocument/ManyResourceIdentifiersTest.php index eb12973..2d4728d 100644 --- a/test/DataDocument/ManyResourceIdentifiersTest.php +++ b/test/DataDocument/ManyResourceIdentifiersTest.php @@ -1,4 +1,6 @@ -assertEncodesTo( ' { @@ -26,8 +26,7 @@ public function testMinimalDocument() ); } - public function testExtendedDocument() - { + public function testExtendedDocument() { $this->assertEncodesTo( ' { diff --git a/test/DataDocument/ManyResourceObjectsTest.php b/test/DataDocument/ManyResourceObjectsTest.php index 54736e2..a555e0c 100644 --- a/test/DataDocument/ManyResourceObjectsTest.php +++ b/test/DataDocument/ManyResourceObjectsTest.php @@ -1,4 +1,6 @@ -assertEncodesTo( ' { @@ -28,8 +28,7 @@ public function testMinimalDocument() ); } - public function testExtendedDocument() - { + public function testExtendedDocument() { $this->assertEncodesTo( ' { diff --git a/test/DataDocument/NullDataTest.php b/test/DataDocument/NullDataTest.php index 05746cd..4bb759d 100644 --- a/test/DataDocument/NullDataTest.php +++ b/test/DataDocument/NullDataTest.php @@ -1,4 +1,5 @@ assertEncodesTo( ' { @@ -26,8 +25,7 @@ public function testMinimalDocument() ); } - public function testExtendedDocument() - { + public function testExtendedDocument() { $this->assertEncodesTo( ' { diff --git a/test/DataDocument/SingleResourceIdentifierTest.php b/test/DataDocument/SingleResourceIdentifierTest.php index 165a625..3eea202 100644 --- a/test/DataDocument/SingleResourceIdentifierTest.php +++ b/test/DataDocument/SingleResourceIdentifierTest.php @@ -1,4 +1,6 @@ -assertEncodesTo( ' { @@ -29,8 +29,7 @@ public function testMinimalDocument() ); } - public function testExtendedDocument() - { + public function testExtendedDocument() { $this->assertEncodesTo( ' { diff --git a/test/DataDocument/SingleResourceObjectTest.php b/test/DataDocument/SingleResourceObjectTest.php index dfa25a1..0d19dde 100644 --- a/test/DataDocument/SingleResourceObjectTest.php +++ b/test/DataDocument/SingleResourceObjectTest.php @@ -1,4 +1,6 @@ -assertEncodesTo( ' { @@ -29,8 +29,7 @@ public function testMinimalDocument() ); } - public function testExtendedDocument() - { + public function testExtendedDocument() { $this->assertEncodesTo( ' { diff --git a/test/ErrorDocumentTest.php b/test/ErrorDocumentTest.php index 9a9131d..cb0bc4c 100644 --- a/test/ErrorDocumentTest.php +++ b/test/ErrorDocumentTest.php @@ -1,4 +1,5 @@ assertEncodesTo( ' { @@ -36,8 +35,7 @@ public function testMinimalExample() ); } - public function testExtensiveExample() - { + public function testExtensiveExample() { $this->assertEncodesTo( ' { @@ -82,8 +80,7 @@ public function testExtensiveExample() ); } - public function testMultipleErrors() - { + public function testMultipleErrors() { $this->assertEncodesTo( ' { diff --git a/test/ExamplesTest.php b/test/ExamplesTest.php index ba7a1cf..7dc5da4 100644 --- a/test/ExamplesTest.php +++ b/test/ExamplesTest.php @@ -1,23 +1,21 @@ assertJson(`php $file`); } - public function examples() - { + public function examples() { return [ [__DIR__.'/../examples/compound_doc.php'], [__DIR__.'/../examples/simple_doc.php'], diff --git a/test/JsonApiTest.php b/test/JsonApiTest.php index 49c5982..8a5ce91 100644 --- a/test/JsonApiTest.php +++ b/test/JsonApiTest.php @@ -1,4 +1,6 @@ -assertEncodesTo( ' { diff --git a/test/LinkObjectTest.php b/test/LinkObjectTest.php index a69443c..00a9a78 100644 --- a/test/LinkObjectTest.php +++ b/test/LinkObjectTest.php @@ -1,4 +1,6 @@ -assertEncodesTo( '{ "data": {"type": "apples", "id": "1"}, diff --git a/test/MetaDocumentTest.php b/test/MetaDocumentTest.php index 7893dae..1837f3e 100644 --- a/test/MetaDocumentTest.php +++ b/test/MetaDocumentTest.php @@ -1,4 +1,5 @@ assertEncodesTo( ' { @@ -29,8 +28,7 @@ public function testMetaDocument() /** * A meta document may contain jsonapi member */ - public function testMetaDocumentWithExtraMembers() - { + public function testMetaDocumentWithExtraMembers() { $this->assertEncodesTo( ' { diff --git a/test/MetaTest.php b/test/MetaTest.php index 67d3097..c8d8d93 100644 --- a/test/MetaTest.php +++ b/test/MetaTest.php @@ -1,14 +1,14 @@ -expectException(\DomainException::class); $this->expectExceptionMessage("Invalid character in a member name 'invalid:name'"); new Meta('invalid:name', '1'); diff --git a/test/NewResourceObjectTest.php b/test/NewResourceObjectTest.php index 6182ad9..a2fc94e 100644 --- a/test/NewResourceObjectTest.php +++ b/test/NewResourceObjectTest.php @@ -1,4 +1,6 @@ -assertEncodesTo( ' { @@ -51,8 +51,7 @@ public function testFullFledgedResourceObject() ); } - public function testRelationshipWithSingleIdLinkage() - { + public function testRelationshipWithSingleIdLinkage() { $this->assertEncodesTo( ' { @@ -75,8 +74,7 @@ public function testRelationshipWithSingleIdLinkage() ); } - public function testRelationshipWithMultiIdLinkage() - { + public function testRelationshipWithMultiIdLinkage() { $this->assertEncodesTo( ' { @@ -111,8 +109,7 @@ public function testRelationshipWithMultiIdLinkage() ); } - public function testRelationshipWithEmptyMultiIdLinkage() - { + public function testRelationshipWithEmptyMultiIdLinkage() { $this->assertEncodesTo( ' { @@ -135,8 +132,7 @@ public function testRelationshipWithEmptyMultiIdLinkage() ); } - public function testRelationshipWithNoData() - { + public function testRelationshipWithNoData() { $this->assertEncodesTo( ' { @@ -188,8 +184,7 @@ public function testRelationshipWithNoData() ); } - public function testResourceFieldsMustBeUnique() - { + public function testResourceFieldsMustBeUnique() { $this->expectException(\LogicException::class); $this->expectExceptionMessage("Field 'foo' already exists"); new NewResourceObject( @@ -199,8 +194,7 @@ public function testResourceFieldsMustBeUnique() ); } - public function testNameValidation() - { + public function testNameValidation() { $this->expectException(\DomainException::class); new NewResourceObject('invalid:id'); } diff --git a/test/PaginationTest.php b/test/PaginationTest.php index 7add66f..a9307d8 100644 --- a/test/PaginationTest.php +++ b/test/PaginationTest.php @@ -1,4 +1,6 @@ -assertEncodesTo( ' { @@ -57,8 +57,7 @@ public function testPaginatedResourceCollection() ); } - public function testPaginatedResourceIdentifierCollection() - { + public function testPaginatedResourceIdentifierCollection() { $this->assertEncodesTo( ' { diff --git a/test/ResourceIdentifierTest.php b/test/ResourceIdentifierTest.php index 5dcd959..eb479ee 100644 --- a/test/ResourceIdentifierTest.php +++ b/test/ResourceIdentifierTest.php @@ -1,14 +1,13 @@ expectException(\DomainException::class); new ResourceIdentifier('invalid:id', 'foo'); } diff --git a/test/ResourceObjectTest.php b/test/ResourceObjectTest.php index 3541c8d..b565d7d 100644 --- a/test/ResourceObjectTest.php +++ b/test/ResourceObjectTest.php @@ -1,4 +1,6 @@ -assertEncodesTo( ' { @@ -63,8 +63,7 @@ public function testFullFledgedResourceObject() ); } - public function testRelationshipWithSingleIdLinkage() - { + public function testRelationshipWithSingleIdLinkage() { $this->assertEncodesTo( ' { @@ -89,8 +88,7 @@ public function testRelationshipWithSingleIdLinkage() ); } - public function testRelationshipWithMultiIdLinkage() - { + public function testRelationshipWithMultiIdLinkage() { $this->assertEncodesTo( ' { @@ -127,8 +125,7 @@ public function testRelationshipWithMultiIdLinkage() ); } - public function testRelationshipWithEmptyMultiIdLinkage() - { + public function testRelationshipWithEmptyMultiIdLinkage() { $this->assertEncodesTo( ' { @@ -153,8 +150,7 @@ public function testRelationshipWithEmptyMultiIdLinkage() ); } - public function testRelationshipWithNoData() - { + public function testRelationshipWithNoData() { $this->assertEncodesTo( ' { @@ -210,29 +206,25 @@ public function testRelationshipWithNoData() ); } - public function testCanNotCreateIdAttribute() - { + public function testCanNotCreateIdAttribute() { $this->expectException(\DomainException::class); $this->expectExceptionMessage("Can not use 'id' as a resource field"); new Attribute('id', 'foo'); } - public function testCanNotCreateTypeAttribute() - { + public function testCanNotCreateTypeAttribute() { $this->expectException(\DomainException::class); $this->expectExceptionMessage("Can not use 'type' as a resource field"); new Attribute('type', 'foo'); } - public function testCanNotCreateIdRelationship() - { + public function testCanNotCreateIdRelationship() { $this->expectException(\DomainException::class); $this->expectExceptionMessage("Can not use 'id' as a resource field"); new ToOne('id', new ResourceIdentifier('apples', '1')); } - public function testCanNotCreateTypeRelationship() - { + public function testCanNotCreateTypeRelationship() { $this->expectException(\DomainException::class); $this->expectExceptionMessage("Can not use 'type' as a resource field"); new ToOne('type', new ResourceIdentifier('apples', '1')); @@ -242,8 +234,7 @@ public function testCanNotCreateTypeRelationship() * @dataProvider invalidCharacters * @param string $invalid_char */ - public function testAttributeMustOnlyHaveAllowedCharacters(string $invalid_char) - { + public function testAttributeMustOnlyHaveAllowedCharacters(string $invalid_char) { $this->expectException(\DomainException::class); $this->expectExceptionMessage('Invalid character in a member name'); new Attribute("foo{$invalid_char}bar", 'plus can not be used'); @@ -253,15 +244,13 @@ public function testAttributeMustOnlyHaveAllowedCharacters(string $invalid_char) * @dataProvider invalidCharacters * @param string $invalid_char */ - public function testRelationshipMustOnlyHaveAllowedCharacters(string $invalid_char) - { + public function testRelationshipMustOnlyHaveAllowedCharacters(string $invalid_char) { $this->expectException(\DomainException::class); $this->expectExceptionMessage('Invalid character in a member name'); new ToNull("foo{$invalid_char}bar"); } - public function invalidCharacters() - { + public function invalidCharacters() { return [ ['+'], ['!'], @@ -271,8 +260,7 @@ public function invalidCharacters() ]; } - public function testResourceFieldsMustBeUnique() - { + public function testResourceFieldsMustBeUnique() { $this->expectException(\LogicException::class); $this->expectExceptionMessage("Field 'foo' already exists"); new ResourceObject( @@ -283,8 +271,7 @@ public function testResourceFieldsMustBeUnique() ); } - public function testNameValidation() - { + public function testNameValidation() { $this->expectException(\DomainException::class); new ResourceObject('invalid:id', 'foo'); } diff --git a/test/benchmarks/compound10k.php b/test/benchmarks/compound10k.php index 513518b..44664fd 100644 --- a/test/benchmarks/compound10k.php +++ b/test/benchmarks/compound10k.php @@ -1,4 +1,6 @@ - Date: Tue, 26 Jul 2022 17:48:27 -0700 Subject: [PATCH 2/2] v3 --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index f22a32d..c195581 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ composer.phar /vendor/ /composer.lock /.php_cs.cache +.php-cs-fixer.cache +.phpunit.result.cache