Skip to content

Commit 070e261

Browse files
committed
Create DateTimeStringifier
1 parent bfec8cb commit 070e261

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Respect/Stringifier.
5+
*
6+
* (c) Henrique Moody <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the "LICENSE.md"
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Respect\Stringifier\Stringifiers;
15+
16+
use DateTimeInterface;
17+
use Respect\Stringifier\Stringifier;
18+
19+
/**
20+
* Converts an instance of DateTimeInterface into a string.
21+
*
22+
* @author Henrique Moody <[email protected]>
23+
*/
24+
final class DateTimeStringifier implements Stringifier
25+
{
26+
/**
27+
* @var Stringifier
28+
*/
29+
private $stringifier;
30+
31+
/**
32+
* @var string
33+
*/
34+
private $format;
35+
36+
/**
37+
* Initializes the stringifier.
38+
*
39+
* @param Stringifier $stringifier
40+
* @param string $format
41+
*/
42+
public function __construct(Stringifier $stringifier, string $format)
43+
{
44+
$this->stringifier = $stringifier;
45+
$this->format = $format;
46+
}
47+
48+
/**
49+
* {@inheritdoc}
50+
*/
51+
public function stringify($raw, int $depth): ?string
52+
{
53+
if (!$raw instanceof DateTimeInterface) {
54+
return null;
55+
}
56+
57+
return $this->stringifier->stringify($raw->format($this->format), $depth);
58+
}
59+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Respect/Stringifier.
5+
*
6+
* (c) Henrique Moody <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the "LICENSE.md"
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Respect\Stringifier\Test\Stringifiers;
15+
16+
use DateTime;
17+
use DateTimeImmutable;
18+
use DateTimeInterface;
19+
use PHPUnit\Framework\TestCase;
20+
use Respect\Stringifier\Stringifier;
21+
use Respect\Stringifier\Stringifiers\DateTimeStringifier;
22+
23+
/**
24+
* @covers \Respect\Stringifier\Stringifiers\DateTimeStringifier
25+
*
26+
* @author Henrique Moody <[email protected]>
27+
*/
28+
final class DateTimeStringifierTest extends TestCase
29+
{
30+
/**
31+
* @test
32+
*/
33+
public function shouldNotConvertWhenNotInstanceOfDateTimeInterface(): void
34+
{
35+
$stringifierMock = $this->createMock(Stringifier::class);
36+
$stringifierMock
37+
->expects($this->never())
38+
->method('stringify');
39+
40+
$dateTimeStringifier = new DateTimeStringifier($stringifierMock, 'c');
41+
42+
self::assertNull($dateTimeStringifier->stringify('NotDateTimeInterface', 0));
43+
}
44+
45+
/**
46+
* @test
47+
*
48+
* @dataProvider validValuesProvider
49+
*
50+
* @param DateTimeInterface $raw
51+
* @param string $format
52+
* @param string $expectedValue
53+
*/
54+
public function shouldConvertDateTimeInterfaceToString(
55+
DateTimeInterface $raw,
56+
string $format,
57+
string $expectedValue
58+
): void {
59+
$depth = 0;
60+
61+
$stringifierMock = $this->createMock(Stringifier::class);
62+
$stringifierMock
63+
->expects($this->once())
64+
->method('stringify')
65+
->with($expectedValue, $depth)
66+
->willReturn($expectedValue);
67+
68+
$dateTimeStringifier = new DateTimeStringifier($stringifierMock, $format);
69+
70+
self::assertSame($expectedValue, $dateTimeStringifier->stringify($raw, $depth));
71+
}
72+
73+
public function validValuesProvider(): array
74+
{
75+
$dateTime = DateTime::createFromFormat('Y-m-d\TH:i:sP', '2017-12-31T23:59:59+00:00');
76+
$dateTimeImmutable = DateTimeImmutable::createFromMutable($dateTime);
77+
78+
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'],
82+
];
83+
}
84+
}

0 commit comments

Comments
 (0)