Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicit type hint and magic methods for id #51

Merged
merged 3 commits into from
Jul 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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