Skip to content

Commit

Permalink
feature #23591 [VarDumper] Add time zone caster (maidmaid)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nicolas-grekas committed Jul 21, 2017
2 parents 6dc5f59 + 5c4bfac commit 04f3e60
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Symfony/Component/VarDumper/Caster/DateCaster.php
Expand Up @@ -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;
}
}
1 change: 1 addition & 0 deletions src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php
Expand Up @@ -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'),
Expand Down
105 changes: 105 additions & 0 deletions src/Symfony/Component/VarDumper/Tests/Caster/DateCasterTest.php
Expand Up @@ -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 = <<<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),
);
}
}
1 change: 1 addition & 0 deletions src/Symfony/Component/VarDumper/composer.json
Expand Up @@ -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": {
Expand Down

0 comments on commit 04f3e60

Please sign in to comment.