Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 27bf394

Browse filesBrowse files
committed
Add time zone caster
1 parent 8c028bd commit 27bf394
Copy full SHA for 27bf394

File tree

4 files changed

+129
-4
lines changed
Filter options

4 files changed

+129
-4
lines changed

‎src/Symfony/Component/VarDumper/Caster/DateCaster.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/VarDumper/Caster/DateCaster.php
+22-3Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,18 @@ class DateCaster
2323
public static function castDateTime(\DateTimeInterface $d, array $a, Stub $stub, $isNested, $filter)
2424
{
2525
$prefix = Caster::PREFIX_VIRTUAL;
26-
$location = $d->getTimezone()->getLocation();
2726
$fromNow = (new \DateTime())->diff($d);
2827

2928
$title = $d->format('l, F j, Y')
3029
."\n".$fromNow->format('%R').self::formatInterval($fromNow).' from now'
31-
.($location ? ($d->format('I') ? "\nDST On" : "\nDST Off") : '')
30+
.($d->getTimezone()->getLocation() ? ($d->format('I') ? "\nDST On" : "\nDST Off") : '')
3231
;
3332

3433
$a = array();
35-
$a[$prefix.'date'] = new ConstStub($d->format('Y-m-d H:i:s.u '.($location ? 'e (P)' : 'P')), $title);
34+
$a[$prefix.'date'] = new ConstStub(
35+
$d->format('Y-m-d H:i:s.u ').self::formatTimeZone($d->getTimezone()),
36+
$title
37+
);
3638

3739
$stub->class .= $d->format(' @U');
3840

@@ -68,4 +70,21 @@ private static function formatInterval(\DateInterval $i)
6870

6971
return $i->format(rtrim($format));
7072
}
73+
74+
public static function castTimeZone(\DateTimeZone $timeZone, array $a, Stub $stub, $isNested, $filter)
75+
{
76+
$title = ($location = $timeZone->getLocation()) && method_exists('\Locale', 'getDisplayRegion')
77+
? \Locale::getDisplayRegion('-'.$location['country_code'])
78+
: ''
79+
;
80+
81+
$z = array(Caster::PREFIX_VIRTUAL.'timezone' => new ConstStub(self::formatTimeZone($timeZone), $title));
82+
83+
return $filter & Caster::EXCLUDE_VERBOSE ? $z : $z + $a;
84+
}
85+
86+
private static function formatTimeZone(\DateTimeZone $z)
87+
{
88+
return (new \Datetime('now', $z))->format($z->getLocation() ? 'e (P)' : 'P');
89+
}
7190
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ abstract class AbstractCloner implements ClonerInterface
111111

112112
'DateTimeInterface' => array('Symfony\Component\VarDumper\Caster\DateCaster', 'castDateTime'),
113113
'DateInterval' => array('Symfony\Component\VarDumper\Caster\DateCaster', 'castInterval'),
114+
'DateTimeZone' => array('Symfony\Component\VarDumper\Caster\DateCaster', 'castTimeZone'),
114115

115116
':curl' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castCurl'),
116117
':dba' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castDba'),

