Passed
Push — develop ( b21b17...91ba7c )
by Mathieu
02:21
created

slug()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.0123

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 2
b 0
f 0
nc 3
nop 1
dl 0
loc 14
ccs 8
cts 9
cp 0.8889
crap 3.0123
rs 9.9666
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((string) $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
                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(string $str, $delimiter)
133
    {
134
        $replace = '$1' . $delimiter . '$2';
135
136
        return ctype_lower($str)
137
            ? $str
138
            : strtolower((string) preg_replace('/(.)([A-Z])/', $replace, $str));
139
    }
140
}
141
142
if (!function_exists('contains')) {
143
    function contains($haystack, $needles)
144
    {
145 1
        foreach ((array) $needles as $currentNeedle) {
146 1
            if (strpos($haystack, $currentNeedle) !== false) {
147 1
                return true;
148
            }
149
        }
150
151 1
        return false;
152
    }
153
}
154
155
if (!function_exists('startsWith')) {
156
    function startsWith($haystack, $needles)
157
    {
158 1
        foreach ((array) $needles as $currentNeedle) {
159 1
            if (strpos($haystack, $currentNeedle) === 0) {
160 1
                return true;
161
            }
162
        }
163
164 1
        return false;
165
    }
166
}
167
168
if (!function_exists('endsWith')) {
169
    function endsWith($haystack, $needles)
170
    {
171 1
        foreach ((array) $needles as $currentNeedle) {
172
            if (
173
                $currentNeedle ==
174 1
                substr($haystack, strlen($haystack) - strlen($currentNeedle))
175
            ) {
176 1
                return true;
177
            }
178
        }
179
180 1
        return false;
181
    }
182
}
183
184
if (!function_exists('wordLimit')) {
185
    function wordLimit($str, $limit = 100, $end = '...')
186
    {
187 1
        if (strlen($str) < $limit) {
188
            return $str;
189
        }
190
191 1
        $substr = substr($str, 0, $limit);
192 1
        $spacePos = strrpos($substr, ' ');
193 1
        if ($spacePos !== false) {
194 1
            return substr($substr, 0, $spacePos) . $end;
195
        } else {
196 1
            return $substr . $end;
197
        }
198
    }
199
}
200
201
if (!function_exists('slug')) {
202
    function slug($str)
203
    {
204 1
        if (class_exists('Transliterator')) {
205 1
            $translit = \Transliterator::create(
206 1
                'Any-Latin; NFD; [:Nonspacing Mark:] Remove; NFC; [:Punctuation:] Remove; Lower();'
207
            );
208 1
            if ($translit !== null) {
209 1
                return preg_replace(
210 1
                    '/\s/',
211 1
                    '-',
212 1
                    (string) $translit->transliterate($str)
213
                );
214
            }
215
            throw new \RuntimeException("Cannot instanciate Transliterator");
216
        }
217
    }
218
}
219
220
if (!function_exists('app')) {
221
    function app()
222
    {
223 1
        return Suricate::App();
224
    }
225
}
226
227
if (!function_exists('app_path')) {
228
    function app_path($str = '')
229
    {
230 2
        return Suricate::App()->getParameter('path.app') .
231 2
            ($str ? '/' . $str : $str);
232
    }
233
}
234
235
if (!function_exists('base_path')) {
236
    function base_path($str = '')
237
    {
238
        return Suricate::App()->getParameter('path.base') .
239
            ($str ? '/' . $str : $str);
240
    }
241
}
242
243
if (!function_exists('public_path')) {
244
    function public_path($str = '')
245
    {
246
        return Suricate::App()->getParameter('path.public') .
247
            ($str ? '/' . $str : $str);
248
    }
249
}
250
251
if (!function_exists('url')) {
252
    function url($str = '/')
253
    {
254
        return Suricate::App()->getParameter('url') . $str;
255
    }
256
}
257
258
if (!function_exists('getPostParam')) {
259
    function getPostParam($param, $defaultValue = null)
260
    {
261
        return Suricate::Request()->getPostParam($param, $defaultValue);
262
    }
263
}
264
265
if (!function_exists('getParam')) {
266
    function getParam($param, $defaultValue = null)
267
    {
268
        return Suricate::Request()->getParam($param, $defaultValue);
269
    }
270
}
271
272
if (!function_exists('i18n')) {
273
    function i18n()
274
    {
275
        return call_user_func_array([Suricate::I18n(), 'get'], func_get_args());
276
    }
277
}
278
279
if (!function_exists('generateUuid')) {
280
    // Via https://rogerstringer.com/2013/11/15/generate-uuids-php/
281
    function generateUuid()
282
    {
283
        return sprintf(
284
            '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
285
            mt_rand(0, 0xffff),
286
            mt_rand(0, 0xffff),
287
            mt_rand(0, 0xffff),
288
            mt_rand(0, 0x0fff) | 0x4000,
289
            mt_rand(0, 0x3fff) | 0x8000,
290
            mt_rand(0, 0xffff),
291
            mt_rand(0, 0xffff),
292
            mt_rand(0, 0xffff)
293
        );
294
    }
295
}
296
297
/**
298
 TODO : implements i18n
299
 **/
300
if (!function_exists('niceTime')) {
301
    function niceTime($time)
302
    {
303 1
        $delta = time() - $time;
304 1
        if ($delta < 60) {
305 1
            return 'il y a moins d\'une minute.';
306 1
        } elseif ($delta < 120) {
307 1
            return 'il y a environ une minute.';
308 1
        } elseif ($delta < 45 * 60) {
309 1
            return 'il y a ' . floor($delta / 60) . ' minutes.';
310 1
        } elseif ($delta < 120 * 60) {
311 1
            return 'il y a environ une heure.';
312 1
        } elseif ($delta < 24 * 60 * 60) {
313 1
            return 'il y a environ ' . floor($delta / 3600) . ' heures.';
314 1
        } elseif ($delta < 48 * 60 * 60) {
315 1
            return 'hier.';
316 1
        } elseif ($delta < 30 * 24 * 3600) {
317 1
            return 'il y a ' . floor($delta / 86400) . ' jours.';
318 1
        } elseif ($delta < 365 * 24 * 3600) {
319 1
            return 'il y a ' . floor($delta / (24 * 3600 * 30)) . ' mois.';
320
        } else {
321 1
            $diff = floor($delta / (24 * 3600 * 365));
322
323 1
            if ($diff == 1) {
324 1
                return 'il y a plus d\'un an.';
325
            } else {
326 1
                return 'il y a plus de ' . $diff . ' ans.';
327
            }
328
        }
329
    }
330
}
331