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 04f3e60

Browse filesBrowse files
feature #23591 [VarDumper] Add time zone caster (maidmaid)
This PR was merged into the 3.4 branch. Discussion ---------- [VarDumper] Add time zone caster | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #22431 (comment) | License | MIT | Doc PR | / Commits ------- 5c4bfac Add time zone caster
2 parents 6dc5f59 + 5c4bfac commit 04f3e60
Copy full SHA for 04f3e60

File tree

4 files changed

+118
-0
lines changed
Filter options

4 files changed

+118
-0
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/VarDumper/Caster/DateCaster.php
+11Lines 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

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
+105Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/VarDumper/composer.json
+1Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.