Skip to content

Commit 603de8a

Browse files
committed
Allow serializing any \DateTimeInterface object as an Item value
1 parent 21a5cc2 commit 603de8a

File tree

4 files changed

+32
-13
lines changed

4 files changed

+32
-13
lines changed

src/Date.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@
44

55
namespace gapple\StructuredFields;
66

7-
class Date
7+
class Date extends \DateTimeImmutable
88
{
9-
public function __construct(private readonly int $value)
9+
public function __construct(int $timestamp)
1010
{
11+
parent::__construct("@{$timestamp}");
1112
}
1213

14+
/**
15+
* @deprecated in 2.3.0 and will be removed from 3.0.0. Use Date::getTimestamp() instead.
16+
*/
1317
public function toInt(): int
1418
{
15-
return $this->value;
19+
return $this->getTimestamp();
1620
}
1721
}

src/Serializer.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ private static function serializeInnerList(array $value, ?object $parameters = n
149149
return $returnValue;
150150
}
151151

152+
/**
153+
* @param int|float|bool|\DateTimeInterface|Bytes|DisplayString|Token|string|\Stringable $value
154+
*/
152155
private static function serializeBareItem(mixed $value): string
153156
{
154157
if (is_int($value)) {
@@ -157,14 +160,14 @@ private static function serializeBareItem(mixed $value): string
157160
return self::serializeDecimal($value);
158161
} elseif (is_bool($value)) {
159162
return self::serializeBoolean($value);
160-
} elseif ($value instanceof Token) {
161-
return self::serializeToken($value);
163+
} elseif ($value instanceof \DateTimeInterface) {
164+
return self::serializeDate($value);
162165
} elseif ($value instanceof Bytes) {
163166
return self::serializeByteSequence($value);
164-
} elseif ($value instanceof Date) {
165-
return self::serializeDate($value);
166167
} elseif ($value instanceof DisplayString) {
167168
return self::serializeDisplayString($value);
169+
} elseif ($value instanceof Token) {
170+
return self::serializeToken($value);
168171
} elseif (is_string($value) || $value instanceof \Stringable) {
169172
return self::serializeString((string) $value);
170173
}
@@ -260,9 +263,9 @@ private static function serializeParameters(object $value): string
260263
return $returnValue;
261264
}
262265

263-
private static function serializeDate(Date $value): string
266+
private static function serializeDate(\DateTimeInterface $value): string
264267
{
265-
return '@' . self::serializeInteger($value->toInt());
268+
return '@' . self::serializeInteger($value->getTimestamp());
266269
}
267270

268271
private static function serializeKey(string $value): string

tests/DateTest.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,47 @@
22

33
namespace gapple\Tests\StructuredFields;
44

5+
use gapple\StructuredFields\Item;
6+
57
/**
68
* Additional Date parsing and serializing tests.
79
*/
810
class DateTest extends RulesetTestBase
911
{
1012
use ParsingRulesetTrait;
13+
use SerializingRulesetTrait;
1114

1215
/**
1316
* {@inheritdoc}
1417
*/
1518
protected static function rulesetDataProvider(): array
1619
{
1720
return [
18-
'date - large int' => [
21+
'large int' => [
1922
Rule::fromArray([
2023
'name' => 'date - large int',
2124
'raw' => ['@1234567890123456'],
2225
'header_type' => 'item',
2326
'must_fail' => true,
2427
]),
2528
],
26-
'date - hexadecimal' => [
29+
'hexadecimal' => [
2730
Rule::fromArray([
2831
'name' => 'date - hexadecimal',
2932
'raw' => ['@0x62EB2779'],
3033
'header_type' => 'item',
3134
'must_fail' => true,
3235
]),
3336
],
37+
// Serialize any \DateTimeInterface object.
38+
'DateTimeInterface' => [
39+
Rule::fromArray([
40+
'name' => 'date - DateTimeInterface',
41+
'header_type' => 'item',
42+
'expected' => new Item(new \DateTimeImmutable('@629528400')),
43+
'canonical' => ['@629528400'],
44+
]),
45+
],
3446
];
3547
}
3648
}

tests/Httpwg/HttpwgRuleExpectedConverter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,9 @@ private static function innerList(array $innerList): InnerList
127127
*
128128
* @param ExpectedBareValue $data
129129
* The expected bare value.
130-
* @return bool|int|float|string|Bytes|Date|DisplayString|Token
130+
* @return bool|int|float|string|Bytes|\DateTimeInterface|DisplayString|Token
131131
*/
132-
private static function value($data)
132+
private static function value($data): mixed
133133
{
134134
if (!is_object($data)) {
135135
return $data;

0 commit comments

Comments
 (0)