Add interval caster
This commit is contained in:
parent
5a3abf5e82
commit
a73522c0de
@ -27,7 +27,7 @@ class DateCaster
|
|||||||
$fromNow = (new \DateTime())->diff($d);
|
$fromNow = (new \DateTime())->diff($d);
|
||||||
|
|
||||||
$title = $d->format('l, F j, Y')
|
$title = $d->format('l, F j, Y')
|
||||||
."\n".$fromNow->format('%R').(ltrim($fromNow->format('%yy %mm %dd %H:%I:%Ss'), ' 0ymd:s') ?: '0s').' from now'
|
."\n".$fromNow->format('%R').self::formatInterval($fromNow).' from now'
|
||||||
.($location ? ($d->format('I') ? "\nDST On" : "\nDST Off") : '')
|
.($location ? ($d->format('I') ? "\nDST On" : "\nDST Off") : '')
|
||||||
;
|
;
|
||||||
|
|
||||||
@ -38,4 +38,34 @@ class DateCaster
|
|||||||
|
|
||||||
return $a;
|
return $a;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function castInterval(\DateInterval $interval, array $a, Stub $stub, $isNested, $filter)
|
||||||
|
{
|
||||||
|
$now = new \DateTimeImmutable();
|
||||||
|
$numberOfSeconds = $now->add($interval)->getTimestamp() - $now->getTimestamp();
|
||||||
|
$title = number_format($numberOfSeconds, 0, '.', ' ').'s';
|
||||||
|
|
||||||
|
$i = array(Caster::PREFIX_VIRTUAL.'interval' => new ConstStub(self::formatInterval($interval), $title));
|
||||||
|
|
||||||
|
return $filter & Caster::EXCLUDE_VERBOSE ? $i : $i + $a;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function formatInterval(\DateInterval $i)
|
||||||
|
{
|
||||||
|
$format = '%R '
|
||||||
|
.($i->y ? '%yy ' : '')
|
||||||
|
.($i->m ? '%mm ' : '')
|
||||||
|
.($i->d ? '%dd ' : '')
|
||||||
|
;
|
||||||
|
|
||||||
|
if (\PHP_VERSION_ID >= 70100 && isset($i->f)) {
|
||||||
|
$format .= $i->h || $i->i || $i->s || $i->f ? '%H:%I:%S.%F' : '';
|
||||||
|
} else {
|
||||||
|
$format .= $i->h || $i->i || $i->s ? '%H:%I:%S' : '';
|
||||||
|
}
|
||||||
|
|
||||||
|
$format = '%R ' === $format ? '0s' : $format;
|
||||||
|
|
||||||
|
return $i->format(rtrim($format));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -110,6 +110,7 @@ abstract class AbstractCloner implements ClonerInterface
|
|||||||
'RedisArray' => array('Symfony\Component\VarDumper\Caster\RedisCaster', 'castRedisArray'),
|
'RedisArray' => array('Symfony\Component\VarDumper\Caster\RedisCaster', 'castRedisArray'),
|
||||||
|
|
||||||
'DateTimeInterface' => array('Symfony\Component\VarDumper\Caster\DateCaster', 'castDateTime'),
|
'DateTimeInterface' => array('Symfony\Component\VarDumper\Caster\DateCaster', 'castDateTime'),
|
||||||
|
'DateInterval' => array('Symfony\Component\VarDumper\Caster\DateCaster', 'castInterval'),
|
||||||
|
|
||||||
':curl' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castCurl'),
|
':curl' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castCurl'),
|
||||||
':dba' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castDba'),
|
':dba' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castDba'),
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
namespace Symfony\Component\VarDumper\Tests\Caster;
|
namespace Symfony\Component\VarDumper\Tests\Caster;
|
||||||
|
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Symfony\Component\VarDumper\Caster\Caster;
|
||||||
use Symfony\Component\VarDumper\Caster\DateCaster;
|
use Symfony\Component\VarDumper\Caster\DateCaster;
|
||||||
use Symfony\Component\VarDumper\Cloner\Stub;
|
use Symfony\Component\VarDumper\Cloner\Stub;
|
||||||
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
|
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
|
||||||
@ -84,4 +85,103 @@ EODUMP;
|
|||||||
array('2017-04-30 00:00:00.000000', '+02:00', '2017-04-30 00:00:00.000000 +02:00'),
|
array('2017-04-30 00:00:00.000000', '+02:00', '2017-04-30 00:00:00.000000 +02:00'),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider provideIntervals
|
||||||
|
*/
|
||||||
|
public function testDumpInterval($intervalSpec, $invert, $expected)
|
||||||
|
{
|
||||||
|
$interval = new \DateInterval($intervalSpec);
|
||||||
|
$interval->invert = $invert;
|
||||||
|
|
||||||
|
$xDump = <<<EODUMP
|
||||||
|
DateInterval {
|
||||||
|
interval: $expected
|
||||||
|
%A}
|
||||||
|
EODUMP;
|
||||||
|
|
||||||
|
$this->assertDumpMatchesFormat($xDump, $interval);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider provideIntervals
|
||||||
|
*/
|
||||||
|
public function testDumpIntervalExcludingVerbosity($intervalSpec, $invert, $expected)
|
||||||
|
{
|
||||||
|
$interval = new \DateInterval($intervalSpec);
|
||||||
|
$interval->invert = $invert;
|
||||||
|
|
||||||
|
$xDump = <<<EODUMP
|
||||||
|
DateInterval {
|
||||||
|
interval: $expected
|
||||||
|
}
|
||||||
|
EODUMP;
|
||||||
|
|
||||||
|
$this->assertDumpMatchesFormat($xDump, $interval, Caster::EXCLUDE_VERBOSE);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider provideIntervals
|
||||||
|
*/
|
||||||
|
public function testCastInterval($intervalSpec, $invert, $xInterval, $xSeconds)
|
||||||
|
{
|
||||||
|
$interval = new \DateInterval($intervalSpec);
|
||||||
|
$interval->invert = $invert;
|
||||||
|
$stub = new Stub();
|
||||||
|
|
||||||
|
$cast = DateCaster::castInterval($interval, array('foo' => 'bar'), $stub, false, Caster::EXCLUDE_VERBOSE);
|
||||||
|
|
||||||
|
$xDump = <<<EODUMP
|
||||||
|
array:1 [
|
||||||
|
"\\x00~\\x00interval" => $xInterval
|
||||||
|
]
|
||||||
|
EODUMP;
|
||||||
|
|
||||||
|
$this->assertDumpMatchesFormat($xDump, $cast);
|
||||||
|
|
||||||
|
if (null === $xSeconds) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$xDump = <<<EODUMP
|
||||||
|
Symfony\Component\VarDumper\Caster\ConstStub {
|
||||||
|
+type: "ref"
|
||||||
|
+class: "$xInterval"
|
||||||
|
+value: "$xSeconds"
|
||||||
|
+cut: 0
|
||||||
|
+handle: 0
|
||||||
|
+refCount: 0
|
||||||
|
+position: 0
|
||||||
|
+attr: []
|
||||||
|
}
|
||||||
|
EODUMP;
|
||||||
|
|
||||||
|
$this->assertDumpMatchesFormat($xDump, $cast["\0~\0interval"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function provideIntervals()
|
||||||
|
{
|
||||||
|
$i = new \DateInterval('PT0S');
|
||||||
|
$ms = \PHP_VERSION_ID >= 70100 && isset($i->f) ? '.000000' : '';
|
||||||
|
|
||||||
|
return array(
|
||||||
|
array('PT0S', 0, '0s', '0s'),
|
||||||
|
array('PT1S', 0, '+ 00:00:01'.$ms, '1s'),
|
||||||
|
array('PT2M', 0, '+ 00:02:00'.$ms, '120s'),
|
||||||
|
array('PT3H', 0, '+ 03:00:00'.$ms, '10 800s'),
|
||||||
|
array('P4D', 0, '+ 4d', '345 600s'),
|
||||||
|
array('P5M', 0, '+ 5m', null),
|
||||||
|
array('P6Y', 0, '+ 6y', null),
|
||||||
|
array('P1Y2M3DT4H5M6S', 0, '+ 1y 2m 3d 04:05:06'.$ms, null),
|
||||||
|
|
||||||
|
array('PT0S', 1, '0s', '0s'),
|
||||||
|
array('PT1S', 1, '- 00:00:01'.$ms, '-1s'),
|
||||||
|
array('PT2M', 1, '- 00:02:00'.$ms, '-120s'),
|
||||||
|
array('PT3H', 1, '- 03:00:00'.$ms, '-10 800s'),
|
||||||
|
array('P4D', 1, '- 4d', '-345 600s'),
|
||||||
|
array('P5M', 1, '- 5m', null),
|
||||||
|
array('P6Y', 1, '- 6y', null),
|
||||||
|
array('P1Y2M3DT4H5M6S', 1, '- 1y 2m 3d 04:05:06'.$ms, null),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user