-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathPeriod.php
179 lines (162 loc) · 5.08 KB
/
Period.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/*
* This file is part of the zenstruck/messenger-monitor-bundle package.
*
* (c) Kevin Bond <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Zenstruck\Messenger\Monitor\History;
use function Symfony\Component\Clock\now;
/**
* @author Kevin Bond <[email protected]>
*/
enum Period: string
{
case IN_LAST_HOUR = 'in-last-hour';
case IN_LAST_DAY = 'in-last-day';
case IN_LAST_WEEK = 'in-last-week';
case IN_LAST_MONTH = 'in-last-month';
case TODAY = 'today';
case YESTERDAY = 'yesterday';
case LAST_WEEK = 'last-week';
case LAST_MONTH = 'last-month';
case ALL = 'all';
case OLDER_THAN_1_HOUR = '1-hour';
case OLDER_THAN_1_DAY = '1-day';
case OLDER_THAN_1_WEEK = '1-week';
case OLDER_THAN_1_MONTH = '1-month';
public static function parse(string|self|null $value, self $default = self::IN_LAST_DAY): self
{
try {
return self::parseOrFail($value);
} catch (\InvalidArgumentException) {
return $default;
}
}
public static function parseOrFail(string|self|null $value): self
{
if ($value instanceof self) {
return $value;
}
try {
return self::from((string) $value);
} catch (\ValueError $e) {
throw new \InvalidArgumentException(\sprintf('Invalid period "%s".', $value), previous: $e);
}
}
/**
* @return string[]
*/
public static function values(): array
{
return \array_map(static fn(self $p) => $p->value, self::cases());
}
/**
* @return self[]
*/
public static function inLastCases(): array
{
return [
self::IN_LAST_HOUR,
self::IN_LAST_DAY,
self::IN_LAST_WEEK,
self::IN_LAST_MONTH,
];
}
/**
* @return string[]
*/
public static function inLastValues(): array
{
return \array_map(static fn(self $p) => $p->value, self::inLastCases());
}
/**
* @return self[]
*/
public static function absoluteCases(): array
{
return [
self::TODAY,
self::YESTERDAY,
self::LAST_WEEK,
self::LAST_MONTH,
self::ALL,
];
}
/**
* @return string[]
*/
public static function absoluteValues(): array
{
return \array_map(static fn(self $p) => $p->value, self::absoluteCases());
}
/**
* @return self[]
*/
public static function olderThanCases(): array
{
return [
self::OLDER_THAN_1_HOUR,
self::OLDER_THAN_1_DAY,
self::OLDER_THAN_1_WEEK,
self::OLDER_THAN_1_MONTH,
];
}
/**
* @return string[]
*/
public static function olderThanValues(): array
{
return \array_map(static fn(self $p) => $p->value, self::olderThanCases());
}
public function humanize(): string
{
return match ($this) {
self::IN_LAST_HOUR => 'In Last Hour',
self::IN_LAST_DAY => 'In Last Day',
self::IN_LAST_WEEK => 'In Last Week',
self::IN_LAST_MONTH => 'In Last Month',
self::TODAY => 'Today',
self::YESTERDAY => 'Yesterday',
self::LAST_WEEK => 'Last Week',
self::LAST_MONTH => 'Last Month',
self::ALL => 'All Time',
self::OLDER_THAN_1_HOUR => 'Older Than 1 Hour',
self::OLDER_THAN_1_DAY => 'Older Than 1 Day',
self::OLDER_THAN_1_WEEK => 'Older Than 1 Week',
self::OLDER_THAN_1_MONTH => 'Older Than 1 Month',
};
}
/**
* From, to.
*
* @return array{?\DateTimeImmutable, ?\DateTimeImmutable}
*/
public function timestamps(): array
{
$now = now();
return match ($this) {
self::IN_LAST_HOUR => [$now->modify('-1 hour'), null],
self::IN_LAST_DAY => [$now->modify('-24 hours'), null],
self::IN_LAST_WEEK => [$now->modify('-7 days'), null],
self::IN_LAST_MONTH => [$now->modify('-1 month'), null],
self::TODAY => [new \DateTimeImmutable('today'), new \DateTimeImmutable('tomorrow')],
self::YESTERDAY => [new \DateTimeImmutable('yesterday'), new \DateTimeImmutable('today')],
self::LAST_WEEK => [
($to = new \DateTimeImmutable('last sunday'))->modify('-1 week'),
$to,
],
self::LAST_MONTH => [
new \DateTimeImmutable(\date('Y-m-d', \strtotime('first day of last month'))),
new \DateTimeImmutable(\date('Y-m-d', \strtotime('first day of this month'))),
],
self::ALL => [null, null],
self::OLDER_THAN_1_HOUR => [null, $now->modify('-1 hour')],
self::OLDER_THAN_1_DAY => [null, $now->modify('-24 hours')],
self::OLDER_THAN_1_WEEK => [null, $now->modify('-7 days')],
self::OLDER_THAN_1_MONTH => [null, $now->modify('-1 month')],
};
}
}