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