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