Skip to content

Commit 4c4c398

Browse files
committed
Add period caster
1 parent 457d57b commit 4c4c398

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,40 @@ public static function castTimeZone(\DateTimeZone $timeZone, array $a, Stub $stu
8080
return $filter & Caster::EXCLUDE_VERBOSE ? $z : $z + $a;
8181
}
8282

83+
public static function castPeriod(\DatePeriod $p, array $a, Stub $stub, $isNested, $filter)
84+
{
85+
if (defined('HHVM_VERSION_ID') || \PHP_VERSION_ID < 50605) {
86+
return $a;
87+
}
88+
89+
$dates = array();
90+
if (\PHP_VERSION_ID >= 70107) { // see https://bugs.php.net/bug.php?id=74639
91+
foreach (clone $p as $i => $d) {
92+
if (3 === $i) {
93+
$now = new \DateTimeImmutable();
94+
$dates[] = sprintf('%s more', ($end = $p->getEndDate())
95+
? ceil(($end->format('U.u') - $d->format('U.u')) / ($now->add($p->getDateInterval())->format('U.u') - $now->format('U.u')))
96+
: $p->recurrences - $i
97+
);
98+
break;
99+
}
100+
$dates[] = sprintf('%s) %s', $i + 1, $d->format('Y-m-d H:i:s'));
101+
}
102+
}
103+
104+
$period = sprintf(
105+
'every %s, from %s (%s) %s',
106+
self::formatInterval($p->getDateInterval()),
107+
$p->getStartDate()->format('Y-m-d H:i:s'),
108+
$p->include_start_date ? 'included' : 'excluded',
109+
($end = $p->getEndDate()) ? 'to '.$end->format('Y-m-d H:i:s') : 'recurring '.$p->recurrences.' time/s'
110+
);
111+
112+
$p = array(Caster::PREFIX_VIRTUAL.'period' => new ConstStub($period, implode("\n", $dates)));
113+
114+
return $filter & Caster::EXCLUDE_VERBOSE ? $p : $p + $a;
115+
}
116+
83117
private static function formatSeconds($s, $us)
84118
{
85119
return sprintf('%02d.%s', $s, 0 === ($len = strlen($t = rtrim($us, '0'))) ? '0' : ($len <= 3 ? str_pad($t, 3, '0') : $us));

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ abstract class AbstractCloner implements ClonerInterface
112112
'DateTimeInterface' => array('Symfony\Component\VarDumper\Caster\DateCaster', 'castDateTime'),
113113
'DateInterval' => array('Symfony\Component\VarDumper\Caster\DateCaster', 'castInterval'),
114114
'DateTimeZone' => array('Symfony\Component\VarDumper\Caster\DateCaster', 'castTimeZone'),
115+
'DatePeriod' => array('Symfony\Component\VarDumper\Caster\DateCaster', 'castPeriod'),
115116

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

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

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,4 +296,97 @@ public function provideTimeZones()
296296
array('Pacific/Tahiti', 'Pacific/Tahiti (-10:00)', $xRegion),
297297
);
298298
}
299+
300+
/**
301+
* @dataProvider providePeriods
302+
*/
303+
public function testDumpPeriod($start, $interval, $end, $options, $expected)
304+
{
305+
if (defined('HHVM_VERSION_ID') || \PHP_VERSION_ID < 50605) {
306+
$this->markTestSkipped();
307+
}
308+
309+
$p = new \DatePeriod(new \DateTime($start), new \DateInterval($interval), is_int($end) ? $end : new \DateTime($end), $options);
310+
311+
$xDump = <<<EODUMP
312+
DatePeriod {
313+
period: $expected
314+
%A}
315+
EODUMP;
316+
317+
$this->assertDumpMatchesFormat($xDump, $p);
318+
}
319+
320+
/**
321+
* @dataProvider providePeriods
322+
*/
323+
public function testCastPeriod($start, $interval, $end, $options, $xPeriod, $xDates)
324+
{
325+
if (defined('HHVM_VERSION_ID') || \PHP_VERSION_ID < 50605) {
326+
$this->markTestSkipped();
327+
}
328+
329+
$p = new \DatePeriod(new \DateTime($start), new \DateInterval($interval), is_int($end) ? $end : new \DateTime($end), $options);
330+
$stub = new Stub();
331+
332+
$cast = DateCaster::castPeriod($p, array(), $stub, false, 0);
333+
334+
$xDump = <<<EODUMP
335+
array:1 [
336+
"\\x00~\\x00period" => $xPeriod
337+
]
338+
EODUMP;
339+
340+
$this->assertDumpMatchesFormat($xDump, $cast);
341+
342+
$xDump = <<<EODUMP
343+
Symfony\Component\VarDumper\Caster\ConstStub {
344+
+type: 1
345+
+class: "$xPeriod"
346+
+value: "%A$xDates%A"
347+
+cut: 0
348+
+handle: 0
349+
+refCount: 0
350+
+position: 0
351+
+attr: []
352+
}
353+
EODUMP;
354+
355+
$this->assertDumpMatchesFormat($xDump, $cast["\0~\0period"]);
356+
}
357+
358+
public function providePeriods()
359+
{
360+
$i = new \DateInterval('PT0S');
361+
$ms = \PHP_VERSION_ID >= 70100 && isset($i->f) ? '.0' : '';
362+
363+
$periods = array(
364+
array('2017-01-01', 'P1D', '2017-01-03', 0, 'every + 1d, from 2017-01-01 00:00:00 (included) to 2017-01-03 00:00:00', '1) 2017-01-01%a2) 2017-01-02'),
365+
array('2017-01-01', 'P1D', 1, 0, 'every + 1d, from 2017-01-01 00:00:00 (included) recurring 2 time/s', '1) 2017-01-01%a2) 2017-01-02'),
366+
367+
array('2017-01-01', 'P1D', '2017-01-04', 0, 'every + 1d, from 2017-01-01 00:00:00 (included) to 2017-01-04 00:00:00', '1) 2017-01-01%a2) 2017-01-02%a3) 2017-01-03'),
368+
array('2017-01-01', 'P1D', 2, 0, 'every + 1d, from 2017-01-01 00:00:00 (included) recurring 3 time/s', '1) 2017-01-01%a2) 2017-01-02%a3) 2017-01-03'),
369+
370+
array('2017-01-01', 'P1D', '2017-01-05', 0, 'every + 1d, from 2017-01-01 00:00:00 (included) to 2017-01-05 00:00:00', '1) 2017-01-01%a2) 2017-01-02%a1 more'),
371+
array('2017-01-01', 'P1D', 3, 0, 'every + 1d, from 2017-01-01 00:00:00 (included) recurring 4 time/s', '1) 2017-01-01%a2) 2017-01-02%a3) 2017-01-03%a1 more'),
372+
373+
array('2017-01-01', 'P1D', '2017-01-21', 0, 'every + 1d, from 2017-01-01 00:00:00 (included) to 2017-01-21 00:00:00', '1) 2017-01-01%a17 more'),
374+
array('2017-01-01', 'P1D', 19, 0, 'every + 1d, from 2017-01-01 00:00:00 (included) recurring 20 time/s', '1) 2017-01-01%a17 more'),
375+
376+
array('2017-01-01 01:00:00', 'P1D', '2017-01-03 01:00:00', 0, 'every + 1d, from 2017-01-01 01:00:00 (included) to 2017-01-03 01:00:00', '1) 2017-01-01 01:00:00%a2) 2017-01-02 01:00:00'),
377+
array('2017-01-01 01:00:00', 'P1D', 1, 0, 'every + 1d, from 2017-01-01 01:00:00 (included) recurring 2 time/s', '1) 2017-01-01 01:00:00%a2) 2017-01-02 01:00:00'),
378+
379+
array('2017-01-01', 'P1DT1H', '2017-01-03', 0, "every + 1d 01:00:00$ms, from 2017-01-01 00:00:00 (included) to 2017-01-03 00:00:00", '1) 2017-01-01 00:00:00%a2) 2017-01-02 01:00:00'),
380+
array('2017-01-01', 'P1DT1H', 1, 0, "every + 1d 01:00:00$ms, from 2017-01-01 00:00:00 (included) recurring 2 time/s", '1) 2017-01-01 00:00:00%a2) 2017-01-02 01:00:00'),
381+
382+
array('2017-01-01', 'P1D', '2017-01-04', \DatePeriod::EXCLUDE_START_DATE, 'every + 1d, from 2017-01-01 00:00:00 (excluded) to 2017-01-04 00:00:00', '1) 2017-01-02%a2) 2017-01-03'),
383+
array('2017-01-01', 'P1D', 2, \DatePeriod::EXCLUDE_START_DATE, 'every + 1d, from 2017-01-01 00:00:00 (excluded) recurring 2 time/s', '1) 2017-01-02%a2) 2017-01-03'),
384+
);
385+
386+
if (\PHP_VERSION_ID < 70107) {
387+
array_walk($periods, function (&$i) { $i[5] = ''; });
388+
}
389+
390+
return $periods;
391+
}
299392
}

0 commit comments

Comments
 (0)