Skip to content

Commit

Permalink
Add more accessors and mutators for ResourceObject
Browse files Browse the repository at this point in the history
  • Loading branch information
kocsismate committed Mar 26, 2019
1 parent 1fcf20d commit fb53960
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

ADDED:

- New accessor and mutator methods for `WoohooLabs\Yang\JsonApi\Request\ResourceObject`: `id()`, `setId()`, `type()`, `setType()`,
`attributes()`, `relationships()`

CHANGED:

REMOVED:
Expand Down
37 changes: 37 additions & 0 deletions src/JsonApi/Request/ResourceObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,35 @@ public function __construct(string $type, string $id = "")
$this->relationships = [];
}

public function type(): string
{
return $this->type;
}

public function setType(string $type): ResourceObject

This comment has been minimized.

Copy link
@holtkamp

holtkamp Mar 26, 2019

Contributor

Is there a specific reason you use ResourceObject as return type and not self?

This comment has been minimized.

Copy link
@kocsismate

kocsismate Mar 26, 2019

Author Member

No, there is no specific reason, only this is what I am used to.

This comment has been minimized.

Copy link
@holtkamp

holtkamp Mar 26, 2019

Contributor

ok, thanks. Functionally it is the same I guess... In case of a refactor / change of class name, using self would reduce the number of changes...

Was just wondering 😄

{
$this->type = $type;

return $this;
}

public function id(): string
{
return $this->id;
}

public function setId(string $id): ResourceObject
{
$this->id = $id;

return $this;
}

public function attributes(): array
{
return $this->attributes;
}

public function setAttributes(array $attributes): ResourceObject
{
$this->attributes = $attributes;
Expand All @@ -55,6 +84,14 @@ public function setAttribute(string $name, $value): ResourceObject
return $this;
}

/**
* @return RelationshipInterface[]
*/
public function relationships(): array
{
return $this->relationships;
}

public function setToOneRelationship(string $name, ToOneRelationship $relationship): ResourceObject
{
return $this->setRelationship($name, $relationship);
Expand Down
104 changes: 102 additions & 2 deletions tests/JsonApi/Request/ResourceObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,19 @@ public function toArray()
/**
* @test
*/
public function getType()
public function type()
{
$resource = new ResourceObject("a");

$type = $resource->type();

$this->assertSame("a", $type);
}

/**
* @test
*/
public function typeToArray()
{
$resource = new ResourceObject("a");

Expand All @@ -58,7 +70,38 @@ public function getType()
/**
* @test
*/
public function getId()
public function setType()
{
$resource = new ResourceObject("");

$resource->setType("a");

$this->assertSame(
[
"data" => [
"type" => "a",
],
],
$resource->toArray()
);
}

/**
* @test
*/
public function id()
{
$resource = new ResourceObject("", "1");

$id = $resource->id();

$this->assertSame("1", $id);
}

/**
* @test
*/
public function idToArray()
{
$resource = new ResourceObject("", "0");

Expand All @@ -73,6 +116,44 @@ public function getId()
);
}

/**
* @test
*/
public function setId()
{
$resource = new ResourceObject("");

$resource->setId("0");

$this->assertSame(
[
"data" => [
"type" => "",
"id" => "0",
],
],
$resource->toArray()
);
}

/**
* @test
*/
public function attributes()
{
$resource = new ResourceObject("", "");
$resource
->setAttributes(["a" => "b", "c" => "d"]);

$this->assertSame(
[
"a" => "b",
"c" => "d",
],
$resource->attributes()
);
}

/**
* @test
*/
Expand Down Expand Up @@ -120,6 +201,25 @@ public function setAttribute()
);
}

/**
* @test
*/
public function relationships()
{
$resource = new ResourceObject("", "");
$resource
->setToOneRelationship("a", ToOneRelationship::create("", ""))
->setToManyRelationship("b", ToManyRelationship::create());

$this->assertEquals(
[
"a" => ToOneRelationship::create("", ""),
"b" => ToManyRelationship::create(),
],
$resource->relationships()
);
}

/**
* @test
*/
Expand Down

0 comments on commit fb53960

Please sign in to comment.