Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2a64de6

Browse files
committed
Add interval caster
1 parent 5a3abf5 commit 2a64de6

File tree

3 files changed

+132
-1
lines changed

3 files changed

+132
-1
lines changed

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

Lines changed: 31 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,34 @@ public static function castDateTime(\DateTimeInterface $d, array $a, Stub $stub,
3838

3939
return $a;
4040
}
41+
42+
public static function castInterval(\DateInterval $interval, array $a, Stub $stub, $isNested, $filter)
43+
{
44+
$now = new \DateTimeImmutable();
45+
$numberOfSeconds = $now->add($interval)->getTimestamp() - $now->getTimestamp();
46+
$title = number_format($numberOfSeconds, 0, '.', ' ').'s';
47+
48+
$i = array(Caster::PREFIX_VIRTUAL.'interval' => new ConstStub(self::formatInterval($interval), $title));
49+
50+
return $filter & Caster::EXCLUDE_VERBOSE ? $i : $i + $a;
51+
}
52+
53+
private static function formatInterval(\DateInterval $i)
54+
{
55+
$format = '%R '
56+
.($i->y ? '%yy ' : '')
57+
.($i->m ? '%mm ' : '')
58+
.($i->d ? '%dd ' : '')
59+
;
60+
61+
if (\PHP_VERSION_ID >= 70100) {
62+
$format .= $i->h || $i->i || $i->s || $i->f ? '%H:%I:%S.%F' : '';
63+
} else {
64+
$format .= $i->h || $i->i || $i->s ? '%H:%I:%S' : '';
65+
}
66+
67+
$format = '%R ' === $format ? '0s' : $format;
68+
69+
return $i->format(rtrim($format));
70+
}
4171
}

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: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\VarDumper\Tests\Caster;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\VarDumper\Caster\Caster;
1516
use Symfony\Component\VarDumper\Caster\DateCaster;
1617
use Symfony\Component\VarDumper\Cloner\Stub;
1718
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
@@ -84,4 +85,103 @@ public function provideDateTimes()
8485
array('2017-04-30 00:00:00.000000', '+02:00', '2017-04-30 00:00:00.000000 +02:00'),
8586
);
8687
}
88+
89+
/**
90+
* @dataProvider provideIntervals
91+
*/
92+
public function testDumpInterval($intervalSpec, $invert, $expected)
93+
{
94+
$interval = new \DateInterval($intervalSpec);
95+
$interval->invert = $invert;
96+
97+
$xDump = <<<EODUMP
98+
DateInterval {
99+
interval: $expected
100+
%a
101+
}
102+
EODUMP;
103+
104+
$this->assertDumpMatchesFormat($xDump, $interval);
105+
}
106+
107+
/**
108+
* @dataProvider provideIntervals
109+
*/
110+
public function testDumpIntervalExcludingVerbosity($intervalSpec, $invert, $expected)
111+
{
112+
$interval = new \DateInterval($intervalSpec);
113+
$interval->invert = $invert;
114+
115+
$xDump = <<<EODUMP
116+
DateInterval {
117+
interval: $expected
118+
}
119+
EODUMP;
120+
121+
$this->assertDumpMatchesFormat($xDump, $interval, Caster::EXCLUDE_VERBOSE);
122+
}
123+
124+
/**
125+
* @dataProvider provideIntervals
126+
*/
127+
public function testCastInterval($intervalSpec, $invert, $xInterval, $xSeconds)
128+
{
129+
$interval = new \DateInterval($intervalSpec);
130+
$interval->invert = $invert;
131+
$stub = new Stub();
132+
133+
$cast = DateCaster::castInterval($interval, array('foo' => 'bar'), $stub, false, Caster::EXCLUDE_VERBOSE);
134+
135+
$xDump = <<<EODUMP
136+
array:1 [
137+
"\\x00~\\x00interval" => $xInterval
138+
]
139+
EODUMP;
140+
141+
$this->assertDumpMatchesFormat($xDump, $cast);
142+
143+
if (null === $xSeconds) {
144+
return;
145+
}
146+
147+
$xDump = <<<EODUMP
148+
Symfony\Component\VarDumper\Caster\ConstStub {
149+
+type: "ref"
150+
+class: "$xInterval"
151+
+value: "$xSeconds"
152+
+cut: 0
153+
+handle: 0
154+
+refCount: 0
155+
+position: 0
156+
+attr: []
157+
}
158+
EODUMP;
159+
160+
$this->assertDumpMatchesFormat($xDump, $cast["\0~\0interval"]);
161+
}
162+
163+
public function provideIntervals()
164+
{
165+
$ms = \PHP_VERSION_ID >= 70100 ? '.000000' : '';
166+
167+
return array(
168+
array('PT0S', 0, '0s', '0s'),
169+
array('PT1S', 0, '+ 00:00:01'.$ms, '1s'),
170+
array('PT2M', 0, '+ 00:02:00'.$ms, '120s'),
171+
array('PT3H', 0, '+ 03:00:00'.$ms, '10 800s'),
172+
array('P4D', 0, '+ 4d', '345 600s'),
173+
array('P5M', 0, '+ 5m', null),
174+
array('P6Y', 0, '+ 6y', null),
175+
array('P1Y2M3DT4H5M6S', 0, '+ 1y 2m 3d 04:05:06'.$ms, null),
176+
177+
array('PT0S', 1, '0s', '0s'),
178+
array('PT1S', 1, '- 00:00:01'.$ms, '-1s'),
179+
array('PT2M', 1, '- 00:02:00'.$ms, '-120s'),
180+
array('PT3H', 1, '- 03:00:00'.$ms, '-10 800s'),
181+
array('P4D', 1, '- 4d', '-345 600s'),
182+
array('P5M', 1, '- 5m', null),
183+
array('P6Y', 1, '- 6y', null),
184+
array('P1Y2M3DT4H5M6S', 1, '- 1y 2m 3d 04:05:06'.$ms, null),
185+
);
186+
}
87187
}

0 commit comments

Comments
 (0)