7e8831296c08b4918cdb8bcfc8cf20096b8e1cc2
[yaffs-website] / vendor / symfony / var-dumper / Caster / DateCaster.php
1 <?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
12 namespace Symfony\Component\VarDumper\Caster;
13
14 use 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 class DateCaster
22 {
23     public static function castDateTime(\DateTimeInterface $d, array $a, Stub $stub, $isNested, $filter)
24     {
25         $prefix = Caster::PREFIX_VIRTUAL;
26         $location = $d->getTimezone()->getLocation();
27         $fromNow = (new \DateTime())->diff($d);
28
29         $title = $d->format('l, F j, Y')
30             ."\n".self::formatInterval($fromNow).' from now'
31             .($location ? ($d->format('I') ? "\nDST On" : "\nDST Off") : '')
32         ;
33
34         $a = array();
35         $a[$prefix.'date'] = new ConstStub(self::formatDateTime($d, $location ? ' e (P)' : ' P'), $title);
36
37         $stub->class .= $d->format(' @U');
38
39         return $a;
40     }
41
42     public static function castInterval(\DateInterval $interval, array $a, Stub $stub, $isNested, $filter)
43     {
44         $now = new \DateTimeImmutable();
45         $numberOfSeconds = $now->add($interval)->getTimestamp() - $now->getTimestamp();
46         $title = number_format($numberOfSeconds, 0, '.', ' ').'s';
47
48         $i = array(Caster::PREFIX_VIRTUAL.'interval' => new ConstStub(self::formatInterval($interval), $title));
49
50         return $filter & Caster::EXCLUDE_VERBOSE ? $i : $i + $a;
51     }
52
53     private static function formatInterval(\DateInterval $i)
54     {
55         $format = '%R ';
56
57         if (0 === $i->y && 0 === $i->m && ($i->h >= 24 || $i->i >= 60 || $i->s >= 60)) {
58             $i = date_diff($d = new \DateTime(), date_add(clone $d, $i)); // recalculate carry over points
59             $format .= 0 < $i->days ? '%ad ' : '';
60         } else {
61             $format .= ($i->y ? '%yy ' : '').($i->m ? '%mm ' : '').($i->d ? '%dd ' : '');
62         }
63
64         if (\PHP_VERSION_ID >= 70100 && isset($i->f)) {
65             $format .= $i->h || $i->i || $i->s || $i->f ? '%H:%I:'.self::formatSeconds($i->s, substr($i->f, 2)) : '';
66         } else {
67             $format .= $i->h || $i->i || $i->s ? '%H:%I:%S' : '';
68         }
69
70         $format = '%R ' === $format ? '0s' : $format;
71
72         return $i->format(rtrim($format));
73     }
74
75     public static function castTimeZone(\DateTimeZone $timeZone, array $a, Stub $stub, $isNested, $filter)
76     {
77         $location = $timeZone->getLocation();
78         $formatted = (new \DateTime('now', $timeZone))->format($location ? 'e (P)' : 'P');
79         $title = $location && extension_loaded('intl') ? \Locale::getDisplayRegion('-'.$location['country_code'], \Locale::getDefault()) : '';
80
81         $z = array(Caster::PREFIX_VIRTUAL.'timezone' => new ConstStub($formatted, $title));
82
83         return $filter & Caster::EXCLUDE_VERBOSE ? $z : $z + $a;
84     }
85
86     public static function castPeriod(\DatePeriod $p, array $a, Stub $stub, $isNested, $filter)
87     {
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
89             return $a;
90         }
91
92         $dates = array();
93         if (\PHP_VERSION_ID >= 70107) { // see https://bugs.php.net/bug.php?id=74639
94             foreach (clone $p as $i => $d) {
95                 if (3 === $i) {
96                     $now = new \DateTimeImmutable();
97                     $dates[] = sprintf('%s more', ($end = $p->getEndDate())
98                         ? ceil(($end->format('U.u') - $d->format('U.u')) / ($now->add($p->getDateInterval())->format('U.u') - $now->format('U.u')))
99                         : $p->recurrences - $i
100                     );
101                     break;
102                 }
103                 $dates[] = sprintf('%s) %s', $i + 1, self::formatDateTime($d));
104             }
105         }
106
107         $period = sprintf(
108             'every %s, from %s (%s) %s',
109             self::formatInterval($p->getDateInterval()),
110             self::formatDateTime($p->getStartDate()),
111             $p->include_start_date ? 'included' : 'excluded',
112             ($end = $p->getEndDate()) ? 'to '.self::formatDateTime($end) : 'recurring '.$p->recurrences.' time/s'
113         );
114
115         $p = array(Caster::PREFIX_VIRTUAL.'period' => new ConstStub($period, implode("\n", $dates)));
116
117         return $filter & Caster::EXCLUDE_VERBOSE ? $p : $p + $a;
118     }
119
120     private static function formatDateTime(\DateTimeInterface $d, $extra = '')
121     {
122         return $d->format('Y-m-d H:i:'.self::formatSeconds($d->format('s'), $d->format('u')).$extra);
123     }
124
125     private static function formatSeconds($s, $us)
126     {
127         return sprintf('%02d.%s', $s, 0 === ($len = strlen($t = rtrim($us, '0'))) ? '0' : ($len <= 3 ? str_pad($t, 3, '0') : $us));
128     }
129 }