|
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
|
8 |
|
protected function formatAggregateKey(Chronos $date, string $unit): string |
|
28
|
|
|
{ |
|
29
|
|
|
switch ($unit) { |
|
30
|
8 |
|
case static::BY_MONTHS: |
|
31
|
|
|
return $this->formatAggregateKeyByMonths($date); |
|
32
|
|
|
|
|
33
|
8 |
|
case static::BY_WEEKS: |
|
34
|
|
|
return $this->formatAggregateKeyByWeeks($date); |
|
35
|
|
|
|
|
36
|
8 |
|
case static::BY_DAYS: |
|
37
|
8 |
|
return $this->formatAggregateKeyByDays($date); |
|
38
|
|
|
|
|
39
|
|
|
case static::BY_HOURS: |
|
40
|
|
|
return $this->formatAggregateKeyByHours($date); |
|
41
|
|
|
|
|
42
|
|
|
case static::BY_MINUTES: |
|
43
|
|
|
return $this->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
|
8 |
|
protected function formatLabelBy(Chronos $date, string $unit, bool $twelveHourTime = false): string |
|
60
|
|
|
{ |
|
61
|
|
|
switch ($unit) { |
|
62
|
8 |
|
case static::BY_MONTHS: |
|
63
|
|
|
return $this->formatLabelByMonths($date); |
|
64
|
|
|
|
|
65
|
8 |
|
case static::BY_WEEKS: |
|
66
|
|
|
return $this->formatLabelByWeeks($date); |
|
67
|
|
|
|
|
68
|
8 |
|
case static::BY_DAYS: |
|
69
|
8 |
|
return $this->formatLabelByDays($date); |
|
70
|
|
|
|
|
71
|
|
|
case static::BY_HOURS: |
|
72
|
|
|
return $this->formatLabelByHours($date, $twelveHourTime); |
|
73
|
|
|
|
|
74
|
|
|
case static::BY_MINUTES: |
|
75
|
|
|
return $this->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
|
|
|
protected function formatAggregateKeyByMonths(Chronos $date): string |
|
95
|
|
|
{ |
|
96
|
|
|
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
|
|
|
protected function formatAggregateKeyByWeeks(Chronos $date): string |
|
107
|
|
|
{ |
|
108
|
|
|
return sprintf('%s %s', |
|
109
|
|
|
static::formatAggregateKeyByDays($date->startOfWeek()), |
|
110
|
|
|
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 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
|
|
|
protected function formatAggregateKeyByHours(Chronos $date): string |
|
134
|
|
|
{ |
|
135
|
|
|
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 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
|
|
|
protected function formatLabelByMonths(Chronos $date): string |
|
158
|
|
|
{ |
|
159
|
|
|
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
|
|
|
protected function formatLabelByWeeks(Chronos $date): string |
|
170
|
|
|
{ |
|
171
|
|
|
return sprintf('%s %s - %s %s', |
|
172
|
|
|
__($date->startOfWeek()->format('F')), |
|
173
|
|
|
$date->startOfWeek()->format('j'), |
|
174
|
|
|
|
|
175
|
|
|
__($date->endOfWeek()->format('F')), |
|
176
|
|
|
$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
|
8 |
|
protected function formatLabelByDays(Chronos $date): string |
|
188
|
|
|
{ |
|
189
|
8 |
|
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
|
|
|
protected function formatLabelByHours(Chronos $date, bool $twelveHourTime = false): string |
|
201
|
|
|
{ |
|
202
|
|
|
return sprintf('%s %s - %s', |
|
203
|
|
|
__($date->format('F')), |
|
204
|
|
|
$date->format('j'), |
|
205
|
|
|
$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 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
|
|
|
|