Text::slug()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 12
cts 12
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 12
nc 2
nop 3
crap 2
1
<?php
2
3
namespace WSW\SimpleUpload\Support;
4
5
use RuntimeException;
6
7
/**
8
 * Class Text
9
 * @package WSW\SimpleUpload\Support
10
 */
11
abstract class Text
12
{
13
    /**
14
     * @param string $string
15
     * @param string $delimiter
16
     * @param array $replace
17
     * @return string
18
     */
19 3
    public static function slug($string, $delimiter = '-', array $replace = [])
20
    {
21 3
        $oldLocale = setlocale(LC_ALL, '0');
22 3
        setlocale(LC_ALL, 'en_US.UTF-8');
23 3
        $clean = iconv('UTF-8', 'ASCII//TRANSLIT', $string);
24
25 3
        if (!empty($replace)) {
26 1
            $clean = str_replace($replace, '', $clean);
27
        }
28
29 3
        $clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
30 3
        $clean = strtolower($clean);
31 3
        $clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
32 3
        $clean = trim($clean, $delimiter);
33
34 3
        setlocale(LC_ALL, $oldLocale);
35 3
        return $clean;
36
    }
37
}
38