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

[VarDumper] Add time zone caster #23591

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add time zone caster
  • Loading branch information
maidmaid committed Jul 21, 2017
commit 5c4bfacdef92fd4ea66b67c734b7857cb7e43005
11 changes: 11 additions & 0 deletions 11 src/Symfony/Component/VarDumper/Caster/DateCaster.php
Original file line number Diff line number Diff line change
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()) : '';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just realized, does it make sense to use Locale::getDefault() instead of just en. Not sure this output needs to be localized at all 😕

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dump output is already locale sensitive, so this makes sense to me

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

date format in castDateTime is not.. which made me think this should follow.

No blocker for me or so :)


$z = array(Caster::PREFIX_VIRTUAL.'timezone' => new ConstStub($formatted, $title));

return $filter & Caster::EXCLUDE_VERBOSE ? $z : $z + $a;
}
}
1 change: 1 addition & 0 deletions 1 src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php
Original file line number Diff line number Diff line change
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'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im assuming next is DatePeriod for completion? That can be easily wired now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! But let's first fully agree on time zone 😅


':curl' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castCurl'),
':dba' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castDba'),
Expand Down
105 changes: 105 additions & 0 deletions 105 src/Symfony/Component/VarDumper/Tests/Caster/DateCasterTest.php
Original file line number Diff line number Diff line change
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 1 src/Symfony/Component/VarDumper/composer.json
Original file line number Diff line number Diff line change
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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.