|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use Illuminate\Support\Arr; |
|
4
|
|
|
use Illuminate\Support\Carbon; |
|
5
|
|
|
use Illuminate\Support\Facades\Http; |
|
6
|
|
|
use Illuminate\Support\Facades\Storage; |
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
|
|
9
|
|
|
if (! function_exists('terbilang')) { |
|
10
|
|
|
/** |
|
11
|
|
|
* Return the given value into readable number. |
|
12
|
|
|
* |
|
13
|
|
|
* @param mixed $value |
|
14
|
|
|
* @return string |
|
15
|
|
|
*/ |
|
16
|
|
|
function terbilang($value): string |
|
17
|
|
|
{ |
|
18
|
8 |
|
$result = value(function () use ($value) { |
|
19
|
8 |
|
$angka = ['', 'satu', 'dua', 'tiga', 'empat', 'lima', 'enam', 'tujuh', 'delapan', 'sembilan', 'sepuluh', 'sebelas']; |
|
20
|
|
|
|
|
21
|
8 |
|
$number = abs($value); |
|
22
|
|
|
|
|
23
|
|
|
switch (true) { |
|
24
|
8 |
|
case $number < 12: |
|
25
|
8 |
|
return ' ' . $angka[$number]; |
|
26
|
|
|
|
|
27
|
8 |
|
case $number < 20: |
|
28
|
|
|
return terbilang($number - 10) . ' belas'; |
|
29
|
|
|
|
|
30
|
8 |
|
case $number < 100: |
|
31
|
3 |
|
return terbilang($number / 10) . ' puluh ' . terbilang($number % 10); |
|
32
|
|
|
|
|
33
|
8 |
|
case $number < 200: |
|
34
|
5 |
|
return 'seratus ' . terbilang($number - 100); |
|
35
|
|
|
|
|
36
|
7 |
|
case $number < 1000: |
|
37
|
4 |
|
return terbilang($number / 100) . ' ratus ' . terbilang($number % 100); |
|
38
|
|
|
|
|
39
|
6 |
|
case $number < 2000: |
|
40
|
4 |
|
return 'seribu ' . terbilang($number - 1000); |
|
41
|
|
|
|
|
42
|
6 |
|
case $number < 1000000: |
|
43
|
6 |
|
return terbilang($number / 1000) . ' ribu ' . terbilang($number % 1000); |
|
44
|
|
|
|
|
45
|
|
|
case $number < 1000000000: |
|
46
|
|
|
return terbilang($number / 1000000) . ' juta ' . terbilang($number % 1000000); |
|
47
|
|
|
|
|
48
|
|
|
case $number < 1000000000000: |
|
49
|
|
|
return terbilang($number / 1000000000) . ' milyar ' . terbilang($number % 1000000000); |
|
50
|
|
|
|
|
51
|
|
|
case $number < 1000000000000000: |
|
52
|
|
|
return terbilang($number / 1000000000000) . ' trilyun ' . terbilang($number % 1000000000000); |
|
53
|
|
|
} |
|
54
|
8 |
|
}); |
|
55
|
|
|
|
|
56
|
8 |
|
return trim(($value < 0 ? 'minus ' : '') . $result); |
|
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if (! function_exists('greeting')) { |
|
61
|
|
|
/** |
|
62
|
|
|
* Return specific greeting based on the current hour. |
|
63
|
|
|
* |
|
64
|
|
|
* @param \Illuminate\Support\Carbon|null $date |
|
65
|
|
|
* @return string |
|
66
|
|
|
*/ |
|
67
|
|
|
function greeting(Carbon $date = null): string |
|
68
|
|
|
{ |
|
69
|
1 |
|
$hour = ($date ?? Carbon::now())->format('H'); |
|
70
|
|
|
|
|
71
|
|
|
switch (true) { |
|
72
|
1 |
|
case $hour < 12: |
|
73
|
1 |
|
return trans('Good Morning'); |
|
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
case $hour < 15: |
|
76
|
|
|
return trans('Good Afternoon'); |
|
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
case $hour < 18: |
|
79
|
|
|
return trans('Good Evening'); |
|
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
default: |
|
82
|
|
|
return trans('Good Night'); |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
if (! function_exists('telegram_url')) { |
|
88
|
|
|
/** |
|
89
|
|
|
* Return base telegram bot API url. |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $method |
|
92
|
|
|
* @return string |
|
93
|
|
|
*/ |
|
94
|
|
|
function telegram_url(string $method): string |
|
95
|
|
|
{ |
|
96
|
|
|
return sprintf('https://api.telegram.org/bot%s/%s', env('TELEGRAM_TOKEN'), $method); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
if (! function_exists('download_telegram_photo')) { |
|
101
|
|
|
/** |
|
102
|
|
|
* Download telegram photo based on the given photo list. |
|
103
|
|
|
* |
|
104
|
|
|
* @param array $photos (from $this->getBot()->getMessage()->getPayload()) |
|
105
|
|
|
* @param string $path |
|
106
|
|
|
* @return string |
|
107
|
|
|
* |
|
108
|
|
|
* @throws \Illuminate\Http\Client\RequestException |
|
109
|
|
|
* @throws \InvalidArgumentException |
|
110
|
|
|
*/ |
|
111
|
|
|
function download_telegram_photo(array $photos, string $path) |
|
112
|
|
|
{ |
|
113
|
|
|
$photos = Arr::sort($photos, fn ($photo) => $photo['file_size']); |
|
114
|
|
|
|
|
115
|
|
|
$photoId = Arr::last($photos)['file_id']; |
|
116
|
|
|
|
|
117
|
|
|
throw_unless($photoId, InvalidArgumentException::class, sprintf( |
|
118
|
|
|
'Telegram file_id is not found', $photoId |
|
119
|
|
|
)); |
|
120
|
|
|
|
|
121
|
|
|
$photoFilePath = Http::get(telegram_url('getFile'), [ |
|
122
|
|
|
'file_id' => $photoId, |
|
123
|
|
|
])->throw()->json('result.file_path', null); |
|
124
|
|
|
|
|
125
|
|
|
throw_unless($photoFilePath, InvalidArgumentException::class, sprintf( |
|
126
|
|
|
'Telegram file path for file_id %s is not found', $photoId |
|
127
|
|
|
)); |
|
128
|
|
|
|
|
129
|
|
|
$photo = file_get_contents(sprintf( |
|
130
|
|
|
'https://api.telegram.org/file/bot%s/%s', |
|
131
|
|
|
env('TELEGRAM_TOKEN'), $photoFilePath |
|
|
|
|
|
|
132
|
|
|
)); |
|
133
|
|
|
|
|
134
|
|
|
$filename = Str::random() . '.' . pathinfo($photoFilePath, PATHINFO_EXTENSION); |
|
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
Storage::put($path . '/' . $filename, $photo); |
|
137
|
|
|
|
|
138
|
|
|
return $filename; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
if (! function_exists('google_map_url')) { |
|
143
|
|
|
/** |
|
144
|
|
|
* Return google map url based on the given latitude and longitude. |
|
145
|
|
|
* |
|
146
|
|
|
* @param float $latitude |
|
147
|
|
|
* @param float $longitude |
|
148
|
|
|
* @param string $zoom |
|
149
|
|
|
* @return string |
|
150
|
|
|
*/ |
|
151
|
|
|
function google_map_url(float $latitude, float $longitude, string $zoom = '20z'): string |
|
|
|
|
|
|
152
|
|
|
{ |
|
153
|
69 |
|
return sprintf( |
|
154
|
69 |
|
'https://maps.google.com/?q=%s,%s&z=%s', |
|
155
|
|
|
// 'https://www.google.com/maps/@%s,%s,%s', |
|
156
|
69 |
|
$latitude, $longitude, $zoom |
|
157
|
|
|
); |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
if (! function_exists('format_rupiah')) { |
|
162
|
|
|
/** |
|
163
|
|
|
* Return number in rupiah format. |
|
164
|
|
|
* |
|
165
|
|
|
* @param float $value |
|
166
|
|
|
* @param string $prefix |
|
167
|
|
|
* @return string |
|
168
|
|
|
*/ |
|
169
|
|
|
function format_rupiah(float $value, string $prefix = 'Rp'): string |
|
170
|
|
|
{ |
|
171
|
|
|
return $prefix . number_format($value, 0, ',', '.'); |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
if (! function_exists('gravatar_image')) { |
|
176
|
|
|
/** |
|
177
|
|
|
* Return avatar image url using gravatar. |
|
178
|
|
|
* |
|
179
|
|
|
* @param string $email |
|
180
|
|
|
* @param string $extension |
|
181
|
|
|
* @param int $size |
|
182
|
|
|
* @return string |
|
183
|
|
|
*/ |
|
184
|
|
|
function gravatar_image(string $email, string $extension = 'png', int $size = 30): string |
|
185
|
|
|
{ |
|
186
|
19 |
|
return sprintf('https://www.gravatar.com/avatar/%s.%s?s=%s', md5($email), $extension, $size); |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|