Chris@12: Chris@12: * Chris@12: * For the full copyright and license information, please view the LICENSE Chris@12: * file that was distributed with this source code. Chris@12: */ Chris@12: Chris@12: namespace Symfony\Component\VarDumper\Caster; Chris@12: Chris@12: use Symfony\Component\VarDumper\Cloner\Stub; Chris@12: Chris@12: /** Chris@12: * Casts DateTimeInterface related classes to array representation. Chris@12: * Chris@12: * @author Dany Maillard Chris@12: */ Chris@12: class DateCaster Chris@12: { Chris@12: public static function castDateTime(\DateTimeInterface $d, array $a, Stub $stub, $isNested, $filter) Chris@12: { Chris@12: $prefix = Caster::PREFIX_VIRTUAL; Chris@12: $location = $d->getTimezone()->getLocation(); Chris@12: $fromNow = (new \DateTime())->diff($d); Chris@12: Chris@12: $title = $d->format('l, F j, Y') Chris@12: ."\n".self::formatInterval($fromNow).' from now' Chris@12: .($location ? ($d->format('I') ? "\nDST On" : "\nDST Off") : '') Chris@12: ; Chris@12: Chris@17: $a = []; Chris@12: $a[$prefix.'date'] = new ConstStub(self::formatDateTime($d, $location ? ' e (P)' : ' P'), $title); Chris@12: Chris@12: $stub->class .= $d->format(' @U'); Chris@12: Chris@12: return $a; Chris@12: } Chris@12: Chris@12: public static function castInterval(\DateInterval $interval, array $a, Stub $stub, $isNested, $filter) Chris@12: { Chris@12: $now = new \DateTimeImmutable(); Chris@12: $numberOfSeconds = $now->add($interval)->getTimestamp() - $now->getTimestamp(); Chris@12: $title = number_format($numberOfSeconds, 0, '.', ' ').'s'; Chris@12: Chris@17: $i = [Caster::PREFIX_VIRTUAL.'interval' => new ConstStub(self::formatInterval($interval), $title)]; Chris@12: Chris@12: return $filter & Caster::EXCLUDE_VERBOSE ? $i : $i + $a; Chris@12: } Chris@12: Chris@12: private static function formatInterval(\DateInterval $i) Chris@12: { Chris@12: $format = '%R '; Chris@12: Chris@12: if (0 === $i->y && 0 === $i->m && ($i->h >= 24 || $i->i >= 60 || $i->s >= 60)) { Chris@12: $i = date_diff($d = new \DateTime(), date_add(clone $d, $i)); // recalculate carry over points Chris@12: $format .= 0 < $i->days ? '%ad ' : ''; Chris@12: } else { Chris@12: $format .= ($i->y ? '%yy ' : '').($i->m ? '%mm ' : '').($i->d ? '%dd ' : ''); Chris@12: } Chris@12: Chris@12: if (\PHP_VERSION_ID >= 70100 && isset($i->f)) { Chris@12: $format .= $i->h || $i->i || $i->s || $i->f ? '%H:%I:'.self::formatSeconds($i->s, substr($i->f, 2)) : ''; Chris@12: } else { Chris@12: $format .= $i->h || $i->i || $i->s ? '%H:%I:%S' : ''; Chris@12: } Chris@12: Chris@12: $format = '%R ' === $format ? '0s' : $format; Chris@12: Chris@12: return $i->format(rtrim($format)); Chris@12: } Chris@12: Chris@12: public static function castTimeZone(\DateTimeZone $timeZone, array $a, Stub $stub, $isNested, $filter) Chris@12: { Chris@12: $location = $timeZone->getLocation(); Chris@12: $formatted = (new \DateTime('now', $timeZone))->format($location ? 'e (P)' : 'P'); Chris@17: $title = $location && \extension_loaded('intl') ? \Locale::getDisplayRegion('-'.$location['country_code'], \Locale::getDefault()) : ''; Chris@12: Chris@17: $z = [Caster::PREFIX_VIRTUAL.'timezone' => new ConstStub($formatted, $title)]; Chris@12: Chris@12: return $filter & Caster::EXCLUDE_VERBOSE ? $z : $z + $a; Chris@12: } Chris@12: Chris@12: public static function castPeriod(\DatePeriod $p, array $a, Stub $stub, $isNested, $filter) Chris@12: { Chris@17: if (\defined('HHVM_VERSION_ID') || \PHP_VERSION_ID < 50620 || (\PHP_VERSION_ID >= 70000 && \PHP_VERSION_ID < 70005)) { // see https://bugs.php.net/bug.php?id=71635 Chris@12: return $a; Chris@12: } Chris@12: Chris@17: $dates = []; Chris@12: if (\PHP_VERSION_ID >= 70107) { // see https://bugs.php.net/bug.php?id=74639 Chris@12: foreach (clone $p as $i => $d) { Chris@12: if (3 === $i) { Chris@12: $now = new \DateTimeImmutable(); Chris@12: $dates[] = sprintf('%s more', ($end = $p->getEndDate()) Chris@12: ? ceil(($end->format('U.u') - $d->format('U.u')) / ($now->add($p->getDateInterval())->format('U.u') - $now->format('U.u'))) Chris@12: : $p->recurrences - $i Chris@12: ); Chris@12: break; Chris@12: } Chris@12: $dates[] = sprintf('%s) %s', $i + 1, self::formatDateTime($d)); Chris@12: } Chris@12: } Chris@12: Chris@12: $period = sprintf( Chris@12: 'every %s, from %s (%s) %s', Chris@12: self::formatInterval($p->getDateInterval()), Chris@12: self::formatDateTime($p->getStartDate()), Chris@12: $p->include_start_date ? 'included' : 'excluded', Chris@12: ($end = $p->getEndDate()) ? 'to '.self::formatDateTime($end) : 'recurring '.$p->recurrences.' time/s' Chris@12: ); Chris@12: Chris@17: $p = [Caster::PREFIX_VIRTUAL.'period' => new ConstStub($period, implode("\n", $dates))]; Chris@12: Chris@12: return $filter & Caster::EXCLUDE_VERBOSE ? $p : $p + $a; Chris@12: } Chris@12: Chris@12: private static function formatDateTime(\DateTimeInterface $d, $extra = '') Chris@12: { Chris@12: return $d->format('Y-m-d H:i:'.self::formatSeconds($d->format('s'), $d->format('u')).$extra); Chris@12: } Chris@12: Chris@12: private static function formatSeconds($s, $us) Chris@12: { Chris@17: return sprintf('%02d.%s', $s, 0 === ($len = \strlen($t = rtrim($us, '0'))) ? '0' : ($len <= 3 ? str_pad($t, 3, '0') : $us)); Chris@12: } Chris@12: }