Skip to content

Commit

Permalink
Fix: make jsonSerialize backward compatibile with PHP 7.4 (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
albertoarena authored Mar 20, 2022
1 parent 5a89b24 commit d9f256c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Fractal.php
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,11 @@ public function getResourceName()

/**
* Convert the object into something JSON serializable.
*
* @return array
*/
public function jsonSerialize(): mixed
#[\ReturnTypeWillChange]
public function jsonSerialize(): array
{
return $this->toArray();
}
Expand Down
15 changes: 15 additions & 0 deletions tests/FractalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,19 @@ public function it_can_define_primitive_after_resource_name()

$this->assertEquals('tests', $resource->getResource()->getResourceKey());
}

/** @test */
public function it_can_convert_into_something_that_is_json_serializable()
{
$jsonSerialized = $this->fractal
->collection($this->testBooks, new TestTransformer())
->jsonSerialize();

$expectedArray = ['data' => [
['id' => 1, 'author' => 'Philip K Dick'],
['id' => 2, 'author' => 'George R. R. Satan'],
]];

$this->assertEquals($expectedArray, $jsonSerialized);
}
}

0 comments on commit d9f256c

Please sign in to comment.