Add time zone caster
This commit is contained in:
parent
6dc5f59ae3
commit
5c4bfacdef
@ -68,4 +68,15 @@ class DateCaster
|
||||
|
||||
return $i->format(rtrim($format));
|
||||
}
|
||||
|
||||
public static function castTimeZone(\DateTimeZone $timeZone, array $a, Stub $stub, $isNested, $filter)
|
||||
{
|
||||
$location = $timeZone->getLocation();
|
||||
$formatted = (new \Datetime('now', $timeZone))->format($location ? 'e (P)' : 'P');
|
||||
$title = $location && extension_loaded('intl') ? \Locale::getDisplayRegion('-'.$location['country_code'], \Locale::getDefault()) : '';
|
||||
|
||||
$z = array(Caster::PREFIX_VIRTUAL.'timezone' => new ConstStub($formatted, $title));
|
||||
|
||||
return $filter & Caster::EXCLUDE_VERBOSE ? $z : $z + $a;
|
||||
}
|
||||
}
|
||||
|
@ -111,6 +111,7 @@ abstract class AbstractCloner implements ClonerInterface
|
||||
|
||||
'DateTimeInterface' => array('Symfony\Component\VarDumper\Caster\DateCaster', 'castDateTime'),
|
||||
'DateInterval' => array('Symfony\Component\VarDumper\Caster\DateCaster', 'castInterval'),
|
||||
'DateTimeZone' => array('Symfony\Component\VarDumper\Caster\DateCaster', 'castTimeZone'),
|
||||
|
||||
':curl' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castCurl'),
|
||||
':dba' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castDba'),
|
||||
|
@ -184,4 +184,109 @@ EODUMP;
|
||||
array('P1Y2M3DT4H5M6S', 1, '- 1y 2m 3d 04:05:06'.$ms, null),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideTimeZones
|
||||
*/
|
||||
public function testDumpTimeZone($timezone, $expected)
|
||||
{
|
||||
if ((defined('HHVM_VERSION_ID') || PHP_VERSION_ID <= 50509) && !preg_match('/\w+\/\w+/', $timezone)) {
|
||||
$this->markTestSkipped('DateTimeZone GMT offsets are supported since 5.5.10. See https://github.com/facebook/hhvm/issues/5875 for HHVM.');
|
||||
}
|
||||
|
||||
$timezone = new \DateTimeZone($timezone);
|
||||
|
||||
$xDump = <<<EODUMP
|
||||
DateTimeZone {
|
||||
timezone: $expected
|
||||
%A}
|
||||
EODUMP;
|
||||
|
||||
$this->assertDumpMatchesFormat($xDump, $timezone);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideTimeZones
|
||||
*/
|
||||
public function testDumpTimeZoneExcludingVerbosity($timezone, $expected)
|
||||
{
|
||||
if ((defined('HHVM_VERSION_ID') || PHP_VERSION_ID <= 50509) && !preg_match('/\w+\/\w+/', $timezone)) {
|
||||
$this->markTestSkipped('DateTimeZone GMT offsets are supported since 5.5.10. See https://github.com/facebook/hhvm/issues/5875 for HHVM.');
|
||||
}
|
||||
|
||||
$timezone = new \DateTimeZone($timezone);
|
||||
|
||||
$xDump = <<<EODUMP
|
||||
DateTimeZone {
|
||||
timezone: $expected
|
||||
}
|
||||
EODUMP;
|
||||
|
||||
$this->assertDumpMatchesFormat($xDump, $timezone, Caster::EXCLUDE_VERBOSE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideTimeZones
|
||||
*/
|
||||
public function testCastTimeZone($timezone, $xTimezone, $xRegion)
|
||||
{
|
||||
if ((defined('HHVM_VERSION_ID') || PHP_VERSION_ID <= 50509) && !preg_match('/\w+\/\w+/', $timezone)) {
|
||||
$this->markTestSkipped('DateTimeZone GMT offsets are supported since 5.5.10. See https://github.com/facebook/hhvm/issues/5875 for HHVM.');
|
||||
}
|
||||
|
||||
$timezone = new \DateTimeZone($timezone);
|
||||
$stub = new Stub();
|
||||
|
||||
$cast = DateCaster::castTimeZone($timezone, array('foo' => 'bar'), $stub, false, Caster::EXCLUDE_VERBOSE);
|
||||
|
||||
$xDump = <<<EODUMP
|
||||
array:1 [
|
||||
"\\x00~\\x00timezone" => $xTimezone
|
||||
]
|
||||
EODUMP;
|
||||
|
||||
$this->assertDumpMatchesFormat($xDump, $cast);
|
||||
|
||||
$xDump = <<<EODUMP
|
||||
Symfony\Component\VarDumper\Caster\ConstStub {
|
||||
+type: "ref"
|
||||
+class: "$xTimezone"
|
||||
+value: "$xRegion"
|
||||
+cut: 0
|
||||
+handle: 0
|
||||
+refCount: 0
|
||||
+position: 0
|
||||
+attr: []
|
||||
}
|
||||
EODUMP;
|
||||
|
||||
$this->assertDumpMatchesFormat($xDump, $cast["\0~\0timezone"]);
|
||||
}
|
||||
|
||||
public function provideTimeZones()
|
||||
{
|
||||
$xRegion = extension_loaded('intl') ? '%s' : '';
|
||||
|
||||
return array(
|
||||
// type 1 (UTC offset)
|
||||
array('-12:00', '-12:00', ''),
|
||||
array('+00:00', '+00:00', ''),
|
||||
array('+14:00', '+14:00', ''),
|
||||
|
||||
// type 2 (timezone abbreviation)
|
||||
array('GMT', '+00:00', ''),
|
||||
array('a', '+01:00', ''),
|
||||
array('b', '+02:00', ''),
|
||||
array('z', '+00:00', ''),
|
||||
|
||||
// type 3 (timezone identifier)
|
||||
array('Africa/Tunis', 'Africa/Tunis (+01:00)', $xRegion),
|
||||
array('America/Panama', 'America/Panama (-05:00)', $xRegion),
|
||||
array('Asia/Jerusalem', 'Asia/Jerusalem (+03:00)', $xRegion),
|
||||
array('Atlantic/Canary', 'Atlantic/Canary (+01:00)', $xRegion),
|
||||
array('Australia/Perth', 'Australia/Perth (+08:00)', $xRegion),
|
||||
array('Europe/Zurich', 'Europe/Zurich (+02:00)', $xRegion),
|
||||
array('Pacific/Tahiti', 'Pacific/Tahiti (-10:00)', $xRegion),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -28,6 +28,7 @@
|
||||
},
|
||||
"suggest": {
|
||||
"ext-iconv": "To convert non-UTF-8 strings to UTF-8 (or symfony/polyfill-iconv in case ext-iconv cannot be used).",
|
||||
"ext-intl": "To show region name in time zone dump",
|
||||
"ext-symfony_debug": ""
|
||||
},
|
||||
"autoload": {
|
||||
|
Reference in New Issue
Block a user