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