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

Skip to content

Commit c49895f

Browse files
Merge branch '3.4'
* 3.4: Add time zone caster
2 parents 9c4c0f3 + 04f3e60 commit c49895f

File tree

4 files changed

+107
-1
lines changed

4 files changed

+107
-1
lines changed

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

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

6464
return $i->format(rtrim($format));
6565
}
66+
67+
public static function castTimeZone(\DateTimeZone $timeZone, array $a, Stub $stub, $isNested, $filter)
68+
{
69+
$location = $timeZone->getLocation();
70+
$formatted = (new \Datetime('now', $timeZone))->format($location ? 'e (P)' : 'P');
71+
$title = $location && extension_loaded('intl') ? \Locale::getDisplayRegion('-'.$location['country_code'], \Locale::getDefault()) : '';
72+
73+
$z = array(Caster::PREFIX_VIRTUAL.'timezone' => new ConstStub($formatted, $title));
74+
75+
return $filter & Caster::EXCLUDE_VERBOSE ? $z : $z + $a;
76+
}
6677
}

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: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,97 @@ public function provideIntervals()
177177
array('P1Y2M3DT4H5M6S', 1, '- 1y 2m 3d 04:05:06.000000', null),
178178
);
179179
}
180+
181+
/**
182+
* @dataProvider provideTimeZones
183+
*/
184+
public function testDumpTimeZone($timezone, $expected)
185+
{
186+
$timezone = new \DateTimeZone($timezone);
187+
188+
$xDump = <<<EODUMP
189+
DateTimeZone {
190+
timezone: $expected
191+
%A}
192+
EODUMP;
193+
194+
$this->assertDumpMatchesFormat($xDump, $timezone);
195+
}
196+
197+
/**
198+
* @dataProvider provideTimeZones
199+
*/
200+
public function testDumpTimeZoneExcludingVerbosity($timezone, $expected)
201+
{
202+
$timezone = new \DateTimeZone($timezone);
203+
204+
$xDump = <<<EODUMP
205+
DateTimeZone {
206+
timezone: $expected
207+
}
208+
EODUMP;
209+
210+
$this->assertDumpMatchesFormat($xDump, $timezone, Caster::EXCLUDE_VERBOSE);
211+
}
212+
213+
/**
214+
* @dataProvider provideTimeZones
215+
*/
216+
public function testCastTimeZone($timezone, $xTimezone, $xRegion)
217+
{
218+
$timezone = new \DateTimeZone($timezone);
219+
$stub = new Stub();
220+
221+
$cast = DateCaster::castTimeZone($timezone, array('foo' => 'bar'), $stub, false, Caster::EXCLUDE_VERBOSE);
222+
223+
$xDump = <<<EODUMP
224+
array:1 [
225+
"\\x00~\\x00timezone" => $xTimezone
226+
]
227+
EODUMP;
228+
229+
$this->assertDumpMatchesFormat($xDump, $cast);
230+
231+
$xDump = <<<EODUMP
232+
Symfony\Component\VarDumper\Caster\ConstStub {
233+
+type: "ref"
234+
+class: "$xTimezone"
235+
+value: "$xRegion"
236+
+cut: 0
237+
+handle: 0
238+
+refCount: 0
239+
+position: 0
240+
+attr: []
241+
}
242+
EODUMP;
243+
244+
$this->assertDumpMatchesFormat($xDump, $cast["\0~\0timezone"]);
245+
}
246+
247+
public function provideTimeZones()
248+
{
249+
$xRegion = extension_loaded('intl') ? '%s' : '';
250+
251+
return array(
252+
// type 1 (UTC offset)
253+
array('-12:00', '-12:00', ''),
254+
array('+00:00', '+00:00', ''),
255+
array('+14:00', '+14:00', ''),
256+
257+
// type 2 (timezone abbreviation)
258+
array('GMT', '+00:00', ''),
259+
array('a', '+01:00', ''),
260+
array('b', '+02:00', ''),
261+
array('z', '+00:00', ''),
262+
263+
// type 3 (timezone identifier)
264+
array('Africa/Tunis', 'Africa/Tunis (+01:00)', $xRegion),
265+
array('America/Panama', 'America/Panama (-05:00)', $xRegion),
266+
array('Asia/Jerusalem', 'Asia/Jerusalem (+03:00)', $xRegion),
267+
array('Atlantic/Canary', 'Atlantic/Canary (+01:00)', $xRegion),
268+
array('Australia/Perth', 'Australia/Perth (+08:00)', $xRegion),
269+
array('Europe/Zurich', 'Europe/Zurich (+02:00)', $xRegion),
270+
array('Pacific/Tahiti', 'Pacific/Tahiti (-10:00)', $xRegion),
271+
);
272+
}
180273
}

src/Symfony/Component/VarDumper/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"phpunit/phpunit": "<4.8.35|<5.4.3,>=5.0"
2828
},
2929
"suggest": {
30-
"ext-iconv": "To convert non-UTF-8 strings to UTF-8 (or symfony/polyfill-iconv in case ext-iconv cannot be used)."
30+
"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
},
3233
"autoload": {
3334
"files": [ "Resources/functions/dump.php" ],

0 commit comments

Comments
 (0)