‎src/Symfony/Component/VarDumper/Tests/Caster/DateCasterTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/VarDumper/Tests/Caster/DateCasterTest.php
+104Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,4 +184,108 @@ public function provideIntervals()
184184
array('P1Y2M3DT4H5M6S', 1, '- 1y 2m 3d 04:05:06'.$ms, null),
185185
);
186186
}
187+
188+
/**
189+
* @dataProvider provideTimeZones
190+
*/
191+
public function testDumpTimeZone($timezone, $expected)
192+
{
193+
if ((defined('HHVM_VERSION_ID') || PHP_VERSION_ID <= 50509) && !preg_match('/\w+\/\w+/', $timezone)) {
194+
$this->markTestSkipped('DateTimeZone GMT offsets are supported since 5.5.10. See https://github.com/facebook/hhvm/issues/5875 for HHVM.');
195+
}
196+
197+
$timezone = new \DateTimeZone($timezone);
198+
199+
$xDump = <<<EODUMP
200+
DateTimeZone {
201+
timezone: $expected
202+
%A}
203+
EODUMP;
204+
205+
$this->assertDumpMatchesFormat($xDump, $timezone);
206+
}
207+
208+
/**
209+
* @dataProvider provideTimeZones
210+
*/
211+
public function testDumpTimeZoneExcludingVerbosity($timezone, $expected)
212+
{
213+
if ((defined('HHVM_VERSION_ID') || PHP_VERSION_ID <= 50509) && !preg_match('/\w+\/\w+/', $timezone)) {
214+
$this->markTestSkipped('DateTimeZone GMT offsets are supported since 5.5.10. See https://github.com/facebook/hhvm/issues/5875 for HHVM.');
215+
}
216+
217+
$timezone = new \DateTimeZone($timezone);
218+
219+
$xDump = <<<EODUMP
220+
DateTimeZone {
221+
timezone: $expected
222+
}
223+
EODUMP;
224+
225+
$this->assertDumpMatchesFormat($xDump, $timezone, Caster::EXCLUDE_VERBOSE);
226+
}
227+
228+
/**
229+
* @dataProvider provideTimeZones
230+
*/
231+
public function testCastTimeZone($timezone, $xTimezone, $xRegion)
232+
{
233+
if ((defined('HHVM_VERSION_ID') || PHP_VERSION_ID <= 50509) && !preg_match('/\w+\/\w+/', $timezone)) {
234+
$this->markTestSkipped('DateTimeZone GMT offsets are supported since 5.5.10. See https://github.com/facebook/hhvm/issues/5875 for HHVM.');
235+
}
236+
237+
$timezone = new \DateTimeZone($timezone);
238+
$stub = new Stub();
239+
240+
$cast = DateCaster::castTimeZone($timezone, array('foo' => 'bar'), $stub, false, Caster::EXCLUDE_VERBOSE);
241+
242+
$xDump = <<<EODUMP
243+
array:1 [
244+
"\\x00~\\x00timezone" => $xTimezone
245+
]
246+
EODUMP;
247+
248+
$this->assertDumpMatchesFormat($xDump, $cast);
249+
250+
$xDump = <<<EODUMP
251+
Symfony\Component\VarDumper\Caster\ConstStub {
252+
+type: "ref"
253+
+class: "$xTimezone"
254+
+value: "$xRegion"
255+
+cut: 0
256+
+handle: 0
257+
+refCount: 0
258+
+position: 0
259+
+attr: []
260+
}
261+
EODUMP;
262+
263+
$this->assertDumpMatchesFormat($xDump, $cast["\0~\0timezone"]);
264+
}
265+
266+
public function provideTimeZones()
267+
{
268+
return array(
269+
// type 1 (UTC offset)
270+
array('-12:00', '-12:00', ''),
271+
array('+00:00', '+00:00', ''),
272+
array('+14:00', '+14:00', ''),
273+
274+
// type 2 (timezone abbreviation)
275+
array('GMT', '+00:00', ''),
276+
array('a', '+01:00', ''),
277+
array('b', '+02:00', ''),
278+
array('z', '+00:00', ''),
279+
280+
// type 3 (timezone identifier)
281+
array('Africa/Tunis', 'Africa/Tunis (+01:00)', 'Tunisia'),
282+
array('America/Panama', 'America/Panama (-05:00)', 'Panama'),
283+
array('Antarctica/Troll', 'Antarctica/Troll (+02:00)', 'Antarctica'),
284+
array('Asia/Jerusalem', 'Asia/Jerusalem (+03:00)', 'Israel'),
285+
array('Atlantic/Canary', 'Atlantic/Canary (+01:00)', 'Spain'),
286+
array('Australia/Perth', 'Australia/Perth (+08:00)', 'Australia'),
287+
array('Europe/Zurich', 'Europe/Zurich (+02:00)', 'Switzerland'),
288+
array('Pacific/Tahiti', 'Pacific/Tahiti (-10:00)', 'French Polynesia'),
289+
);
290+
}
187291
}

‎src/Symfony/Component/VarDumper/composer.json

Copy file name to clipboardExpand all lines: src/Symfony/Component/VarDumper/composer.json
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
},
2929
"suggest": {
3030
"ext-iconv": "To convert non-UTF-8 strings to UTF-8 (or symfony/polyfill-iconv in case ext-iconv cannot be used).",
31-
"ext-symfony_debug": ""
31+
"ext-symfony_debug": "",
32+
"ext-intl": "To show region in time zone dump"
3233
},
3334
"autoload": {
3435
"files": [ "Resources/functions/dump.php" ],

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.