Skip to content

Commit 5dbedc3

Browse files
authored
Merge pull request #62 from nathansalter/bugfix/55-type-checks-too-strict
Reduce strictness on checks for Date/Time types
2 parents f8b8a0c + 5a532aa commit 5dbedc3

5 files changed

Lines changed: 61 additions & 3 deletions

File tree

src/Helpers/DateTime.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static function iso8601($date, $isDateOnly = false)
2222
return $date->toISO8601();
2323
}
2424

25-
if ($date instanceof \DateTime === false) {
25+
if (!$date instanceof \DateTime && !$date instanceof \DateTimeImmutable) {
2626
throw new \Exception("Invalid argument type.");
2727
}
2828

src/Helpers/JsonLd.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ public static function prepareDataForSerialization($obj, $parent = null, $schema
3131
$fq_classname = "\\".get_class($obj);
3232

3333
// Get interval spec string, e.g. "P1D"
34-
if($fq_classname === "\\DateInterval") {
34+
if($obj instanceof \DateInterval) {
3535
return DateIntervalHelper::specString($obj);
3636
}
3737

3838
// Get ISO 8601 date time representation,
3939
// e.g. "2019-01-01T00:00:00-08:00"
40-
if($fq_classname === "\\DateTime") {
40+
if($obj instanceof \DateTime || $obj instanceof \DateTimeImmutable) {
4141
return DateTimeHelper::iso8601($obj);
4242
}
4343

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace OpenActive\Models\Tests\Unit\Helpers;
4+
5+
class DateIntervalExtension extends \DateInterval
6+
{
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace OpenActive\Models\Tests\Unit\Helpers;
4+
5+
class DateTimeExtension extends \DateTime
6+
{
7+
}

tests/Unit/Helpers/JsonLdTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace OpenActive\Models\Tests\Unit\Helpers;
4+
5+
use OpenActive\Helpers\JsonLd;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class JsonLdTest extends TestCase
9+
{
10+
/**
11+
* @param $dateTime
12+
* @dataProvider dateTimeProvider
13+
*/
14+
public function testSerializesDateTimes($dateTime)
15+
{
16+
$this->assertEquals('2020-01-01T11:00:00+00:00', JsonLd::prepareDataForSerialization($dateTime));
17+
}
18+
19+
/**
20+
* @param $dateInterval
21+
* @dataProvider dateIntervalProvider
22+
*/
23+
public function testSerializesDateIntervals($dateInterval)
24+
{
25+
$this->assertEquals('P1DT1H', JsonLd::prepareDataForSerialization($dateInterval));
26+
}
27+
28+
public function dateTimeProvider()
29+
{
30+
return [
31+
[new \DateTime('2020-01-01T11:00:00+00:00')],
32+
[new \DateTimeImmutable('2020-01-01T11:00:00+00:00')],
33+
[new DateTimeExtension('2020-01-01T11:00:00+00:00')],
34+
];
35+
}
36+
37+
public function dateIntervalProvider()
38+
{
39+
return [
40+
[new \DateInterval('P1DT1H')],
41+
[new DateIntervalExtension('P1DT1H')],
42+
];
43+
}
44+
}

0 commit comments

Comments
 (0)