From 3622c5df08f57da0de294c92010015412b8d7541 Mon Sep 17 00:00:00 2001 From: Jasper Zonneveld Date: Mon, 1 Jul 2019 11:59:34 +0200 Subject: [PATCH 1/3] Add parameter hints for id According to the specification, the id must always be a string when present. See: https://jsonapi.org/format/#document-resource-object-identification --- src/Interfaces/ItemInterface.php | 8 ++++---- src/Item.php | 10 +++++----- tests/ItemRelationsTest.php | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Interfaces/ItemInterface.php b/src/Interfaces/ItemInterface.php index 18924d0..8e2fa14 100644 --- a/src/Interfaces/ItemInterface.php +++ b/src/Interfaces/ItemInterface.php @@ -8,9 +8,9 @@ interface ItemInterface extends DataInterface { /** - * @return string + * @return string|null */ - public function getId(); + public function getId(): ? string; /** * @return bool @@ -23,11 +23,11 @@ public function hasId(): bool; public function isNew(): bool; /** - * @param string $id + * @param string|null $id * * @return static */ - public function setId($id); + public function setId(? string $id); /** * @return string diff --git a/src/Item.php b/src/Item.php index a9ea87d..3f0f800 100644 --- a/src/Item.php +++ b/src/Item.php @@ -22,7 +22,7 @@ class Item extends Model implements ItemInterface use HasType; /** - * @var + * @var string|null */ protected $id; @@ -103,19 +103,19 @@ public function hasId(): bool } /** - * @return string + * @return string|null */ - public function getId() + public function getId(): ? string { return $this->id; } /** - * @param string $id + * @param string|null $id * * @return static */ - public function setId($id) + public function setId(? string $id) { $this->id = $id; diff --git a/tests/ItemRelationsTest.php b/tests/ItemRelationsTest.php index 3421190..009884f 100644 --- a/tests/ItemRelationsTest.php +++ b/tests/ItemRelationsTest.php @@ -37,7 +37,7 @@ public function it_can_get_all_relations() 'child' => [ 'data' => [ 'type' => 'child', - 'id' => 1, + 'id' => '1', ], ], ], $relations); From 62d793268810c85fdc64f1270888a434462f9966 Mon Sep 17 00:00:00 2001 From: Jasper Zonneveld Date: Mon, 1 Jul 2019 11:49:08 +0200 Subject: [PATCH 2/3] Allow id to be set/get using magic methods This allows you to set/get the id just like any other attribute. This is useful for example if you want to pluck all ids of a collection of items. --- src/Item.php | 36 ++++++++++++++++++++++++++++++++++++ tests/ItemTest.php | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/src/Item.php b/src/Item.php index 3f0f800..c82ef38 100644 --- a/src/Item.php +++ b/src/Item.php @@ -15,6 +15,9 @@ use Swis\JsonApi\Client\Traits\HasMeta; use Swis\JsonApi\Client\Traits\HasType; +/** + * @property string|null id + */ class Item extends Model implements ItemInterface { use HasLinks; @@ -219,6 +222,20 @@ public function canBeIncluded(): bool return true; } + /** + * @param string $key + * + * @return mixed + */ + public function __get($key) + { + if ($key === 'id') { + return $this->getId(); + } + + return parent::__get($key); + } + /** * @param string $key * @@ -243,6 +260,21 @@ public function hasAttribute($key): bool return array_key_exists($key, $this->attributes); } + /** + * @param string $key + * @param mixed $value + */ + public function __set($key, $value) + { + if ($key === 'id') { + $this->setId($value); + + return; + } + + parent::__set($key, $value); + } + /** * Get the relationship data. * @@ -277,6 +309,10 @@ public function getRelationValue($key) */ public function __isset($key) { + if ($key === 'id') { + return $this->hasId(); + } + return parent::__isset($key) || $this->hasRelationship($key) || $this->hasRelationship(snake_case($key)); } diff --git a/tests/ItemTest.php b/tests/ItemTest.php index abca9d6..6532c84 100644 --- a/tests/ItemTest.php +++ b/tests/ItemTest.php @@ -103,6 +103,40 @@ public function it_returns_id_when_id_isset() $this->assertEquals(1234, $item->getId()); } + /** + * @test + */ + public function it_can_set_the_id_using_the_magic_method() + { + $item = new Item(); + + $item->id = 1234; + $this->assertEquals(1234, $item->getId()); + } + + /** + * @test + */ + public function it_can_get_the_id_using_the_magic_method() + { + $item = new Item(); + $item->setId(1234); + + $this->assertEquals(1234, $item->id); + } + + /** + * @test + */ + public function it_can_check_if_the_id_is_set_using_the_magic_method() + { + $item = new Item(); + + $this->assertFalse(isset($item->id)); + $item->setId(1234); + $this->assertTrue(isset($item->id)); + } + /** * @test */ From 2dd8d60b95444e90ee9373fa121cea13a5dac197 Mon Sep 17 00:00:00 2001 From: Jasper Zonneveld Date: Mon, 1 Jul 2019 11:49:59 +0200 Subject: [PATCH 3/3] Remove useless test constructor --- tests/ItemTest.php | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/tests/ItemTest.php b/tests/ItemTest.php index 6532c84..59c9f16 100644 --- a/tests/ItemTest.php +++ b/tests/ItemTest.php @@ -14,17 +14,7 @@ class ItemTest extends AbstractTest { - protected $attributes; - - /** - * ItemTest constructor. - */ - public function __construct() - { - $this->attributes = ['testKey' => 'testValue']; - - parent::__construct(); - } + protected $attributes = ['testKey' => 'testValue']; /** * @test