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