Skip to content

Commit

Permalink
Add user data value objects JSON serialize support
Browse files Browse the repository at this point in the history
  • Loading branch information
Zorato committed Sep 27, 2017
1 parent 79fa49d commit 274d3b0
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
12 changes: 12 additions & 0 deletions spec/UserDataItemSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace spec\Blockvis\Civic\Sip;

use Blockvis\Civic\Sip\UserDataItem;
use JsonSerializable;
use PhpSpec\ObjectBehavior;

class UserDataItemSpec extends ObjectBehavior
Expand All @@ -17,6 +18,17 @@ function it_is_initializable()
$this->shouldHaveType(UserDataItem::class);
}

function it_is_json_serializable()
{
$this->shouldImplement(JsonSerializable::class);
assert(json_encode($this->getWrappedObject()) === json_encode([
'label' => 'label',
'value' => 'value',
'isValid' => true,
'isOwner' => false,
]), 'JSON representation');
}

public function it_is_readable()
{
$this->label()->shouldBe('label');
Expand Down
10 changes: 10 additions & 0 deletions spec/UserDataSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace spec\Blockvis\Civic\Sip;

use Blockvis\Civic\Sip\UserData;
use JsonSerializable;
use PhpSpec\ObjectBehavior;

class UserDataSpec extends ObjectBehavior
Expand All @@ -22,6 +23,15 @@ function it_is_initializable()
$this->shouldHaveType(UserData::class);
}

function it_is_json_serializable()
{
$this->shouldImplement(JsonSerializable::class);
assert(json_encode($this->getWrappedObject()) === json_encode([
['label' => 'label1', 'value' => 'value1', 'isValid' => true, 'isOwner' => true],
['label' => 'label2', 'value' => 'value2', 'isValid' => true, 'isOwner' => false],
]), 'JSON representation');
}

function it_returns_data_items()
{
$items = $this->items();
Expand Down
13 changes: 12 additions & 1 deletion src/UserData.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Blockvis\Civic\Sip;

class UserData
use JsonSerializable;

class UserData implements JsonSerializable
{
/**
* @var UserDataItem[]
Expand Down Expand Up @@ -47,6 +49,14 @@ public function items(): array
return array_values($this->items);
}

/**
* @return array|UserDataItem[]
*/
public function jsonSerialize()
{
return $this->items();
}

/**
* Returns the user id.
*
Expand Down Expand Up @@ -77,4 +87,5 @@ private function createDataItems(array $data): array

return $items;
}

}
17 changes: 16 additions & 1 deletion src/UserDataItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Blockvis\Civic\Sip;

class UserDataItem
use JsonSerializable;

class UserDataItem implements JsonSerializable
{
/**
* Civic SIP service challenges the user during scope request approval to ensure
Expand Down Expand Up @@ -71,6 +73,19 @@ public function isValid(): bool
return $this->isValid;
}

/**
* @return array
*/
public function jsonSerialize()
{
return [
'label' => $this->label,
'value' => $this->value,
'isValid' => $this->isValid,
'isOwner' => $this->isOwner,
];
}

/**
* Returns the item label.
*
Expand Down

0 comments on commit 274d3b0

Please sign in to comment.