-
-
Notifications
You must be signed in to change notification settings - Fork 999
Expand file tree
/
Copy pathFormat.php
More file actions
334 lines (281 loc) · 11 KB
/
Copy pathFormat.php
File metadata and controls
334 lines (281 loc) · 11 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
namespace Leantime\Core\Support;
use Carbon\CarbonImmutable;
use Carbon\CarbonInterface;
use Illuminate\Support\Facades\Date;
use Leantime\Core\Language;
use PHPUnit\Exception;
/**
* Class Format
*
* This class provides various formatting methods for simple data types (strings, ints, floats)
*/
class Format
{
private mixed $value = '';
private mixed $value2 = '';
private DateTimeHelper $dateTimeHelper;
private Language $language;
/**
* Creates a new instance of the class.
*
* PSA: This class will NOT throw exceptions or error messages since it is a user facing string formatting class.
* If you need to evaluate correct parsing of datetimes use the datetime helper and not this format class.
*
* @param string|int|float $value The value to be assigned. If empty, the constructor will return early.
* @param null|string|int|float $value2 The second value to be assigned. It can be null. Used for certain cases
* as specified by $fromFormat.
* @param FromFormat|null $fromFormat The format of the values. Can be one of the constants defined in the
* FromFormat class.
* @return void
*/
public function __construct(
string|int|float|null|\DateTimeInterface|CarbonInterface $value,
string|int|float|null|\DateTimeInterface|CarbonInterface $value2,
?FromFormat $fromFormat = FromFormat::DbDate
) {
$this->dateTimeHelper = app()->make(DateTimeHelper::class);
$this->language = app()->make(Language::class);
if (empty($value)) {
return;
}
if ($value instanceof \DateTime) {
$value = CarbonImmutable::create($value);
$this->value = CarbonImmutable::create($value);
}
if ($value2 instanceof \DateTime) {
$value2 = CarbonImmutable::create($value2);
$this->value = CarbonImmutable::create($value2);
}
try {
switch ($fromFormat) {
case FromFormat::DbDate:
$this->value = $this->dateTimeHelper->parseDbDateTime($value);
break;
case FromFormat::UserDateTime:
$this->value = $this->dateTimeHelper->parseUserDateTime($value, $value2);
break;
case FromFormat::User24hTime:
$this->value = $this->dateTimeHelper->parseUser24hTime($value);
break;
case FromFormat::Db24hTime:
$this->value = $this->dateTimeHelper->parseDb24hTime($value);
break;
case FromFormat::UserDateStartOfDay:
$this->value = $this->dateTimeHelper->parseUserDateTime($value, 'start');
break;
case FromFormat::UserDateEndOfDay:
$this->value = $this->dateTimeHelper->parseUserDateTime($value, 'end');
break;
default:
$this->value = $value;
break;
}
} catch (\Throwable $e) {
// Several things can throw exceptions in the date parsing scripts.
// Most common is an invalid date format. This could also be an empty string or a 0000-00... date.
// Since this format class is purely for user facing purposes we will not show an error message
// but return an empty string.
$this->value = $value;
return;
}
}
/**
* Returns the user formatted date string based on the 'value' property.
*
* @param string $emptyOutput The output to be returned when the 'value' property is empty.
* Defaults to an empty string.
* @return string The formatted date string or the $emptyOutput if the 'value' property is empty or
* the formatted date string is empty.
*/
public function date(string $emptyOutput = ''): string
{
if (empty($this->value) || ! $this->value instanceof CarbonImmutable) {
return $emptyOutput;
}
$formattedDate = $this->value->formatDateForUser();
return $formattedDate !== '' ? $formattedDate : $emptyOutput;
}
/**
* Retrieve the formatted time string from the ISO value.
* Returns an empty string if 'value' is null.
*
* @return string The formatted time string.
*/
public function time(): string
{
if (empty($this->value) || ! $this->value instanceof CarbonImmutable) {
return '';
}
return $this->value->formatTimeForUser();
}
/**
* Generates an ISO 8601 formatted date and time string in user timezone
*
* @return string The ISO 8601 formatted date and time string. Returns an empty string if the value is null.
*/
public function isoDateTime(): string
{
if (empty($this->value) || ! $this->value instanceof CarbonImmutable) {
return '';
}
return $this->value->setToUserTimezone()->format('Y-m-d H:i:s');
}
/**
* Generates an ISO 8601 formatted date and time string in UTC timezone
*
* @return string The ISO 8601 formatted date and time string. Returns an empty string if the value is null.
*/
public function isoDateTimeUTC(): string
{
if (empty($this->value) || ! $this->value instanceof CarbonImmutable) {
return '';
}
return $this->value->formatDateTimeForDb();
}
/**
* @deprecated
*
* This method is deprecated and only included because of plugin backwards compatibility.
* Once all plugins are updated this will be removed.
*
* @return string The ISO 8601 formatted date string. Returns an empty string if the value is null.
*/
public function isoDate(): string
{
if (empty($this->value)) {
return '';
}
// This method should not be used anymore however we have plugins that are still using it and they will not have
// the new enum values. So they are still calling format($var)->isoDate() without a enum modifier that would
// indicate that this is a user date (which it was historically).
// So now we have to shuffle things around and since the format was probably not correct anyways, let's reparse
if (! $this->value instanceof CarbonImmutable) {
try {
$this->value = $this->dateTimeHelper->parseUserDateTime($this->value, 'start');
} catch (Exception $e) {
report($e);
return '';
}
} else {
// If for some reason Carbon was able to parse the date we'll need to make sure the timezone is set to the
// users timezone.
// Date was falsly parsed as UTC but is actually user date. Shift timezone.
$userTimezone = session('usersettings.timezone');
// Carbon shift timezone will change timezone without actually changing the numbers
$this->value->shiftTimezone($userTimezone);
}
return $this->value->formatDateTimeForDb();
}
/**
* Generate unix timestamp from date.
*/
public function timestamp(): int|bool
{
if (empty($this->value) || ! $this->value instanceof CarbonImmutable) {
return false;
}
return $this->value->getTimestamp();
}
/**
* Generate unix timestamp from date in miliseconds for javascript usage
*/
public function jsTimestamp(): int|bool
{
if (empty($this->value) || ! $this->value instanceof CarbonImmutable) {
return false;
}
return $this->value->getTimestampMs();
}
/**
* Retrieves the 24-hour time string from the ISO formatted value property.
*
* @return string The 24-hour time string. If the value property is null, an empty string is returned.
*/
public function time24(): string
{
if (empty($this->value) || ! $this->value instanceof CarbonImmutable) {
return '';
}
return $this->value->format24HTimeForUser();
}
/**
* Converts a 24-hour formatted time string to a user-friendly time string.
*
* @return string The user-friendly time string in the format "H:i A". Returns an empty string if the value is null.
*/
public function userTime24toUserTime(): string
{
if (empty($this->value) || ! $this->value instanceof CarbonImmutable) {
return '';
}
return $this->value->formatTimeForUser();
}
/**
* Generates a string representation of currency.
*
* @return string The string representation of currency.
*/
public function currency(): string
{
if ($this->value == null) {
return '';
}
return $this->language->__('language.currency').''.number_format($this->value, 2);
}
/**
* Generates a string representation of a percentage.
*
* @return string The percentage string. Returns an empty string if the value is null.
* If the second value is empty, the first value is returned with a "%" sign appended.
* If both values are set, the percentage is calculated and formatted to two decimal places, followed by a "%" sign.
*/
public function percent(): string
{
// First value empty, just return empty string
if ($this->value == null) {
return '';
}
// Second value empty return first value with % sign
if (empty($this->value2)) {
return number_format($this->value, 2).'%';
}
// Both values set. Return percent calculation
$percent = ($this->value / $this->value2) * 100;
return number_format($percent, 2).'%';
}
/**
* Formats a decimal number with two decimal places.
*
* @return string The decimal number formatted with two decimal places.
*/
public function decimal(): string
{
return number_format((float) $this->value, 2);
}
public function diffForHumans(): string
{
if ($this->value->isToday()) {
return $this->language->__('dates.today');
} elseif ($this->value->isYesterday()) {
return $this->language->__('dates.yesterday');
} elseif ($this->value->isTomorrow()) {
return $this->language->__('dates.tomorrow');
} else {
return $this->value->endOfDay()->diffForHumans();
}
}
/**
* Format bytes to human readable format
*
* @return string The formatted size
*/
public function formatBytes(): string
{
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
$bytes = max($this->value, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
return round($bytes / (1024 ** $pow), 2).' '.$units[$pow];
}
}