Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/ImmutableRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* @package EventEngine\Data
* @psalm-immutable
*/
interface ImmutableRecord
interface ImmutableRecord extends \JsonSerializable
{
const PHP_TYPE_STRING = 'string';
const PHP_TYPE_INT = 'int';
Expand Down
5 changes: 5 additions & 0 deletions src/ImmutableRecordLogic.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ public function equals(ImmutableRecord $other): bool
return $this->toArray() === $other->toArray();
}

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

private function setRecordData(array $recordData): void
{
foreach ($recordData as $key => $value) {
Expand Down
27 changes: 27 additions & 0 deletions tests/ImmutableRecordLogicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,31 @@ public function it_throws_an_exception_if_no_type_hint_was_found()

ImmutableRecordWithNoTypes::fromArray($this->data);
}

/**
* @test
*/
public function it_supports_json_serializable()
{
$record = TypeHintedImmutableRecordWithValueObjects::fromArray($this->data);

$this->data['type'] = null;
$this->data['percentage'] = 0.5;

$this->assertEquals(
$this->data,
$record->jsonSerialize()
);

$this->assertEquals(
$record->toArray(),
$record->jsonSerialize()
);

$json = json_encode($record);
$this->assertJson($json);

$decoded = json_decode($json, true);
$this->assertEquals($this->data, $decoded);
}
}