-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[VarDumper] Add date caster #22431
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
[VarDumper] Add date caster #22431
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\VarDumper\Caster; | ||
|
||
use Symfony\Component\VarDumper\Cloner\Stub; | ||
|
||
/** | ||
* Casts DateTimeInterface related classes to array representation. | ||
* | ||
* @author Dany Maillard <danymaillard93b@gmail.com> | ||
*/ | ||
class DateCaster | ||
{ | ||
public static function castDateTime(\DateTimeInterface $d, array $a, Stub $stub, $isNested, $filter) | ||
{ | ||
$prefix = Caster::PREFIX_VIRTUAL; | ||
$location = $d->getTimezone()->getLocation(); | ||
$fromNow = (new \DateTime())->diff($d); | ||
|
||
$title = $d->format('l, F j, Y') | ||
."\n".$fromNow->format('%R').(ltrim($fromNow->format('%yy %mm %dd %H:%I:%Ss'), ' 0ymd:s') ?: '0s').' from now' | ||
.($location ? ($d->format('I') ? "\nDST On" : "\nDST Off") : '') | ||
; | ||
|
||
$a = array(); | ||
$a[$prefix.'date'] = new ConstStub($d->format('Y-m-d H:i:s.u '.($location ? 'e (P)' : 'P')), $title); | ||
|
||
$stub->class .= $d->format(' @U'); | ||
|
||
return $a; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/Symfony/Component/VarDumper/Tests/Caster/DateCasterTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\VarDumper\Tests\Caster; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\VarDumper\Caster\DateCaster; | ||
use Symfony\Component\VarDumper\Cloner\Stub; | ||
use Symfony\Component\VarDumper\Test\VarDumperTestTrait; | ||
|
||
/** | ||
* @author Dany Maillard <danymaillard93b@gmail.com> | ||
*/ | ||
class DateCasterTest extends TestCase | ||
{ | ||
use VarDumperTestTrait; | ||
|
||
/** | ||
* @dataProvider provideDateTimes | ||
*/ | ||
public function testDumpDateTime($time, $timezone, $expected) | ||
{ | ||
if ((defined('HHVM_VERSION_ID') || PHP_VERSION_ID <= 50509) && preg_match('/[-+]\d{2}:\d{2}/', $timezone)) { | ||
$this->markTestSkipped('DateTimeZone GMT offsets are supported since 5.5.10. See https://github.com/facebook/hhvm/issues/5875 for HHVM.'); | ||
} | ||
|
||
$date = new \DateTime($time, new \DateTimeZone($timezone)); | ||
|
||
$xDump = <<<EODUMP | ||
DateTime @1493503200 { | ||
date: $expected | ||
} | ||
EODUMP; | ||
|
||
$this->assertDumpMatchesFormat($xDump, $date); | ||
} | ||
|
||
public function testCastDateTime() | ||
{ | ||
$stub = new Stub(); | ||
$date = new \DateTime('2017-08-30 00:00:00.000000', new \DateTimeZone('Europe/Zurich')); | ||
$cast = DateCaster::castDateTime($date, array('foo' => 'bar'), $stub, false, 0); | ||
|
||
$xDump = <<<'EODUMP' | ||
array:1 [ | ||
"\x00~\x00date" => 2017-08-30 00:00:00.000000 Europe/Zurich (+02:00) | ||
] | ||
EODUMP; | ||
|
||
$this->assertDumpMatchesFormat($xDump, $cast); | ||
|
||
$xDump = <<<'EODUMP' | ||
Symfony\Component\VarDumper\Caster\ConstStub { | ||
+type: "ref" | ||
+class: "2017-08-30 00:00:00.000000 Europe/Zurich (+02:00)" | ||
+value: """ | ||
Wednesday, August 30, 2017\n | ||
+%a from now\n | ||
DST On | ||
""" | ||
+cut: 0 | ||
+handle: 0 | ||
+refCount: 0 | ||
+position: 0 | ||
+attr: [] | ||
} | ||
EODUMP; | ||
|
||
$this->assertDumpMatchesFormat($xDump, $cast["\0~\0date"]); | ||
} | ||
|
||
public function provideDateTimes() | ||
{ | ||
return array( | ||
array('2017-04-30 00:00:00.000000', 'Europe/Zurich', '2017-04-30 00:00:00.000000 Europe/Zurich (+02:00)'), | ||
array('2017-04-30 00:00:00.000000', '+02:00', '2017-04-30 00:00:00.000000 +02:00'), | ||
); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be named
DateTimeCaster
IMOThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In a next PR, I would cast other date classes like
DateInterval
. It's why a genericDateCaster
is better IMO.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why waiting?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, it would be better to have everything in the same PR.