Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 660fecc

Browse filesBrowse files
feature #23668 [VarDumper] Add period caster (maidmaid)
This PR was merged into the 3.4 branch. Discussion ---------- [VarDumper] Add period caster | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #23591 (comment) | License | MIT | Doc PR | / Result: ![](https://user-images.githubusercontent.com/4578773/29788181-fce3eb32-8c31-11e7-9da4-72c038d5a14e.png) Commits ------- 4c4c398 Add period caster
2 parents 0096738 + 4c4c398 commit 660fecc
Copy full SHA for 660fecc

File tree

3 files changed

+128
-0
lines changed
Filter options

3 files changed

+128
-0
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/VarDumper/Caster/DateCaster.php
+34Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,40 @@ public static function castTimeZone(\DateTimeZone $timeZone, array $a, Stub $stu
8383
return $filter & Caster::EXCLUDE_VERBOSE ? $z : $z + $a;
8484
}
8585

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php
+1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/VarDumper/Tests/Caster/DateCasterTest.php
+93Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,4 +308,97 @@ public function provideTimeZones()
308308
array('Pacific/Tahiti', 'Pacific/Tahiti (-10:00)', $xRegion),
309309
);
310310
}
311+
312+
/**
313+
* @dataProvider providePeriods
314+
*/
315+
public function testDumpPeriod($start, $interval, $end, $options, $expected)
316+
{
317+
if (defined('HHVM_VERSION_ID') || \PHP_VERSION_ID < 50605) {
318+
$this->markTestSkipped();
319+
}
320+
321+
$p = new \DatePeriod(new \DateTime($start), new \DateInterval($interval), is_int($end) ? $end : new \DateTime($end), $options);
322+
323+
$xDump = <<<EODUMP
324+
DatePeriod {
325+
period: $expected
326+
%A}
327+
EODUMP;
328+
329+
$this->assertDumpMatchesFormat($xDump, $p);
330+
}
331+
332+
/**
333+
* @dataProvider providePeriods
334+
*/
335+
public function testCastPeriod($start, $interval, $end, $options, $xPeriod, $xDates)
336+
{
337+
if (defined('HHVM_VERSION_ID') || \PHP_VERSION_ID < 50605) {
338+
$this->markTestSkipped();
339+
}
340+
341+
$p = new \DatePeriod(new \DateTime($start), new \DateInterval($interval), is_int($end) ? $end : new \DateTime($end), $options);
342+
$stub = new Stub();
343+
344+
$cast = DateCaster::castPeriod($p, array(), $stub, false, 0);
345+
346+
$xDump = <<<EODUMP
347+
array:1 [
348+
"\\x00~\\x00period" => $xPeriod
349+
]
350+
EODUMP;
351+
352+
$this->assertDumpMatchesFormat($xDump, $cast);
353+
354+
$xDump = <<<EODUMP
355+
Symfony\Component\VarDumper\Caster\ConstStub {
356+
+type: 1
357+
+class: "$xPeriod"
358+
+value: "%A$xDates%A"
359+
+cut: 0
360+
+handle: 0
361+
+refCount: 0
362+
+position: 0
363+
+attr: []
364+
}
365+
EODUMP;
366+
367+
$this->assertDumpMatchesFormat($xDump, $cast["\0~\0period"]);
368+
}
369+
370+
public function providePeriods()
371+
{
372+
$i = new \DateInterval('PT0S');
373+
$ms = \PHP_VERSION_ID >= 70100 && isset($i->f) ? '.0' : '';
374+
375+
$periods = array(
376+
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'),
377+
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'),
378+
379+
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'),
380+
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'),
381+
382+
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'),
383+
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'),
384+
385+
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'),
386+
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'),
387+
388+
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'),
389+
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'),
390+
391+
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'),
392+
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'),
393+
394+
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'),
395+
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'),
396+
);
397+
398+
if (\PHP_VERSION_ID < 70107) {
399+
array_walk($periods, function (&$i) { $i[5] = ''; });
400+
}
401+
402+
return $periods;
403+
}
311404
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.