1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arcanedev\LaravelMetrics\Metrics\Concerns; |
6
|
|
|
|
7
|
|
|
use Arcanedev\LaravelMetrics\Exceptions\InvalidTrendUnitException; |
8
|
|
|
use Cake\Chronos\Chronos; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Trait FormatsTrends |
12
|
|
|
* |
13
|
|
|
* @author ARCANEDEV <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
trait FormatsTrends |
16
|
|
|
{ |
17
|
|
|
/* ----------------------------------------------------------------- |
18
|
|
|
| Main Methods |
19
|
|
|
| ----------------------------------------------------------------- |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Format aggregate key. |
24
|
|
|
* |
25
|
|
|
* @param \Cake\Chronos\Chronos $date |
26
|
|
|
* @param string $unit |
27
|
|
|
* |
28
|
|
|
* @return string |
29
|
|
|
*/ |
30
|
36 |
|
protected static function formatAggregateKey(Chronos $date, string $unit): string |
31
|
|
|
{ |
32
|
|
|
switch ($unit) { |
33
|
36 |
|
case static::BY_MONTHS: |
34
|
20 |
|
return static::formatAggregateKeyByMonths($date); |
35
|
|
|
|
36
|
16 |
|
case static::BY_WEEKS: |
37
|
4 |
|
return static::formatAggregateKeyByWeeks($date); |
38
|
|
|
|
39
|
12 |
|
case static::BY_DAYS: |
40
|
4 |
|
return static::formatAggregateKeyByDays($date); |
41
|
|
|
|
42
|
8 |
|
case static::BY_HOURS: |
43
|
4 |
|
return static::formatAggregateKeyByHours($date); |
44
|
|
|
|
45
|
4 |
|
case static::BY_MINUTES: |
46
|
4 |
|
return static::formatAggregateKeyByMinutes($date); |
47
|
|
|
|
48
|
|
|
default: |
49
|
|
|
throw InvalidTrendUnitException::make($unit); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Format the label. |
55
|
|
|
* |
56
|
|
|
* @param \Cake\Chronos\Chronos $date |
57
|
|
|
* @param string $unit |
58
|
|
|
* @param bool $twelveHourTime |
59
|
|
|
* |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
36 |
|
protected static function formatLabelBy(Chronos $date, string $unit, bool $twelveHourTime = false): string |
63
|
|
|
{ |
64
|
|
|
switch ($unit) { |
65
|
36 |
|
case static::BY_MONTHS: |
66
|
20 |
|
return static::formatLabelByMonths($date); |
67
|
|
|
|
68
|
16 |
|
case static::BY_WEEKS: |
69
|
4 |
|
return static::formatLabelByWeeks($date); |
70
|
|
|
|
71
|
12 |
|
case static::BY_DAYS: |
72
|
4 |
|
return static::formatLabelByDays($date); |
73
|
|
|
|
74
|
8 |
|
case static::BY_HOURS: |
75
|
4 |
|
return static::formatLabelByHours($date, $twelveHourTime); |
76
|
|
|
|
77
|
4 |
|
case static::BY_MINUTES: |
78
|
4 |
|
return static::formatLabelByMinutes($date, $twelveHourTime); |
79
|
|
|
|
80
|
|
|
default: |
81
|
|
|
throw InvalidTrendUnitException::make($unit); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/* ----------------------------------------------------------------- |
86
|
|
|
| Formatters |
87
|
|
|
| ----------------------------------------------------------------- |
88
|
|
|
*/ |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Format the given date by months. |
92
|
|
|
* |
93
|
|
|
* @param \Cake\Chronos\Chronos $date |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
20 |
|
protected static function formatAggregateKeyByMonths(Chronos $date): string |
98
|
|
|
{ |
99
|
20 |
|
return $date->format('Y-m'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Format the given date by weeks. |
104
|
|
|
* |
105
|
|
|
* @param \Cake\Chronos\Chronos $date |
106
|
|
|
* |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
4 |
|
protected static function formatAggregateKeyByWeeks(Chronos $date): string |
110
|
|
|
{ |
111
|
4 |
|
return sprintf('%s %s', |
112
|
4 |
|
static::formatAggregateKeyByDays($date), |
113
|
4 |
|
static::formatAggregateKeyByDays($date->endOfWeek()) |
|
|
|
|
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Format the given date by days. |
119
|
|
|
* |
120
|
|
|
* @param \Cake\Chronos\Chronos $date |
121
|
|
|
* |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
8 |
|
protected static function formatAggregateKeyByDays(Chronos $date): string |
125
|
|
|
{ |
126
|
8 |
|
return $date->format('Y-m-d'); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Format the given date by hours. |
131
|
|
|
* |
132
|
|
|
* @param \Cake\Chronos\Chronos $date |
133
|
|
|
* |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
4 |
|
protected static function formatAggregateKeyByHours(Chronos $date): string |
137
|
|
|
{ |
138
|
4 |
|
return $date->format('Y-m-d H:00'); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Format the given date by minutes. |
143
|
|
|
* |
144
|
|
|
* @param \Cake\Chronos\Chronos $date |
145
|
|
|
* |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
4 |
|
protected static function formatAggregateKeyByMinutes(Chronos $date): string |
149
|
|
|
{ |
150
|
4 |
|
return $date->format('Y-m-d H:i'); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Get the label by Months. |
155
|
|
|
* |
156
|
|
|
* @param \Cake\Chronos\Chronos $date |
157
|
|
|
* |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
20 |
|
protected static function formatLabelByMonths(Chronos $date): string |
161
|
|
|
{ |
162
|
20 |
|
return __($date->format('F')).' '.$date->format('Y'); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Get the label by Weeks. |
167
|
|
|
* |
168
|
|
|
* @param \Cake\Chronos\Chronos $date |
169
|
|
|
* |
170
|
|
|
* @return string |
171
|
|
|
*/ |
172
|
4 |
|
protected static function formatLabelByWeeks(Chronos $date): string |
173
|
|
|
{ |
174
|
4 |
|
return sprintf('%s %s - %s %s', |
175
|
4 |
|
__($date->startOfWeek()->format('F')), |
176
|
4 |
|
$date->startOfWeek()->format('j'), |
177
|
|
|
|
178
|
4 |
|
__($date->endOfWeek()->format('F')), |
179
|
4 |
|
$date->endOfWeek()->format('j') |
180
|
|
|
); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Get the label by Days. |
185
|
|
|
* |
186
|
|
|
* @param \Cake\Chronos\Chronos $date |
187
|
|
|
* |
188
|
|
|
* @return string |
189
|
|
|
*/ |
190
|
4 |
|
protected static function formatLabelByDays(Chronos $date): string |
191
|
|
|
{ |
192
|
4 |
|
return __($date->format('F')).' '.$date->format('j, Y'); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Get the label by Hours. |
197
|
|
|
* |
198
|
|
|
* @param \Cake\Chronos\Chronos $date |
199
|
|
|
* @param bool $twelveHourTime |
200
|
|
|
* |
201
|
|
|
* @return string |
202
|
|
|
*/ |
203
|
4 |
|
protected static function formatLabelByHours(Chronos $date, bool $twelveHourTime = false): string |
204
|
|
|
{ |
205
|
4 |
|
return sprintf('%s %s - %s', |
206
|
4 |
|
__($date->format('F')), |
207
|
4 |
|
$date->format('j'), |
208
|
4 |
|
$date->format($twelveHourTime ? 'g:00 A' : 'G:00') |
209
|
|
|
); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Get the label by Minutes. |
214
|
|
|
* |
215
|
|
|
* @param \Cake\Chronos\Chronos $date |
216
|
|
|
* @param bool $twelveHourTime |
217
|
|
|
* |
218
|
|
|
* @return string |
219
|
|
|
*/ |
220
|
4 |
|
protected static function formatLabelByMinutes(Chronos $date, bool $twelveHourTime): string |
221
|
|
|
{ |
222
|
4 |
|
return sprintf('%s %s - %s', |
223
|
4 |
|
__($date->format('F')), |
224
|
4 |
|
$date->format('j'), |
225
|
4 |
|
$date->format($twelveHourTime ? 'g:i A' : 'G:i') |
226
|
|
|
); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.