Debian Codegraph @

vendor/ipl/web/src/Widget/Time.php

raw
1 <?php
2
3 namespace ipl\Web\Widget;
4
5 use DateInterval;
6 use DateTime;
7 use DateTimeZone;
8 use Exception;
9 use IntlDateFormatter;
10 use ipl\Html\Attributes;
11 use ipl\Html\BaseHtmlElement;
12 use ipl\Html\Text;
13 use ipl\I18n\Translation;
14
15 class Time extends BaseHtmlElement
16 {
17 use Translation;
18
19 /** @var int Format relative */
20 public const RELATIVE = 0;
21
22 /** @var int Format time */
23 public const TIME = 1;
24
25 /** @var int Format date */
26 public const DATE = 2;
27
28 /** @var DateTime Time of this widget */
29 protected DateTime $dateTime;
30
31 /** @var string Default formatted datetime string */
32 protected string $timeString;
33
34 /** @var IntlDateFormatter|null Formatter to create the time string */
35 protected ?IntlDateFormatter $formatter = null;
36
37 /** @var DateTime|null Time to compare with, null takes current time */
38 protected ?DateTime $compareTime = null;
39
40 /** @var string Tag of element. */
41 protected $tag = 'time';
42
43 /**
44 * Create a time widget for the given time
45 *
46 * @param DateTime $time
47 */
48 public function __construct(DateTime $time)
49 {
50 $this->dateTime = $time;
51 $this->timeString = $time->format('Y-m-d H:i:s');
52 $this->addAttributes(Attributes::create(['title' => $this->timeString]));
53 }
54
55 /**
56 * Set the formatter to use for formatting the time
57 *
58 * @param IntlDateFormatter $formatter
59 *
60 * @return $this
61 */
62 public function setFormatter(IntlDateFormatter $formatter): static
63 {
64 $this->formatter = $formatter;
65
66 return $this;
67 }
68
69 /**
70 * Compute the difference between the given time and the compare time
71 *
72 * Returns an array with the interval, the formatted string, and the type of difference
73 * Type can be one of the constants RELATIVE, TIME, DATE
74 *
75 * @param ?DateTime $compareTime time to compare with, or null for now
76 *
77 * @return array{0: string, 1: self::TIME|self::DATE|self::RELATIVE, 2: DateInterval}
78 */
79 public function diff(?DateTime $compareTime = null): array
80 {
81 $compareTime = $compareTime ?? new DateTime();
82 $time = $this->dateTime;
83 $interval = $time->diff($compareTime);
84
85 return [...match (true) {
86 $interval->days > 2 => [
87 $time->format($this->isSameByFormat($compareTime, 'Y') ? 'M j' : 'Y-m'),
88 static::DATE
89 ],
90 $interval->days > 0 => [$interval->format('%dd %hh'), static::RELATIVE],
91 $interval->h > 0 => $this->isSameByFormat($compareTime, 'd')
92 ? [$time->format('H:i'), static::TIME]
93 : [$time->format('M j H:i'), static::DATE],
94 default => [$interval->format('%im %ss'), static::RELATIVE],
95 }, $interval];
96 }
97
98 /**
99 * Get formatted time
100 *
101 * Override this method to customize the format of the time widget
102 * i.e., to use custom formatting, add attributes, etc.
103 *
104 * This method is only used by {@see assemble()}
105 *
106 * @return string
107 */
108 protected function format(): string
109 {
110 return $this->formatter ? $this->formatter->format($this->dateTime) : $this->timeString;
111 }
112
113 /**
114 * Return a relative time widget
115 *
116 * @param DateTime $time
117 * @param DateTime|null $compareTime null takes current time
118 *
119 * @return static
120 *
121 * @throws Exception
122 */
123 public static function relative(DateTime $time, ?DateTime $compareTime = null): static
124 {
125 if ($compareTime === null) {
126 $compareTime = new DateTime();
127 }
128
129 return $time->getTimestamp() <= $compareTime->getTimestamp()
130 ? new TimeAgo($time, $compareTime)
131 : new TimeUntil($time, $compareTime);
132 }
133
134 /**
135 * Assemble the content of the time widget
136 *
137 * @return void
138 */
139 protected function assemble(): void
140 {
141 $this->addAttributes(Attributes::create(['datetime' => $this->timeString]));
142 $this->addHtml(Text::create($this->format()));
143 }
144
145 /**
146 * Convert a value to a DateTime object
147 *
148 * @param int|float|DateTime|null $value
149 *
150 * @return DateTime
151 *
152 * @throws Exception
153 *
154 * @internal This method is for backwards compatibility
155 */
156 protected function castToDateTime(int|float|DateTime|null $value = null): DateTime
157 {
158 if ($value === null) {
159 return new DateTime();
160 }
161
162 if ($value instanceof DateTime) {
163 return $value;
164 }
165
166 $value = (int) ($value > 9999999999 ? ($value / 1000) : $value);
167
168 return (new DateTime('@' . $value))->setTimezone(new DateTimeZone(date_default_timezone_get()));
169 }
170
171 /**
172 * Checks whether the widget time and the compare time share the same value for a given format.
173 *
174 * @param DateTime $compareTime DateTime to compare with the widget time
175 * @param string $format A format string accepted by DateTime::format() (e.g. 'Y', 'd', 'Y-m-d').
176 *
177 * @return bool True if both date/times produce the same string for the given format, false otherwise.
178 *
179 * @internal
180 */
181 protected function isSameByFormat(DateTime $compareTime, string $format): bool
182 {
183 return $this->dateTime->format($format) === $compareTime->format($format);
184 }
185 }