Completed
Push — master ( 28e5e2...3bec47 )
by Mathieu
01:38
created

Helper.php ➔ value()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 1
cts 1
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
use Suricate\Suricate;
3
4
// Debug
5
// 
6
if (!function_exists('_p')) {
7
    function _p()
8
    {
9
        echo '<pre>';
10
        foreach (func_get_args() as $var) {
11
            print_r($var);
12
            echo "\n";
13
        }
14
        echo '</pre>';
15
    }
16
}
17
18
if (!function_exists('_d')) {
19
    function _d()
20
    {
21
        echo '<pre>';
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
        return reset($arr);
43
    }
44
}
45
46
if (!function_exists('last')) {
47
    function last($arr)
48
    {
49
        return end($arr);
50
    }
51
}
52
53
if (!function_exists('flatten')) {
54
    function flatten(array $array)
55
    {
56
        $return = array();
57
        array_walk_recursive($array, function ($a) use (&$return) {
58
            $return[] = $a;
59
        });
60
        return $return;
61
    }
62
}
63
64
// Inspired from laravel helper
65
if (!function_exists('dataGet')) {
66
    function dataGet($target, $key, $default = null)
67
    {
68 2
        if (is_null($key)) {
69 1
            return $target;
70
        }
71
        
72 2
        foreach (explode('.', $key) as $segment) {
73 2
            if (is_array($target)) {
74 2
                if (!array_key_exists($segment, $target)) {
75 1
                    return value($default);
76
                }
77
78 2
                $target = $target[$segment];
79 2
            } 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 2
        }
93 2
        return $target;
94
    }
95
}
96
97
if (!function_exists('value')) {
98
    function value($value)
99
    {
100 1
        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
        return $class;
119
    }
120
}
121
122
// Strings
123
124
if (!function_exists('camelCase')) {
125
    function camelCase($str)
126
    {
127
        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 View Code Duplication
if (!function_exists('contains')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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 View Code Duplication
if (!function_exists('startsWith')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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 = '')
0 ignored issues
show
Coding Style introduced by
As per coding-style, this function should be in camelCase.

CamelCase (...) is the practice of writing compound words or phrases such that
each word or abbreviation begins with a capital letter.

Learn more about camelCase.

Loading history...
236
    {
237
        return Suricate::App()->getParameter('path.app')
238
            . ($str ? '/' . $str : $str);
239
    }
240
}
241
242
if (!function_exists('base_path')) {
243
    function base_path($str = '')
0 ignored issues
show
Coding Style introduced by
As per coding-style, this function should be in camelCase.

CamelCase (...) is the practice of writing compound words or phrases such that
each word or abbreviation begins with a capital letter.

Learn more about camelCase.

Loading history...
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 = '')
0 ignored issues
show
Coding Style introduced by
As per coding-style, this function should be in camelCase.

CamelCase (...) is the practice of writing compound words or phrases such that
each word or abbreviation begins with a capital letter.

Learn more about camelCase.

Loading history...
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(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 View Code Duplication
        } elseif ($delta < (24 * 60 * 60)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
320
            return 'il y a environ ' . floor($delta / 3600) . ' heures.';
321
        } elseif ($delta < (48 * 60 * 60)) {
322
            return 'hier';
323 View Code Duplication
        } elseif ($delta < 30 * 24 *3600) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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