annotate vendor/symfony/var-dumper/Caster/DateCaster.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@12 1 <?php
Chris@12 2
Chris@12 3 /*
Chris@12 4 * This file is part of the Symfony package.
Chris@12 5 *
Chris@12 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@12 7 *
Chris@12 8 * For the full copyright and license information, please view the LICENSE
Chris@12 9 * file that was distributed with this source code.
Chris@12 10 */
Chris@12 11
Chris@12 12 namespace Symfony\Component\VarDumper\Caster;
Chris@12 13
Chris@12 14 use Symfony\Component\VarDumper\Cloner\Stub;
Chris@12 15
Chris@12 16 /**
Chris@12 17 * Casts DateTimeInterface related classes to array representation.
Chris@12 18 *
Chris@12 19 * @author Dany Maillard <danymaillard93b@gmail.com>
Chris@12 20 */
Chris@12 21 class DateCaster
Chris@12 22 {
Chris@12 23 public static function castDateTime(\DateTimeInterface $d, array $a, Stub $stub, $isNested, $filter)
Chris@12 24 {
Chris@12 25 $prefix = Caster::PREFIX_VIRTUAL;
Chris@12 26 $location = $d->getTimezone()->getLocation();
Chris@12 27 $fromNow = (new \DateTime())->diff($d);
Chris@12 28
Chris@12 29 $title = $d->format('l, F j, Y')
Chris@12 30 ."\n".self::formatInterval($fromNow).' from now'
Chris@12 31 .($location ? ($d->format('I') ? "\nDST On" : "\nDST Off") : '')
Chris@12 32 ;
Chris@12 33
Chris@17 34 $a = [];
Chris@12 35 $a[$prefix.'date'] = new ConstStub(self::formatDateTime($d, $location ? ' e (P)' : ' P'), $title);
Chris@12 36
Chris@12 37 $stub->class .= $d->format(' @U');
Chris@12 38
Chris@12 39 return $a;
Chris@12 40 }
Chris@12 41
Chris@12 42 public static function castInterval(\DateInterval $interval, array $a, Stub $stub, $isNested, $filter)
Chris@12 43 {
Chris@12 44 $now = new \DateTimeImmutable();
Chris@12 45 $numberOfSeconds = $now->add($interval)->getTimestamp() - $now->getTimestamp();
Chris@12 46 $title = number_format($numberOfSeconds, 0, '.', ' ').'s';
Chris@12 47
Chris@17 48 $i = [Caster::PREFIX_VIRTUAL.'interval' => new ConstStub(self::formatInterval($interval), $title)];
Chris@12 49
Chris@12 50 return $filter & Caster::EXCLUDE_VERBOSE ? $i : $i + $a;
Chris@12 51 }
Chris@12 52
Chris@12 53 private static function formatInterval(\DateInterval $i)
Chris@12 54 {
Chris@12 55 $format = '%R ';
Chris@12 56
Chris@12 57 if (0 === $i->y && 0 === $i->m && ($i->h >= 24 || $i->i >= 60 || $i->s >= 60)) {
Chris@12 58 $i = date_diff($d = new \DateTime(), date_add(clone $d, $i)); // recalculate carry over points
Chris@12 59 $format .= 0 < $i->days ? '%ad ' : '';
Chris@12 60 } else {
Chris@12 61 $format .= ($i->y ? '%yy ' : '').($i->m ? '%mm ' : '').($i->d ? '%dd ' : '');
Chris@12 62 }
Chris@12 63
Chris@12 64 if (\PHP_VERSION_ID >= 70100 && isset($i->f)) {
Chris@12 65 $format .= $i->h || $i->i || $i->s || $i->f ? '%H:%I:'.self::formatSeconds($i->s, substr($i->f, 2)) : '';
Chris@12 66 } else {
Chris@12 67 $format .= $i->h || $i->i || $i->s ? '%H:%I:%S' : '';
Chris@12 68 }
Chris@12 69
Chris@12 70 $format = '%R ' === $format ? '0s' : $format;
Chris@12 71
Chris@12 72 return $i->format(rtrim($format));
Chris@12 73 }
Chris@12 74
Chris@12 75 public static function castTimeZone(\DateTimeZone $timeZone, array $a, Stub $stub, $isNested, $filter)
Chris@12 76 {
Chris@12 77 $location = $timeZone->getLocation();
Chris@12 78 $formatted = (new \DateTime('now', $timeZone))->format($location ? 'e (P)' : 'P');
Chris@17 79 $title = $location && \extension_loaded('intl') ? \Locale::getDisplayRegion('-'.$location['country_code'], \Locale::getDefault()) : '';
Chris@12 80
Chris@17 81 $z = [Caster::PREFIX_VIRTUAL.'timezone' => new ConstStub($formatted, $title)];
Chris@12 82
Chris@12 83 return $filter & Caster::EXCLUDE_VERBOSE ? $z : $z + $a;
Chris@12 84 }
Chris@12 85
Chris@12 86 public static function castPeriod(\DatePeriod $p, array $a, Stub $stub, $isNested, $filter)
Chris@12 87 {
Chris@17 88 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 89 return $a;
Chris@12 90 }
Chris@12 91
Chris@17 92 $dates = [];
Chris@12 93 if (\PHP_VERSION_ID >= 70107) { // see https://bugs.php.net/bug.php?id=74639
Chris@12 94 foreach (clone $p as $i => $d) {
Chris@12 95 if (3 === $i) {
Chris@12 96 $now = new \DateTimeImmutable();
Chris@12 97 $dates[] = sprintf('%s more', ($end = $p->getEndDate())
Chris@12 98 ? ceil(($end->format('U.u') - $d->format('U.u')) / ($now->add($p->getDateInterval())->format('U.u') - $now->format('U.u')))
Chris@12 99 : $p->recurrences - $i
Chris@12 100 );
Chris@12 101 break;
Chris@12 102 }
Chris@12 103 $dates[] = sprintf('%s) %s', $i + 1, self::formatDateTime($d));
Chris@12 104 }
Chris@12 105 }
Chris@12 106
Chris@12 107 $period = sprintf(
Chris@12 108 'every %s, from %s (%s) %s',
Chris@12 109 self::formatInterval($p->getDateInterval()),
Chris@12 110 self::formatDateTime($p->getStartDate()),
Chris@12 111 $p->include_start_date ? 'included' : 'excluded',
Chris@12 112 ($end = $p->getEndDate()) ? 'to '.self::formatDateTime($end) : 'recurring '.$p->recurrences.' time/s'
Chris@12 113 );
Chris@12 114
Chris@17 115 $p = [Caster::PREFIX_VIRTUAL.'period' => new ConstStub($period, implode("\n", $dates))];
Chris@12 116
Chris@12 117 return $filter & Caster::EXCLUDE_VERBOSE ? $p : $p + $a;
Chris@12 118 }
Chris@12 119
Chris@12 120 private static function formatDateTime(\DateTimeInterface $d, $extra = '')
Chris@12 121 {
Chris@12 122 return $d->format('Y-m-d H:i:'.self::formatSeconds($d->format('s'), $d->format('u')).$extra);
Chris@12 123 }
Chris@12 124
Chris@12 125 private static function formatSeconds($s, $us)
Chris@12 126 {
Chris@17 127 return sprintf('%02d.%s', $s, 0 === ($len = \strlen($t = rtrim($us, '0'))) ? '0' : ($len <= 3 ? str_pad($t, 3, '0') : $us));
Chris@12 128 }
Chris@12 129 }