8000 Add interval caster · symfony/symfony@57a2696 · GitHub
[go: up one dir, main page]

Skip to content

Commit 57a2696

Browse files
committed
Add interval caster
1 parent 3c95fb9 commit 57a2696

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

src/Symfony/Component/VarDumper/Caster/DateCaster.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static function castDate(\DateTimeInterface $d, array $a, Stub $stub, $is
2727
$fromNow = (new \DateTime())->diff($d);
2828

2929
$title = $d->format('l, F j, Y')
30-
."\n".$fromNow->format('%R').(ltrim($fromNow->format('%yy %mm %dd %H:%I:%Ss'), ' 0ymd:s') ?: '0s').' from now'
30+
."\n".$fromNow->format('%R').self::formatInterval($fromNow).' from now'
3131
.($location ? ($d->format('I') ? "\nDST On" : "\nDST Off") : '')
3232
;
3333

@@ -38,4 +38,29 @@ public static function castDate(\DateTimeInterface $d, array $a, Stub $stub, $is
3838

3939
return $a;
4040
}
41+
42+
public static function castInterval(\DateInterval $i, array $a, Stub $stub, $isNested, $filter)
43+
{
44+
$prefix = Caster::PREFIX_VIRTUAL;
45+
46+
$a = array(
47+
$prefix.'interval' => self::formatInterval($i),
48+
);
49+
50+
return $a;
51+
}
52+
53+
private static function formatInterval(\DateInterval $i)
54+
{
55+
$format = '%R '
56+
.($i->y ? '%y year(s) ' : '')
57+
.($i->m ? '%m month(s) ' : '')
58+
.($i->d ? '%d day(s) ' : '')
59+
.($i->h || $i->i || $i->s ? '%H:%I:%S' : '')
60+
;
61+
62+
$format = '%R ' === $format ? '0' : $format;
63+
64+
return $i->format(rtrim($format));
65+
}
4166
}

src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ abstract class AbstractCloner implements ClonerInterface
110110
'RedisArray' => array('Symfony\Component\VarDumper\Caster\RedisCaster', 'castRedisArray'),
111111

112112
'DateTimeInterface' => array('Symfony\Component\VarDumper\Caster\DateCaster', 'castDate'),
113+
'DateInterval' => array('Symfony\Component\VarDumper\Caster\DateCaster', 'castInterval'),
113114

114115
':curl' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castCurl'),
115116
':dba' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castDba'),

src/Symfony/Component/VarDumper/Tests/Caster/DateCasterTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,44 @@ public function provideDates()
8484
array('2017-04-30 00:00:00.000000', '+02:00', '2017-04-30 00:00:00.000000 +02:00'),
8585
);
8686
}
87+
88+
/**
89+
* @dataProvider provideIntervals
90+
*/
91+
public function testCastInterval($interval_spec, $invert, $expected)
92+
{
93+
$interval = new \DateInterval($interval_spec);
94+
$interval->invert = $invert;
95+
96+
$xDump = <<<EODUMP
97+
DateInterval {
98+
interval: "$expected"
99+
}
100+
EODUMP;
101+
102+
$this->assertDumpMatchesFormat($xDump, $interval);
103+
}
104+
105+
public function provideIntervals()
106+
{
107+
return array(
108+
array('PT0S', 0, '0'),
109+
array('PT1S', 0, '+ 00:00:01'),
110+
array('PT2M', 0, '+ 00:02:00'),
111+
array('PT3H', 0, '+ 03:00:00'),
112+
array('P4D', 0, '+ 4 day(s)'),
113+
array('P5M', 0, '+ 5 month(s)'),
114+
array('P6Y', 0, '+ 6 year(s)'),
115+
array('P1Y2M3DT4H5M6S', 0, '+ 1 year(s) 2 month(s) 3 day(s) 04:05:06'),
116+
117+
array('PT0S', 1, '0'),
118+
array('PT1S', 1, '- 00:00:01'),
119+
array('PT2M', 1, '- 00:02:00'),
120+
array('PT3H', 1, '- 03:00:00'),
121+
array('P4D', 1, '- 4 day(s)'),
122+
array('P5M', 1, '- 5 month(s)'),
123+
array('P6Y', 1, '- 6 year(s)'),
124+
array('P1Y2M3DT4H5M6S', 1, '- 1 year(s) 2 month(s) 3 day(s) 04:05:06'),
125+
);
126+
}
87127
}

0 commit comments

Comments
 (0)
0