10000 Add interval caster · symfony/symfony@00552e1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 00552e1

Browse files
committed
Add interval caster
1 parent 5a3abf5 commit 00552e1

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static function castDateTime(\DateTimeInterface $d, array $a, Stub $stub,
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,36 @@ public static function castDateTime(\DateTimeInterface $d, array $a, Stub $stub,
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+
Caster::PREFIX_DYNAMIC.'invert' => $i->invert,
49+
Caster::PREFIX_DYNAMIC.'days' => $i->days,
50+
);
51+
52+
return $a;
53+
}
54+
55+
private static function formatInterval(\DateInterval $i)
56+
{
57+
$format = '%R '
58+
.($i->y ? '%yy ' : '')
59+
.($i->m ? '%mm ' : '')
60+
.($i->d ? '%dd ' : '')
61+
;
62+
63+
if (PHP_VERSION_ID >= 70100) {
64+
$format .= $i->h || $i->i || $i->s || $i->f ? '%H:%I:%S.%F' : '';
65+
} else {
66+
$format .= $i->h || $i->i || $i->s ? '%H:%I:%S' : '';
67+
}
68+
69+
$format = '%R ' === $format ? '0' : $format;
70+
71+
return $i->format(rtrim($format));
72+
}
4173
}

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', 'castDateTime'),
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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,48 @@ public function provideDateTimes()
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+
+"invert": $invert
100+
+"days": false
101+
}
102+
EODUMP;
103+
104+
$this->assertDumpMatchesFormat($xDump, $interval);
105+
}
106+
107+
public function provideIntervals()
108+
{
109+
$ms = function () { return PHP_VERSION_ID >= 70100 ? '.000000' : ''; };
110+
111+
return array(
112+
array('PT0S', 0, '0', false),
113+
array('PT1S', 0, '+ 00:00:01'.$ms(), '.0'),
114+
array('PT2M', 0, '+ 00:02:00'.$ms(), '.0'),
115+
array('PT3H', 0, '+ 03:00:00'.$ms(), '.0'),
116+
array('P4D', 0, '+ 4d'),
117+
array('P5M', 0, '+ 5m'),
118+
array('P6Y', 0, '+ 6y'),
119+
array('P1Y2M3DT4H5M6S', 0, '+ 1y 2m 3d 04:05:06'.$ms()),
120+
121+
array('PT0S', 1, '0'),
122+
array('PT1S', 1, '- 00:00:01'.$ms()),
123+
array('PT2M', 1, '- 00:02:00'.$ms()),
124+
array('PT3H', 1, '- 03:00:00'.$ms()),
125+
array('P4D', 1, '- 4d'),
126+
array('P5M', 1, '- 5m'),
127+
array('P6Y', 1, '- 6y'),
128+
array('P1Y2M3DT4H5M6S', 1, '- 1y 2m 3d 04:05:06'.$ms()),
129+
);
130+
}
87131
}

0 commit comments

Comments
 (0)
0