Skip to content

Commit

Permalink
make json serializable
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangut committed Sep 18, 2018
1 parent 1f57f3b commit cfb1cdd
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"prefer-stable": true,
"require": {
"php": "^7.1",
"ext-json": "*",
"phpgears/immutability": "0.1",
"ramsey/uuid": "^3.7"
},
Expand Down
8 changes: 8 additions & 0 deletions src/AbstractIdentity.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ final public function __toString(): string
return $this->value;
}

/**
* {@inheritdoc}
*/
final public function jsonSerialize(): string
{
return $this->value;
}

/**
* {@inheritdoc}
*
Expand Down
2 changes: 1 addition & 1 deletion src/Identity.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/**
* Identity value object interface.
*/
interface Identity
interface Identity extends \JsonSerializable
{
/**
* Get identity from string.
Expand Down
1 change: 1 addition & 0 deletions tests/Identity/AbstractIdentityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function testCreation(): void

$this->assertSame('thisIsMyId', $stub->getValue());
$this->assertSame('thisIsMyId', (string) $stub);
$this->assertSame('"thisIsMyId"', \json_encode($stub));
}

public function testEquality(): void
Expand Down
2 changes: 2 additions & 0 deletions tests/Identity/UuidIdentityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public function testCreation(): void
$stub = UuidIdentity::fromString($uuid);

$this->assertSame($uuid, $stub->getValue());
$this->assertSame('4c4316cb-b48b-44fb-a034-90d789966bac', (string) $stub);
$this->assertSame('"4c4316cb-b48b-44fb-a034-90d789966bac"', \json_encode($stub));
}

/**
Expand Down

0 comments on commit cfb1cdd

Please sign in to comment.