Skip to content

Commit 5c24120

Browse files
committed
Accept DATE_* constants as formatter values for the DateTime filter
1 parent 64f44a5 commit 5c24120

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

src/Filter/DateTime.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class DateTime
2424
*/
2525
public static function filter(?string $value, array $options = []): ?\DateTime
2626
{
27+
static::convertConstants($options);
28+
2729
try {
2830
if (($options['convertNullToNow'] ?? false) && $value === null) {
2931
return new \DateTime();
@@ -38,7 +40,13 @@ public static function filter(?string $value, array $options = []): ?\DateTime
3840
}
3941

4042
if (($options['createFromFormat'] ?? false) && $value !== null) {
41-
return \DateTime::createFromFormat($options['createFromFormat'], $value);
43+
$result = \DateTime::createFromFormat($options['createFromFormat'], $value);
44+
45+
if (!$result) {
46+
throw new Exception();
47+
}
48+
49+
return $result;
4250
}
4351

4452
return $value !== null ? new \DateTime($value) : null;
@@ -55,8 +63,24 @@ public static function filter(?string $value, array $options = []): ?\DateTime
5563
*/
5664
public static function serialize(?\DateTime $value, array $options = []): ?string
5765
{
66+
static::convertConstants($options);
67+
5868
return ($value instanceof \DateTime)
5969
? $value->format($options['outputFormat'] ?? $options['createFromFormat'] ?? DATE_ISO8601)
6070
: null;
6171
}
72+
73+
/**
74+
* Make DATE constants available
75+
*
76+
* @param array $options
77+
*/
78+
protected static function convertConstants(array &$options): void
79+
{
80+
foreach (['createFromFormat', 'outputFormat'] as $format) {
81+
if (isset($options[$format]) && defined("DATE_{$options[$format]}")) {
82+
$options[$format] = constant("DATE_{$options[$format]}");
83+
}
84+
}
85+
}
6286
}

tests/Filter/DateTimeTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ public function testCreateFromFormatOption(): void
8181
);
8282
}
8383

84+
public function testCreateFromFormatOptionWithFormatConstant(): void
85+
{
86+
$dateTime = new DateTime();
87+
$this->assertSame(
88+
$dateTime->format(DATE_ATOM),
89+
DateTimeFilter::filter($dateTime->format(DATE_RSS), ['createFromFormat' => 'RSS'])->format(DATE_ATOM)
90+
);
91+
}
92+
8493
public function testSerializeDefaultFormat(): void
8594
{
8695
$this->assertNull(DateTimeFilter::serialize(null));
@@ -108,4 +117,24 @@ public function testSerializeWithOutputFormat(): void
108117
DateTimeFilter::serialize($dateTime, ['createFromFormat' => 'mdY', 'outputFormat' => 'Ymd'])
109118
);
110119
}
120+
121+
public function testSerializeWithCreateFromFormatWithFormatConstant(): void
122+
{
123+
$dateTime = new DateTime();
124+
125+
$this->assertSame(
126+
$dateTime->format(DATE_RSS),
127+
DateTimeFilter::serialize($dateTime, ['createFromFormat' => 'RSS'])
128+
);
129+
}
130+
131+
public function testSerializeWithOutputFormatWithFormatConstant(): void
132+
{
133+
$dateTime = new DateTime();
134+
135+
$this->assertSame(
136+
$dateTime->format(DATE_RSS),
137+
DateTimeFilter::serialize($dateTime, ['createFromFormat' => 'ATOM', 'outputFormat' => 'RSS'])
138+
);
139+
}
111140
}

0 commit comments

Comments
 (0)