Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2b00e4e

Browse files
committed
Add DateCaster
1 parent e9e19e7 commit 2b00e4e

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\VarDumper\Caster;
13+
14+
use Symfony\Component\VarDumper\Cloner\Stub;
15+
16+
/**
17+
* Casts DateTimeInterface related classes to array representation.
18+
*
19+
* @author Dany Maillard <[email protected]>
20+
*/
21+
class DateCaster
22+
{
23+
public static function castDate(\DateTimeInterface $d, array $a, Stub $stub, $isNested, $filter)
24+
{
25+
$prefix = Caster::PREFIX_VIRTUAL;
26+
$location = $d->getTimezone()->getLocation();
27+
$fromNow = (new \DateTime())->diff($d);
28+
29+
$title = $d->format('l, F j, Y')
30+
."\n".$fromNow->format('%R').(ltrim($fromNow->format('%yy %mm %dd %H:%I:%Ss'), ' 0ymd:s') ?: '0s').' from now'
31+
.($location ? ($d->format('I') ? "\nDST On" : "\nDST Off") : '')
32+
;
33+
34+
$a = array();
35+
$a[$prefix.'date'] = new ConstStub($d->format('Y-m-d H:i:s.u '.($location ? 'e (P)' : 'P')), $title);
36+
37+
$stub->class .= $d->format(' @U');
38+
39+
return $a;
40+
}
41+
}

src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ abstract class AbstractCloner implements ClonerInterface
109109
'Redis' => array('Symfony\Component\VarDumper\Caster\RedisCaster', 'castRedis'),
110110
'RedisArray' => array('Symfony\Component\VarDumper\Caster\RedisCaster', 'castRedisArray'),
111111

112+
'DateTimeInterface' => array('Symfony\Component\VarDumper\Caster\DateCaster', 'castDate'),
113+
112114
':curl' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castCurl'),
113115
':dba' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castDba'),
114116
':dba persistent' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castDba'),
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\VarDumper\Tests\Caster;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\VarDumper\Caster\DateCaster;
16+
use Symfony\Component\VarDumper\Cloner\Stub;
17+
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
18+
19+
/**
20+
* @author Dany Maillard <[email protected]>
21+
*/
22+
class DateCasterTest extends TestCase
23+
{
24+
use VarDumperTestTrait;
25+
26+
/**
27+
* @dataProvider provideDates
28+
*/
29+
public function testDumpDate($time, $timezone, $expected)
30+
{
31+
if ((defined('HHVM_VERSION_ID') || PHP_VERSION_ID <= 50509) && preg_match('/[-+]\d{2}:\d{2}/', $timezone)) {
32+
$this->markTestSkipped('DateTimeZone GMT offsets are supported since 5.5.10. See https://github.com/facebook/hhvm/issues/5875 for HHVM.');
33+
}
34+
35+
$date = new \DateTime($time, new \DateTimeZone($timezone));
36+
37+
$xDump = <<<EODUMP
38+
DateTime @1493503200 {
39+
date: $expected
40+
}
41+
EODUMP;
42+
43+
$this->assertDumpMatchesFormat($xDump, $date);
44+
}
45+
46+
public function testCastDate()
47+
{
48+
$stub = new Stub();
49+
$date = new \DateTime('2017-08-30 00:00:00.000000', new \DateTimeZone('Europe/Zurich'));
50+
$cast = DateCaster::castDate($date, array('foo' => 'bar'), $stub, false, 0);
51+
52+
$xDump = <<<'EODUMP'
53+
array:1 [
54+
"\x00~\x00date" => 2017-08-30 00:00:00.000000 Europe/Zurich (+02:00)
55+
]
56+
EODUMP;
57+
58+
$this->assertDumpMatchesFormat($xDump, $cast);
59+
60+
$xDump = <<<'EODUMP'
61+
Symfony\Component\VarDumper\Caster\ConstStub {
62+
+type: "ref"
63+
+class: "2017-08-30 00:00:00.000000 Europe/Zurich (+02:00)"
64+
+value: """
65+
Wednesday, August 30, 2017\n
66+
+%a from now\n
67+
DST On
68+
"""
69+
+cut: 0
70+
+handle: 0
71+
+refCount: 0
72+
+position: 0
73+
+attr: []
74+
}
75+
EODUMP;
76+
77+
$this->assertDumpMatchesFormat($xDump, $cast["\0~\0date"]);
78+
}
79+
80+
public function provideDates()
81+
{
82+
return array(
83+
array('2017-04-30 00:00:00.000000', 'Europe/Zurich', '2017-04-30 00:00:00.000000 Europe/Zurich (+02:00)'),
84+
array('2017-04-30 00:00:00.000000', '+02:00', '2017-04-30 00:00:00.000000 +02:00'),
85+
);
86+
}
87+
}

0 commit comments

Comments
 (0)