-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmacros.php
More file actions
95 lines (72 loc) · 3.19 KB
/
macros.php
File metadata and controls
95 lines (72 loc) · 3.19 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
<?php
use Carbon\Carbon;
use Illuminate\Support\Number;
use Illuminate\Support\Str;
function initStringMacros()
{
Str::macro('ref', function (int $length = 6) {
throw_if($length < 2, new InvalidArgumentException('Length must be at least 2'));
// exclude similar looking characters: i, l, o, 0, 1, u
$letters = 'abcdefghjkmnpqrstvwxyz';
$digits = '23456789';
$pool = $letters . $digits;
$ref = $letters[random_int(0, strlen($letters) - 1)];
$digitPosition = random_int(1, $length - 1);
for ($i = 1; $i < $length; $i++) {
if ($i === $digitPosition) {
$ref .= $digits[random_int(0, strlen($digits) - 1)];
} else {
$ref .= $pool[random_int(0, strlen($pool) - 1)];
}
}
return $ref;
});
}
function initNumberMacros()
{
// Return number formatted to currency with input
Number::macro('formatCurrencyToInput', function (mixed $number, ?string $in = null, int $precision = 0, string $inputTemplate = '%s'): string {
$formattedNumber = Number::currencyForHumans($number, $in ?? Number::$currency, $precision);
$formattedNumber = preg_replace('/\xc2\xa0|[, .]/', '', $formattedNumber);
// return $input Kč
// return CZK $input
$inputWithNumber = sprintf($inputTemplate, $number);
return preg_replace('/\d+/', $inputWithNumber, $formattedNumber);
});
// Return number formatted to currency
// Or return currency symbol if no number is provided
Number::macro('currencyForHumans', function (mixed $number = null, ?string $in = null, int $precision = 0): string {
$formatter = new NumberFormatter(app()->getLocale(), NumberFormatter::CURRENCY);
if (is_null($number)) {
$formatter->setTextAttribute(NumberFormatter::CURRENCY_CODE, $in ?? Number::$currency);
return $formatter->getSymbol(NumberFormatter::CURRENCY_SYMBOL);
}
$number = (float) $number;
$formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, $precision);
return $formatter->formatCurrency($number, $in ?? Number::$currency);
});
}
function initCarbonMacros()
{
Carbon::macro('dateForHumans', fn () => $this->isoFormat('L'));
Carbon::macro('dateTimeForHumans', fn () => $this->isoFormat('L LT'));
Carbon::macro('myDiffForHumans', function (): string {
$diff = $this->diffForHumans();
$seconds = $this->diffInSeconds(now(), true);
if ($seconds < 60) {
return __('ig-common::layouts.just_now');
}
// show 1 year for date between 11 months and 15 days and 12 months and 15 days
$fullDiff = $this->diff(now(), true);
if (($fullDiff->m == 11 && $fullDiff->d >= 15) || ($fullDiff->m == 12 && $fullDiff->d <= 15)) {
return '1 ' . __('ig-common::layouts.year');
}
return $diff;
});
Carbon::macro('timeForHumans', function () {
return preg_replace(['/:00 /', '/^0/'], '', $this->isoFormat('LT'));
});
Carbon::macro('randomWorkTime', function (int $from = 9, int $to = 17) {
return $this->setTime(rand($from, $to - 1), rand(0, 59), rand(0, 59));
});
}