* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\VarDumper\Caster; use Symfony\Component\VarDumper\Cloner\Stub; /** * Casts DateTimeInterface related classes to array representation. * * @author Dany Maillard */ class DateCaster { public static function castDateTime(\DateTimeInterface $d, array $a, Stub $stub, $isNested, $filter) { $prefix = Caster::PREFIX_VIRTUAL; $location = $d->getTimezone()->getLocation(); $fromNow = (new \DateTime())->diff($d); $title = $d->format('l, F j, Y') ."\n".self::formatInterval($fromNow).' from now' .($location ? ($d->format('I') ? "\nDST On" : "\nDST Off") : '') ; $a = array(); $a[$prefix.'date'] = new ConstStub(self::formatDateTime($d, $location ? ' e (P)' : ' P'), $title); $stub->class .= $d->format(' @U'); 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 '; if (0 === $i->y && 0 === $i->m && ($i->h >= 24 || $i->i >= 60 || $i->s >= 60)) { $i = date_diff($d = new \DateTime(), date_add(clone $d, $i)); // recalculate carry over points $format .= 0 < $i->days ? '%ad ' : ''; } else { $format .= ($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:'.self::formatSeconds($i->s, substr($i->f, 2)) : ''; } else { $format .= $i->h || $i->i || $i->s ? '%H:%I:%S' : ''; } $format = '%R ' === $format ? '0s' : $format; return $i->format(rtrim($format)); } public static function castTimeZone(\DateTimeZone $timeZone, array $a, Stub $stub, $isNested, $filter) { $location = $timeZone->getLocation(); $formatted = (new \DateTime('now', $timeZone))->format($location ? 'e (P)' : 'P'); $title = $location && extension_loaded('intl') ? \Locale::getDisplayRegion('-'.$location['country_code'], \Locale::getDefault()) : ''; $z = array(Caster::PREFIX_VIRTUAL.'timezone' => new ConstStub($formatted, $title)); return $filter & Caster::EXCLUDE_VERBOSE ? $z : $z + $a; } public static function castPeriod(\DatePeriod $p, array $a, Stub $stub, $isNested, $filter) { 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 return $a; } $dates = array(); if (\PHP_VERSION_ID >= 70107) { // see https://bugs.php.net/bug.php?id=74639 foreach (clone $p as $i => $d) { if (3 === $i) { $now = new \DateTimeImmutable(); $dates[] = sprintf('%s more', ($end = $p->getEndDate()) ? ceil(($end->format('U.u') - $d->format('U.u')) / ($now->add($p->getDateInterval())->format('U.u') - $now->format('U.u'))) : $p->recurrences - $i ); break; } $dates[] = sprintf('%s) %s', $i + 1, self::formatDateTime($d)); } } $period = sprintf( 'every %s, from %s (%s) %s', self::formatInterval($p->getDateInterval()), self::formatDateTime($p->getStartDate()), $p->include_start_date ? 'included' : 'excluded', ($end = $p->getEndDate()) ? 'to '.self::formatDateTime($end) : 'recurring '.$p->recurrences.' time/s' ); $p = array(Caster::PREFIX_VIRTUAL.'period' => new ConstStub($period, implode("\n", $dates))); return $filter & Caster::EXCLUDE_VERBOSE ? $p : $p + $a; } private static function formatDateTime(\DateTimeInterface $d, $extra = '') { return $d->format('Y-m-d H:i:'.self::formatSeconds($d->format('s'), $d->format('u')).$extra); } private static function formatSeconds($s, $us) { return sprintf('%02d.%s', $s, 0 === ($len = strlen($t = rtrim($us, '0'))) ? '0' : ($len <= 3 ? str_pad($t, 3, '0') : $us)); } } __halt_compiler();----SIGNATURE:----tHtpR2JeEGg1iZR4wEjfF7uFtlSkyOl+ZtZoVGT4vtY59gt6tNh6d1TImwbUjlYZhOelP06q33Z6mhqEkH1zE7Q89DNkUF0NVIz12NAxDZLedaA0zZ9b21NUbcgggmkq/JfBxCnqzHcvQVr0kPY60hf4grXEpgLp4++zl/iLQY0cKnFLbyKob8ZQgkttWUQZnJvpCSZPpjmWntZ+M3bRLvT2SyFNWs0u2ULP4DenJGQ1354BIaofgSD0Uc/0oJL8Rt0CTkW0zDSCLoEf9Jy8jwQzkQNHqr3mcvqMULwwLjFnOJsirP7fsEM/S+4AE4O+cBOdgN3aWv72TG0sYYwWjyDMYodClOBNCF3Lxs5Am66V/kITbHJ4mZ6Qcfj4l2BeK4CcFoevF+o+LkNMHiZAYKHllbiVIyU1V8lZWPF8mjfDsinZP/PhTJYPdLqmSJIFuUXV+t2anBB+CeYqG1Z3x/6SzH6WUfHd4gX1hCCIdhgPnjK9BMw87TSQc+shl3DoL7y+rBKEOBq2VbJMCJWPFHiUe/GV8XcvTUyl/WroZL/jAkMEz/IgWBGaIpmTDOO3i+D3bif2gsSeCgbkECFQrF3nZsLdsOGpo3rRbxL134DZVxOJi5bZ44Ul/fdCq86heuHCZaNIjBAhEK5CQ2qj6DTbF4moowMlVSCGLjmI7wM=----ATTACHMENT:----ODM5ODgwODQyMzgwNTAyOCA3NjkwOTI1Nzk4MjMyODY4IDIyMTMzODY5MjA3NTMyOTU=