Skip to content

Commit

Permalink
Merge pull request #51 from swisnl/feature/magic-methods-for-id
Browse files Browse the repository at this point in the history
Explicit type hint and magic methods for id
  • Loading branch information
JaZo authored Jul 1, 2019
2 parents ba7fd99 + 2dd8d60 commit 695cd08
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 21 deletions.
8 changes: 4 additions & 4 deletions src/Interfaces/ItemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
interface ItemInterface extends DataInterface
{
/**
* @return string
* @return string|null
*/
public function getId();
public function getId(): ? string;

/**
* @return bool
Expand All @@ -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
Expand Down
46 changes: 41 additions & 5 deletions src/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@
use Swis\JsonApi\Client\Traits\HasMeta;
use Swis\JsonApi\Client\Traits\HasType;

/**
* @property string|null id
*/
class Item extends Model implements ItemInterface
{
use HasLinks;
use HasMeta;
use HasType;

/**
* @var
* @var string|null
*/
protected $id;

Expand Down Expand Up @@ -103,19 +106,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;

Expand Down Expand Up @@ -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
*
Expand All @@ -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.
*
Expand Down Expand Up @@ -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));
}

Expand Down
2 changes: 1 addition & 1 deletion tests/ItemRelationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function it_can_get_all_relations()
'child' => [
'data' => [
'type' => 'child',
'id' => 1,
'id' => '1',
],
],
], $relations);
Expand Down
46 changes: 35 additions & 11 deletions tests/ItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -103,6 +93,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
*/
Expand Down

0 comments on commit 695cd08

Please sign in to comment.