Skip to content

Commit 5c4bfac

Browse files
committed
Add time zone caster
1 parent 6dc5f59 commit 5c4bfac

File tree

4 files changed

+118
-0
lines changed

4 files changed

+118
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,15 @@ private static function formatInterval(\DateInterval $i)
6868

6969
return $i->format(rtrim($format));
7070
}
71+
72+
public static function castTimeZone(\DateTimeZone $timeZone, array $a, Stub $stub, $isNested, $filter)
73+
{
74+
$location = $timeZone->getLocation();
75+
$formatted = (new \Datetime('now', $timeZone))->format($location ? 'e (P)' : 'P');
76+
$title = $location && extension_loaded('intl') ? \Locale::getDisplayRegion('-'.$location['country_code'], \Locale::getDefault()) : '';
77+
78+
$z = array(Caster::PREFIX_VIRTUAL.'timezone' => new ConstStub($formatted, $title));
79+
80+
return $filter & Caster::EXCLUDE_VERBOSE ? $z : $z + $a;
81+
}
7182
}

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

Lines 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

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,4 +184,109 @@ 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+
$xRegion = extension_loaded('intl') ? '%s' : '';
269+
270+
return array(
271+
// type 1 (UTC offset)
272+
array('-12:00', '-12:00', ''),
273+
array('+00:00', '+00:00', ''),
274+
array('+14:00', '+14:00', ''),
275+
276+
// type 2 (timezone abbreviation)
277+
array('GMT', '+00:00', ''),
278+
array('a', '+01:00', ''),
279+
array('b', '+02:00', ''),
280+
array('z', '+00:00', ''),
281+
282+
// type 3 (timezone identifier)
283+
array('Africa/Tunis', 'Africa/Tunis (+01:00)', $xRegion),
284+
array('America/Panama', 'America/Panama (-05:00)', $xRegion),
285+
array('Asia/Jerusalem', 'Asia/Jerusalem (+03:00)', $xRegion),
286+
array('Atlantic/Canary', 'Atlantic/Canary (+01:00)', $xRegion),
287+
array('Australia/Perth', 'Australia/Perth (+08:00)', $xRegion),
288+
array('Europe/Zurich', 'Europe/Zurich (+02:00)', $xRegion),
289+
array('Pacific/Tahiti', 'Pacific/Tahiti (-10:00)', $xRegion),
290+
);
291+
}
187292
}

src/Symfony/Component/VarDumper/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
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-intl": "To show region name in time zone dump",
3132
"ext-symfony_debug": ""
3233
},
3334
"autoload": {

0 commit comments

Comments
 (0)