From 5c4bfacdef92fd4ea66b67c734b7857cb7e43005 Mon Sep 17 00:00:00 2001 From: Dany Maillard Date: Wed, 19 Jul 2017 15:26:15 +0200 Subject: [PATCH] Add time zone caster --- .../Component/VarDumper/Caster/DateCaster.php | 11 ++ .../VarDumper/Cloner/AbstractCloner.php | 1 + .../VarDumper/Tests/Caster/DateCasterTest.php | 105 ++++++++++++++++++ src/Symfony/Component/VarDumper/composer.json | 1 + 4 files changed, 118 insertions(+) diff --git a/src/Symfony/Component/VarDumper/Caster/DateCaster.php b/src/Symfony/Component/VarDumper/Caster/DateCaster.php index b96660605f55..5a59ca48e1e7 100644 --- a/src/Symfony/Component/VarDumper/Caster/DateCaster.php +++ b/src/Symfony/Component/VarDumper/Caster/DateCaster.php @@ -68,4 +68,15 @@ private static function formatInterval(\DateInterval $i) 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; + } } diff --git a/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php b/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php index 14d76bd5ed36..7b5093c44bff 100644 --- a/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php +++ b/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php @@ -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'), diff --git a/src/Symfony/Component/VarDumper/Tests/Caster/DateCasterTest.php b/src/Symfony/Component/VarDumper/Tests/Caster/DateCasterTest.php index 637604e24dc1..1c395e56c429 100644 --- a/src/Symfony/Component/VarDumper/Tests/Caster/DateCasterTest.php +++ b/src/Symfony/Component/VarDumper/Tests/Caster/DateCasterTest.php @@ -184,4 +184,109 @@ public function provideIntervals() 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 = <<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 = <<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 = << $xTimezone +] +EODUMP; + + $this->assertDumpMatchesFormat($xDump, $cast); + + $xDump = <<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), + ); + } } diff --git a/src/Symfony/Component/VarDumper/composer.json b/src/Symfony/Component/VarDumper/composer.json index 32cf575ed4eb..2cf287b4f217 100644 --- a/src/Symfony/Component/VarDumper/composer.json +++ b/src/Symfony/Component/VarDumper/composer.json @@ -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": {