Skip to content

Commit 8f450af

Browse files
committed
Add support to convert entity Datetime to ATOM when use in JSON encode
1 parent e3a28d1 commit 8f450af

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/Entity.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646

4747
namespace Platine\Orm;
4848

49+
use DateTime;
4950
use JsonSerializable;
5051
use Platine\Orm\Exception\PropertyNotFoundException;
5152
use Platine\Orm\Mapper\DataMapper;
@@ -128,7 +129,11 @@ public function jsonSerialize(): mixed
128129
$data[$name] = $relation;
129130
}
130131
} elseif ($this->mapper()->hasColumn($name)) {
131-
$data[$name] = $this->mapper()->getColumn($name);
132+
$value = $this->mapper()->getColumn($name);
133+
if ($value instanceof DateTime) {
134+
$value = $value->format(DateTime::ATOM);
135+
}
136+
$data[$name] = $value;
132137
} else {
133138
$data[$name] = $value;
134139
}

tests/EntityTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Platine\Test\Orm;
66

7+
use DateTime;
8+
use DateTimeZone;
79
use Platine\Dev\PlatineTestCase;
810
use Platine\Orm\Entity;
911
use Platine\Orm\EntityManager;
@@ -155,11 +157,15 @@ public function testJsonRealEntity(): void
155157
$er->name = 'foo';
156158

157159
$e->foo = 'bar';
160+
$e->date = new DateTime('2025-09-01', new DateTimeZone('Africa/Bangui'));
158161
$e->relation = $er;
159162

160-
$this->assertCount(2, $e->jsonSerialize());
163+
$this->assertCount(3, $e->jsonSerialize());
161164
$this->assertArrayHasKey('foo', $e->jsonSerialize());
162-
$this->assertEquals('{"foo":"bar","relation":{"name":"foo"}}', json_encode($e));
165+
$this->assertEquals(
166+
'{"foo":"bar","date":"2025-09-01T00:00:00+01:00","relation":{"name":"foo"}}',
167+
json_encode($e)
168+
);
163169
}
164170

165171
public function testDataMapperInstance(): void

0 commit comments

Comments
 (0)