Skip to content

Commit c0e860e

Browse files
author
n.gnato
committed
Date value serialization
1 parent dc40a39 commit c0e860e

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
### Added
10+
- Built-in DateTime and DateTimeInterface fields value wrapper for json serialization according to jsonapi spec Examples.
11+
912
## [0.0.8] - 2025-08-25
1013

11-
## Added
14+
### Added
1215
- BaseKeyValueStructure::assignFieldValue() method for inject custom field instantiation logic.
1316
- DateTimeInterface support for as field types.
1417

src/FreeElephants/JsonApi/DTO/BaseKeyValueStructure.php

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

33
namespace FreeElephants\JsonApi\DTO;
44

5+
use FreeElephants\JsonApi\DTO\Field\DateTimeFieldValue;
6+
57
class BaseKeyValueStructure
68
{
79
public function __construct(array $attributes)
@@ -22,8 +24,8 @@ protected function assignFieldValue(string $name, $value): self
2224
$value = null;
2325
} else {
2426
$propertyClassName = $propertyType->getName();
25-
if($propertyClassName === \DateTimeInterface::class) {
26-
$value = new \DateTime($value);
27+
if(in_array($propertyClassName, [\DateTimeInterface::class, \DateTime::class])) {
28+
$value = new DateTimeFieldValue($value);
2729
} else {
2830
$value = new $propertyClassName($value);
2931
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace FreeElephants\JsonApi\DTO\Field;
5+
6+
class DateTimeFieldValue extends \DateTime implements \JsonSerializable
7+
{
8+
public const DEFAULT_JSONAPI_DATE_FORMAT = 'Y-m-d\TH:i:s.vp';
9+
private static string $format = self::DEFAULT_JSONAPI_DATE_FORMAT;
10+
11+
public static function setFormat(string $format)
12+
{
13+
self::$format = $format;
14+
}
15+
16+
public function jsonSerialize(): string
17+
{
18+
return $this->format(self::$format);
19+
}
20+
}

tests/FreeElephants/JsonApi/DTO/DocumentTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class DocumentTest extends AbstractTestCase
1111
public function testFromRequest()
1212
{
1313
$request = new ServerRequest('POST', '/foo');
14-
$request->getBody()->write(<<<JSON
14+
$rawJson = <<<JSON
1515
{
1616
"data": {
1717
"id": "123",
@@ -38,7 +38,8 @@ public function testFromRequest()
3838
}
3939
}
4040
}
41-
JSON
41+
JSON;
42+
$request->getBody()->write($rawJson
4243
);
4344

4445
$fooDTO = FooDocument::fromHttpMessage($request);
@@ -53,6 +54,8 @@ public function testFromRequest()
5354
$this->assertNull($fooDTO->data->attributes->nullableObjectField);
5455
$this->assertNull($fooDTO->data->attributes->nullableScalarField);
5556
$this->assertSame('baz', $fooDTO->data->attributes->nullableScalarFilledField);
57+
58+
$this->assertJsonStringEqualsJsonString($rawJson, json_encode($fooDTO));
5659
}
5760
}
5861

0 commit comments

Comments
 (0)