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 a22f12d

Browse filesBrowse files
committed
Add period caster
1 parent 8110598 commit a22f12d
Copy full SHA for a22f12d

File tree

3 files changed

+119
-0
lines changed
Filter options

3 files changed

+119
-0
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/VarDumper/Caster/DateCaster.php
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,31 @@ public static function castTimeZone(\DateTimeZone $timeZone, array $a, Stub $stu
7979

8080
return $filter & Caster::EXCLUDE_VERBOSE ? $z : $z + $a;
8181
}
82+
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+
$format = 'Y-m-d'.('000000' === $p->getStartDate()->format('His') && '000' === $p->getDateInterval()->format('%h%i%s') ? '' : ' H:i:s');
92+
foreach (clone $p as $i => $d) {
93+
$dates[] = sprintf('%s) %s', $i + 1, $d->format($format));
94+
}
95+
}
96+
97+
$period = sprintf(
98+
'every %s, from %s (%s) %s',
99+
self::formatInterval($p->getDateInterval()),
100+
$p->getStartDate()->format('Y-m-d H:i:s'),
101+
$p->include_start_date ? 'included' : 'excluded',
102+
($end = $p->getEndDate()) ? 'to '.$end->format('Y-m-d H:i:s') : 'for '.$p->recurrences.'x'
103+
);
104+
105+
$p = array(Caster::PREFIX_VIRTUAL.'period' => new ConstStub($period, implode(PHP_EOL, $dates)));
106+
107+
return $filter & Caster::EXCLUDE_VERBOSE ? $p : $p + $a;
108+
}
82109
}

‎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
+91Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\VarDumper\Tests\Caster;
1313

14+
use Doctrine\DBAL\Types\DateImmutableType;
1415
use PHPUnit\Framework\TestCase;
1516
use Symfony\Component\VarDumper\Caster\Caster;
1617
use Symfony\Component\VarDumper\Caster\DateCaster;
@@ -289,4 +290,94 @@ public function provideTimeZones()
289290
array('Pacific/Tahiti', 'Pacific/Tahiti (-10:00)', $xRegion),
290291
);
291292
}
293+
294+
/**
295+
* @dataProvider providePeriods
296+
*/
297+
public function testDumpPeriod($start, $interval, $end, $options, $expected)
298+
{
299+
if (defined('HHVM_VERSION_ID') || \PHP_VERSION_ID < 50605) {
300+
$this->markTestSkipped();
301+
}
302+
303+
$p = new \DatePeriod(new \DateTime($start), new \DateInterval($interval), is_int($end) ? $end : new \DateTime($end), $options);
304+
305+
$xDump = <<<EODUMP
306+
DatePeriod {
307+
period: $expected
308+
%A}
309+
EODUMP;
310+
311+
$this->assertDumpMatchesFormat($xDump, $p);
312+
}
313+
314+
/**
315+
* @dataProvider providePeriods
316+
*/
317+
public function testCastPeriod($start, $interval, $end, $options, $xPeriod, $xDates)
318+
{
319+
if (defined('HHVM_VERSION_ID') || \PHP_VERSION_ID < 50605) {
320+
$this->markTestSkipped();
321+
}
322+
323+
$p = new \DatePeriod(new \DateTime($start), new \DateInterval($interval), is_int($end) ? $end : new \DateTime($end), $options);
324+
$stub = new Stub();
325+
326+
$cast = DateCaster::castPeriod($p, array(), $stub, false, 0);
327+
328+
$xDump = <<<EODUMP
329+
array:1 [
330+
"\\x00~\\x00period" => $xPeriod
331+
]
332+
EODUMP;
333+
334+
$this->assertDumpMatchesFormat($xDump, $cast);
335+
336+
$xDump = <<<EODUMP
337+
Symfony\Component\VarDumper\Caster\ConstStub {
338+
+type: 1
339+
+class: "$xPeriod"
340+
+value: "%A$xDates%A"
341+
+cut: 0
342+
+handle: 0
343+
+refCount: 0
344+
+position: 0
345+
+attr: []
346+
}
347+
EODUMP;
348+
349+
$this->assertDumpMatchesFormat($xDump, $cast["\0~\0period"]);
350+
}
351+
352+
public function providePeriods()
353+
{
354+
$i = new \DateInterval('PT0S');
355+
$ms = \PHP_VERSION_ID >= 70100 && isset($i->f) ? '.000000' : '';
356+
357+
$periods = array(
358+
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'),
359+
array('2017-01-01', 'P1D', 1, 0, 'every + 1d, from 2017-01-01 00:00:00 (included) for 2x', '1) 2017-01-01%a2) 2017-01-02'),
360+
361+
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'),
362+
array('2017-01-01', 'P1D', 2, 0, 'every + 1d, from 2017-01-01 00:00:00 (included) for 3x', '1) 2017-01-01%a2) 2017-01-02%a3) 2017-01-03'),
363+
364+
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%a3) 2017-01-03%a4) 2017-01-04'),
365+
array('2017-01-01', 'P1D', 3, 0, 'every + 1d, from 2017-01-01 00:00:00 (included) for 4x', '1) 2017-01-01%a2) 2017-01-02%a3) 2017-01-03%a4) 2017-01-04'),
366+
367+
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'),
368+
array('2017-01-01 01:00:00', 'P1D', 1, 0, 'every + 1d, from 2017-01-01 01:00:00 (included) for 2x', '1) 2017-01-01 01:00:00%a2) 2017-01-02 01:00:00'),
369+
370+
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'),
371+
array('2017-01-01', 'P1DT1H', 1, 0, "every + 1d 01:00:00$ms, from 2017-01-01 00:00:00 (included) for 2x", '1) 2017-01-01 00:00:00%a2) 2017-01-02 01:00:00'),
372+
373+
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'),
374+
array('2017-01-01', 'P1D', 2, \DatePeriod::EXCLUDE_START_DATE, 'every + 1d, from 2017-01-01 00:00:00 (excluded) for 2x', '1) 2017-01-02%a2) 2017-01-03'),
375+
);
376+
377+
if (\PHP_VERSION_ID < 70107 ) {
378+
array_walk($periods, function (&$i) { $i[5] = ''; });
379+
}
380+
381+
return $periods;
382+
}
292383
}

0 commit comments

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