Completed
Pull Request — master (#2)
by Mathieu
05:06 queued 01:16
created

Helper.php ➔ wordLimit()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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