Skip to content

Commit c75c736

Browse files
committed
Fix serialization of carbon objects
1 parent 944c564 commit c75c736

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"php": "^7.3|^8.0"
2222
},
2323
"require-dev": {
24+
"nesbot/carbon": "^2.61",
2425
"pestphp/pest": "^1.21.3",
2526
"phpstan/phpstan": "^1.8.2",
2627
"symfony/var-dumper": "^5.4.11"

src/Serializers/Native.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,12 @@ protected function mapByReference(&$data)
460460

461461
$instance = $data;
462462

463+
if ($data instanceof \DateTime) {
464+
$this->scope[$instance] = $data;
465+
466+
return;
467+
}
468+
463469
if ($data instanceof UnitEnum) {
464470
$this->scope[$instance] = $data;
465471

tests/SerializerTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,19 @@ function () {
401401
expect($f(new Model))->toBeInstanceOf(Model::class);
402402
})->with('serializers');
403403

404+
test('serializes carbon objects', function () {
405+
$carbon = new \Carbon\Carbon('now');
406+
407+
$closure = function () use ($carbon){
408+
return $carbon;
409+
};
410+
411+
$u = s($closure);
412+
$r = $u();
413+
414+
expect($r)->toEqual($carbon);
415+
})->with('serializers');
416+
404417
class A
405418
{
406419
protected static function aStaticProtected()

0 commit comments

Comments
 (0)