1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
use Suricate\Suricate; |
6
|
|
|
|
7
|
|
|
// Debug |
8
|
|
|
// |
9
|
|
|
if (!function_exists('_p')) { |
10
|
|
|
function _p() |
11
|
|
|
{ |
12
|
1 |
|
echo '<pre>' . "\n"; |
13
|
1 |
|
foreach (func_get_args() as $var) { |
14
|
1 |
|
print_r($var); |
15
|
1 |
|
echo "\n"; |
16
|
|
|
} |
17
|
1 |
|
echo '</pre>'; |
18
|
1 |
|
} |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
if (!function_exists('_d')) { |
22
|
|
|
function _d() |
23
|
|
|
{ |
24
|
|
|
echo '<pre>' . "\n"; |
25
|
|
|
foreach (func_get_args() as $var) { |
26
|
|
|
var_dump($var); |
27
|
|
|
echo "\n"; |
28
|
|
|
} |
29
|
|
|
echo '</pre>'; |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
if (!function_exists('e')) { |
34
|
|
|
function e($str) |
35
|
|
|
{ |
36
|
|
|
return htmlentities((string) $str, ENT_COMPAT, 'UTF-8'); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
// Arrays |
41
|
|
|
|
42
|
|
|
if (!function_exists('head')) { |
43
|
|
|
function head($arr) |
44
|
|
|
{ |
45
|
1 |
|
return reset($arr); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if (!function_exists('last')) { |
50
|
|
|
function last($arr) |
51
|
|
|
{ |
52
|
1 |
|
return end($arr); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (!function_exists('flatten')) { |
57
|
|
|
function flatten(array $array) |
58
|
|
|
{ |
59
|
5 |
|
$return = []; |
60
|
|
|
array_walk_recursive($array, function ($a) use (&$return) { |
61
|
5 |
|
$return[] = $a; |
62
|
5 |
|
}); |
63
|
5 |
|
return $return; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// Inspired from laravel helper |
68
|
|
|
if (!function_exists('dataGet')) { |
69
|
|
|
function dataGet($target, $key, $default = null) |
70
|
|
|
{ |
71
|
9 |
|
if (is_null($key)) { |
72
|
1 |
|
return $target; |
73
|
|
|
} |
74
|
|
|
|
75
|
9 |
|
foreach (explode('.', $key) as $segment) { |
76
|
9 |
|
if (is_array($target)) { |
77
|
9 |
|
if (!array_key_exists($segment, $target)) { |
78
|
1 |
|
return value($default); |
79
|
|
|
} |
80
|
|
|
|
81
|
9 |
|
$target = $target[$segment]; |
82
|
|
|
} elseif ($target instanceof \ArrayAccess) { |
83
|
|
|
if (!isset($target[$segment])) { |
84
|
|
|
return value($default); |
85
|
|
|
} |
86
|
|
|
$target = $target[$segment]; |
87
|
|
|
} elseif (is_object($target)) { |
88
|
|
|
if (!isset($target->$segment)) { |
89
|
|
|
return value($default); |
90
|
|
|
} |
91
|
|
|
$target = $target->$segment; |
92
|
|
|
} else { |
93
|
|
|
return value($default); |
94
|
|
|
} |
95
|
|
|
} |
96
|
9 |
|
return $target; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if (!function_exists('value')) { |
101
|
|
|
function value($value) |
102
|
|
|
{ |
103
|
2 |
|
return $value instanceof \Closure ? $value() : $value; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
// Classes |
108
|
|
|
|
109
|
|
|
if (!function_exists('classBasename')) { |
110
|
|
|
function classBasename($class) |
111
|
|
|
{ |
112
|
|
|
$class = is_object($class) ? get_class($class) : $class; |
113
|
|
|
|
114
|
|
|
return basename(str_replace('\\', '/', $class)); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if (!function_exists('with')) { |
119
|
|
|
function with($class) |
120
|
|
|
{ |
121
|
4 |
|
return $class; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
// Strings |
126
|
|
|
|
127
|
|
|
if (!function_exists('camelCase')) { |
128
|
|
|
function camelCase($str) |
129
|
|
|
{ |
130
|
1 |
|
return str_replace(' ', '', ucwords(str_replace('_', ' ', $str))); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if (!function_exists('snakeCase')) { |
135
|
|
|
function snakeCase(string $str, $delimiter) |
136
|
|
|
{ |
137
|
|
|
$replace = '$1' . $delimiter . '$2'; |
138
|
|
|
|
139
|
|
|
return ctype_lower($str) |
140
|
|
|
? $str |
141
|
|
|
: strtolower((string) preg_replace('/(.)([A-Z])/', $replace, $str)); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
if (!function_exists('contains')) { |
146
|
|
|
function contains($haystack, $needles) |
147
|
|
|
{ |
148
|
1 |
|
foreach ((array) $needles as $currentNeedle) { |
149
|
1 |
|
if (strpos($haystack, $currentNeedle) !== false) { |
150
|
1 |
|
return true; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
1 |
|
return false; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if (!function_exists('startsWith')) { |
159
|
|
|
function startsWith($haystack, $needles) |
160
|
|
|
{ |
161
|
1 |
|
foreach ((array) $needles as $currentNeedle) { |
162
|
1 |
|
if (strpos($haystack, $currentNeedle) === 0) { |
163
|
1 |
|
return true; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
1 |
|
return false; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
if (!function_exists('endsWith')) { |
172
|
|
|
function endsWith($haystack, $needles) |
173
|
|
|
{ |
174
|
1 |
|
foreach ((array) $needles as $currentNeedle) { |
175
|
|
|
if ( |
176
|
|
|
$currentNeedle == |
177
|
1 |
|
substr($haystack, strlen($haystack) - strlen($currentNeedle)) |
178
|
|
|
) { |
179
|
1 |
|
return true; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
1 |
|
return false; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
if (!function_exists('wordLimit')) { |
188
|
|
|
function wordLimit($str, $limit = 100, $end = '...') |
189
|
|
|
{ |
190
|
1 |
|
if (strlen($str) < $limit) { |
191
|
1 |
|
return $str; |
192
|
|
|
} |
193
|
|
|
|
194
|
1 |
|
$substr = substr($str, 0, $limit); |
195
|
1 |
|
$spacePos = strrpos($substr, ' '); |
196
|
1 |
|
if ($spacePos !== false) { |
197
|
1 |
|
return substr($substr, 0, $spacePos) . $end; |
198
|
|
|
} else { |
199
|
1 |
|
return $substr . $end; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
if (!function_exists('slug')) { |
205
|
|
|
function slug($str) |
206
|
|
|
{ |
207
|
1 |
|
if (class_exists('Transliterator')) { |
208
|
1 |
|
$translit = \Transliterator::create( |
209
|
1 |
|
'Any-Latin; NFD; [:Nonspacing Mark:] Remove; NFC; [:Punctuation:] Remove; Lower();' |
210
|
|
|
); |
211
|
1 |
|
if ($translit !== null) { |
212
|
1 |
|
return preg_replace( |
213
|
1 |
|
'/\s/', |
214
|
1 |
|
'-', |
215
|
1 |
|
(string) $translit->transliterate($str) |
216
|
|
|
); |
217
|
|
|
} |
218
|
|
|
throw new \RuntimeException("Cannot instanciate Transliterator"); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
if (!function_exists('app')) { |
224
|
|
|
function app() |
225
|
|
|
{ |
226
|
1 |
|
return Suricate::App(); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
if (!function_exists('app_path')) { |
231
|
|
|
function app_path($str = '') |
232
|
|
|
{ |
233
|
2 |
|
return Suricate::App()->getParameter('path.app') . |
234
|
2 |
|
($str ? '/' . $str : $str); |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
if (!function_exists('base_path')) { |
239
|
|
|
function base_path($str = '') |
240
|
|
|
{ |
241
|
|
|
return Suricate::App()->getParameter('path.base') . |
242
|
|
|
($str ? '/' . $str : $str); |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
if (!function_exists('public_path')) { |
247
|
|
|
function public_path($str = '') |
248
|
|
|
{ |
249
|
|
|
return Suricate::App()->getParameter('path.public') . |
250
|
|
|
($str ? '/' . $str : $str); |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
if (!function_exists('url')) { |
255
|
|
|
function url($str = '/') |
256
|
|
|
{ |
257
|
|
|
return Suricate::App()->getParameter('url') . $str; |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
if (!function_exists('getPostParam')) { |
262
|
|
|
function getPostParam($param, $defaultValue = null) |
263
|
|
|
{ |
264
|
|
|
return Suricate::Request()->getPostParam($param, $defaultValue); |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
if (!function_exists('getEnvParam')) { |
269
|
|
|
function getEnvParam($param, $defaultValue = null) |
270
|
|
|
{ |
271
|
|
|
$env = getenv($param); |
272
|
|
|
|
273
|
|
|
return $env === false ? $defaultValue : $env; |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
if (!function_exists('getParam')) { |
278
|
|
|
function getParam($param, $defaultValue = null) |
279
|
|
|
{ |
280
|
|
|
return Suricate::Request()->getParam($param, $defaultValue); |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* i18n |
286
|
|
|
* |
287
|
|
|
* @return void |
288
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
289
|
|
|
*/ |
290
|
|
|
if (!function_exists('i18n')) { |
291
|
|
|
function i18n() |
292
|
|
|
{ |
293
|
|
|
return call_user_func_array([Suricate::I18n(), 'get'], func_get_args()); |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
if (!function_exists('generateUuid')) { |
298
|
|
|
// Via https://rogerstringer.com/2013/11/15/generate-uuids-php/ |
299
|
|
|
function generateUuid() |
300
|
|
|
{ |
301
|
|
|
return sprintf( |
302
|
|
|
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x', |
303
|
|
|
mt_rand(0, 0xffff), |
304
|
|
|
mt_rand(0, 0xffff), |
305
|
|
|
mt_rand(0, 0xffff), |
306
|
|
|
mt_rand(0, 0x0fff) | 0x4000, |
307
|
|
|
mt_rand(0, 0x3fff) | 0x8000, |
308
|
|
|
mt_rand(0, 0xffff), |
309
|
|
|
mt_rand(0, 0xffff), |
310
|
|
|
mt_rand(0, 0xffff) |
311
|
|
|
); |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
TODO : implements i18n |
317
|
|
|
**/ |
318
|
|
|
if (!function_exists('niceTime')) { |
319
|
|
|
function niceTime($time) |
320
|
|
|
{ |
321
|
1 |
|
$delta = time() - $time; |
322
|
1 |
|
if ($delta < 60) { |
323
|
1 |
|
return 'il y a moins d\'une minute.'; |
324
|
1 |
|
} elseif ($delta < 120) { |
325
|
1 |
|
return 'il y a environ une minute.'; |
326
|
1 |
|
} elseif ($delta < 45 * 60) { |
327
|
1 |
|
return 'il y a ' . floor($delta / 60) . ' minutes.'; |
328
|
1 |
|
} elseif ($delta < 120 * 60) { |
329
|
1 |
|
return 'il y a environ une heure.'; |
330
|
1 |
|
} elseif ($delta < 24 * 60 * 60) { |
331
|
1 |
|
return 'il y a environ ' . floor($delta / 3600) . ' heures.'; |
332
|
1 |
|
} elseif ($delta < 48 * 60 * 60) { |
333
|
1 |
|
return 'hier.'; |
334
|
1 |
|
} elseif ($delta < 30 * 24 * 3600) { |
335
|
1 |
|
return 'il y a ' . floor($delta / 86400) . ' jours.'; |
336
|
1 |
|
} elseif ($delta < 365 * 24 * 3600) { |
337
|
1 |
|
return 'il y a ' . floor($delta / (24 * 3600 * 30)) . ' mois.'; |
338
|
|
|
} else { |
339
|
1 |
|
$diff = floor($delta / (24 * 3600 * 365)); |
340
|
|
|
|
341
|
1 |
|
if ($diff == 1) { |
342
|
1 |
|
return 'il y a plus d\'un an.'; |
343
|
|
|
} else { |
344
|
1 |
|
return 'il y a plus de ' . $diff . ' ans.'; |
345
|
|
|
} |
346
|
|
|
} |
347
|
|
|
} |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
if (!function_exists('dispatchEvent')) { |
351
|
|
|
function dispatchEvent($event, $payload = null) |
352
|
|
|
{ |
353
|
|
|
Suricate::EventDispatcher()->fire($event, $payload); |
354
|
|
|
} |
355
|
|
|
} |
356
|
|
|
|