Test Failed
Push — master ( be437e...866f6c )
by Julien
07:25
created

Slug   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 20
c 0
b 0
f 0
dl 0
loc 52
ccs 22
cts 22
cp 1
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A restoreLocale() 0 7 2
A generate() 0 13 2
A cleanString() 0 11 1
1
<?php
2
3
/**
4
 * This file is part of the Zemit Framework.
5
 *
6
 * (c) Zemit Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zemit\Support;
13
14
use Transliterator;
15
16
class Slug
17
{
18
    /**
19
     * Creates a slug to be used for pretty URLs
20
     */
21 3
    public static function generate(string $string, array $replace = [], string $delimiter = '-'): string
22
    {
23
        // Save the old locale and set the new locale to UTF-8
24 3
        $oldLocale = setlocale(LC_ALL, '0');
25 3
        setlocale(LC_ALL, 'en_US.UTF-8');
26 3
        if (!empty($replace)) {
27 1
            $string = str_replace(array_keys($replace), array_values($replace), $string);
28
        }
29 3
        $transliterator = Transliterator::create('Any-Latin; Latin-ASCII');
30 3
        assert($transliterator instanceof Transliterator);
31 3
        $string = $transliterator->transliterate((string)mb_convert_encoding(htmlspecialchars_decode($string), 'UTF-8', 'auto'));
32 3
        self::restoreLocale($oldLocale);
33 3
        return self::cleanString($string, $delimiter);
34
    }
35
    
36
    /**
37
     * Restore the locale settings based on the provided old locale.
38
     *
39
     * @param string|string[] $oldLocale The old locale settings.
40
     *                                   Can be either a string or an array of locale settings.
41
     *                                   If a string, it will be parsed and converted to an array of locale settings.
42
     * @return void
43
     */
44 3
    private static function restoreLocale(string|array $oldLocale): void
45
    {
46 3
        if (is_string($oldLocale)) {
0 ignored issues
show
introduced by
The condition is_string($oldLocale) is always false.
Loading history...
47 3
            parse_str(str_replace(';', '&', $oldLocale), $locales);
48 3
            $oldLocale = array_values($locales);
49
        }
50 3
        setlocale(LC_ALL, $oldLocale);
51
    }
52
    
53
    /**
54
     * Replace non-letter or non-digits by -
55
     * Trim trailing -
56
     */
57 3
    public static function cleanString(string $string, string $delimiter): string
58
    {
59
        // replace non letter or non digits by -
60 3
        $string = preg_replace('#[^\pL\d]+#u', '-', $string);
61
        
62
        // Trim trailing -
63 3
        $string = trim($string, '-');
64 3
        $clean = preg_replace('~[^-\w]+~', '', $string);
65 3
        $clean = strtolower($clean);
66 3
        $clean = preg_replace('#[\/_|+ -]+#', $delimiter, $clean);
67 3
        return trim($clean, $delimiter);
68
    }
69
}
70