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