Skip to content

Commit d9f256c

Browse files
authored
Fix: make jsonSerialize backward compatibile with PHP 7.4 (#55)
1 parent 5a89b24 commit d9f256c

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/Fractal.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,8 +454,11 @@ public function getResourceName()
454454

455455
/**
456456
* Convert the object into something JSON serializable.
457+
*
458+
* @return array
457459
*/
458-
public function jsonSerialize(): mixed
460+
#[\ReturnTypeWillChange]
461+
public function jsonSerialize(): array
459462
{
460463
return $this->toArray();
461464
}

tests/FractalTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,4 +250,19 @@ public function it_can_define_primitive_after_resource_name()
250250

251251
$this->assertEquals('tests', $resource->getResource()->getResourceKey());
252252
}
253+
254+
/** @test */
255+
public function it_can_convert_into_something_that_is_json_serializable()
256+
{
257+
$jsonSerialized = $this->fractal
258+
->collection($this->testBooks, new TestTransformer())
259+
->jsonSerialize();
260+
261+
$expectedArray = ['data' => [
262+
['id' => 1, 'author' => 'Philip K Dick'],
263+
['id' => 2, 'author' => 'George R. R. Satan'],
264+
]];
265+
266+
$this->assertEquals($expectedArray, $jsonSerialized);
267+
}
253268
}

0 commit comments

Comments
 (0)