Skip to content

Commit 1220eae

Browse files
committed
DateTimeInterface should not be treated as strings
They are objects, therefore they need to be represented as is.
1 parent ef92514 commit 1220eae

File tree

4 files changed

+55
-15
lines changed

4 files changed

+55
-15
lines changed

src/Stringifiers/ClusterStringifier.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static function createDefault(): self
5353
$stringifier = new self();
5454
$stringifier->setStringifiers([
5555
new TraversableStringifier($stringifier, $quoter),
56-
new DateTimeStringifier($stringifier, 'c'),
56+
new DateTimeStringifier($stringifier, $quoter, 'c'),
5757
new ThrowableStringifier($stringifier, $quoter),
5858
new StringableObjectStringifier($stringifier),
5959
new JsonSerializableStringifier($stringifier, $quoter),

src/Stringifiers/DateTimeStringifier.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
namespace Respect\Stringifier\Stringifiers;
1515

1616
use DateTimeInterface;
17+
use function get_class;
18+
use function sprintf;
19+
use Respect\Stringifier\Quoter;
1720
use Respect\Stringifier\Stringifier;
1821

1922
/**
@@ -28,6 +31,11 @@ final class DateTimeStringifier implements Stringifier
2831
*/
2932
private $stringifier;
3033

34+
/**
35+
* @var Quoter
36+
*/
37+
private $quoter;
38+
3139
/**
3240
* @var string
3341
*/
@@ -37,11 +45,13 @@ final class DateTimeStringifier implements Stringifier
3745
* Initializes the stringifier.
3846
*
3947
* @param Stringifier $stringifier
48+
* @param Quoter $quoter
4049
* @param string $format
4150
*/
42-
public function __construct(Stringifier $stringifier, string $format)
51+
public function __construct(Stringifier $stringifier, Quoter $quoter, string $format)
4352
{
4453
$this->stringifier = $stringifier;
54+
$this->quoter = $quoter;
4555
$this->format = $format;
4656
}
4757

@@ -54,6 +64,13 @@ public function stringify($raw, int $depth): ?string
5464
return null;
5565
}
5666

57-
return $this->stringifier->stringify($raw->format($this->format), $depth);
67+
return $this->quoter->quote(
68+
sprintf(
69+
'[date-time] (%s: %s)',
70+
get_class($raw),
71+
$this->stringifier->stringify($raw->format($this->format), $depth + 1)
72+
),
73+
$depth
74+
);
5875
}
5976
}

tests/integration/stringify-object-dateTime.phpt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,16 @@ require 'vendor/autoload.php';
55
use function Respect\Stringifier\stringify;
66

77
$dateTime = DateTime::createFromFormat('Y-m-d\TH:i:sP', '2017-12-31T23:59:59+00:00');
8+
$dateTimeImmutable = DateTimeImmutable::createFromMutable($dateTime);
89

9-
echo stringify($dateTime);
10+
echo implode(
11+
PHP_EOL,
12+
[
13+
stringify($dateTime),
14+
stringify($dateTimeImmutable),
15+
]
16+
);
1017
?>
1118
--EXPECT--
12-
"2017-12-31T23:59:59+00:00"
19+
`[date-time] (DateTime: "2017-12-31T23:59:59+00:00")`
20+
`[date-time] (DateTimeImmutable: "2017-12-31T23:59:59+00:00")`

tests/unit/Stringifiers/DateTimeStringifierTest.php

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use DateTimeImmutable;
1818
use DateTimeInterface;
1919
use PHPUnit\Framework\TestCase;
20+
use Respect\Stringifier\Quoter;
2021
use Respect\Stringifier\Stringifier;
2122
use Respect\Stringifier\Stringifiers\DateTimeStringifier;
2223

@@ -37,7 +38,12 @@ public function shouldNotConvertWhenNotInstanceOfDateTimeInterface(): void
3738
->expects($this->never())
3839
->method('stringify');
3940

40-
$dateTimeStringifier = new DateTimeStringifier($stringifierMock, 'c');
41+
$quoterMock = $this->createMock(Quoter::class);
42+
$quoterMock
43+
->expects($this->never())
44+
->method('quote');
45+
46+
$dateTimeStringifier = new DateTimeStringifier($stringifierMock, $quoterMock, 'c');
4147

4248
self::assertNull($dateTimeStringifier->stringify('NotDateTimeInterface', 0));
4349
}
@@ -49,25 +55,34 @@ public function shouldNotConvertWhenNotInstanceOfDateTimeInterface(): void
4955
*
5056
* @param DateTimeInterface $raw
5157
* @param string $format
52-
* @param string $expectedValue
58+
* @param string $expected
5359
*/
5460
public function shouldConvertDateTimeInterfaceToString(
5561
DateTimeInterface $raw,
5662
string $format,
57-
string $expectedValue
63+
string $expected
5864
): void {
5965
$depth = 0;
6066

67+
$formattedDateTime = $raw->format($format);
68+
6169
$stringifierMock = $this->createMock(Stringifier::class);
6270
$stringifierMock
6371
->expects($this->once())
6472
->method('stringify')
65-
->with($expectedValue, $depth)
66-
->willReturn($expectedValue);
73+
->with($formattedDateTime, $depth + 1)
74+
->willReturn($formattedDateTime);
75+
76+
$quoterMock = $this->createMock(Quoter::class);
77+
$quoterMock
78+
->expects($this->once())
79+
->method('quote')
80+
->with($expected)
81+
->willReturn($expected);
6782

68-
$dateTimeStringifier = new DateTimeStringifier($stringifierMock, $format);
83+
$dateTimeStringifier = new DateTimeStringifier($stringifierMock, $quoterMock, $format);
6984

70-
self::assertSame($expectedValue, $dateTimeStringifier->stringify($raw, $depth));
85+
self::assertSame($expected, $dateTimeStringifier->stringify($raw, $depth));
7186
}
7287

7388
public function validValuesProvider(): array
@@ -76,9 +91,9 @@ public function validValuesProvider(): array
7691
$dateTimeImmutable = DateTimeImmutable::createFromMutable($dateTime);
7792

7893
return [
79-
[$dateTime, 'd/m/Y', '31/12/2017'],
80-
[$dateTime, 'c', '2017-12-31T23:59:59+00:00'],
81-
[$dateTimeImmutable, 'Y-m-d H:i:s', '2017-12-31 23:59:59'],
94+
[$dateTime, 'd/m/Y', '[date-time] (DateTime: 31/12/2017)'],
95+
[$dateTime, 'c', '[date-time] (DateTime: 2017-12-31T23:59:59+00:00)'],
96+
[$dateTimeImmutable, 'Y-m-d H:i:s', '[date-time] (DateTimeImmutable: 2017-12-31 23:59:59)'],
8297
];
8398
}
8499
}

0 commit comments

Comments
 (0)