Test Failed
Push — master ( 55c49a...565016 )
by Julien
05:04
created

Slug   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
eloc 19
c 0
b 0
f 0
dl 0
loc 46
ccs 0
cts 21
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A restoreLocale() 0 7 2
A generate() 0 12 3
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
    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
        $oldLocale = setlocale(LC_ALL, '0');
25
        setlocale(LC_ALL, 'en_US.UTF-8');
26
        if (!empty($replace) && is_array($replace)) {
27
            $string = str_replace(array_keys($replace), array_values($replace), $string);
28
        }
29
        $transliterator = Transliterator::create('Any-Latin; Latin-ASCII');
30
        $string = $transliterator->transliterate(mb_convert_encoding(htmlspecialchars_decode($string), 'UTF-8', 'auto'));
0 ignored issues
show
Bug introduced by
It seems like mb_convert_encoding(html...ring), 'UTF-8', 'auto') can also be of type array; however, parameter $subject of Transliterator::transliterate() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

30
        $string = $transliterator->transliterate(/** @scrutinizer ignore-type */ mb_convert_encoding(htmlspecialchars_decode($string), 'UTF-8', 'auto'));
Loading history...
31
        self::restoreLocale($oldLocale);
32
        return self::cleanString($string, $delimiter);
33
    }
34
    
35
    /**
36
     * Revert back to the old locale
37
     */
38
    private static function restoreLocale(string $oldLocale): void
39
    {
40
        if ((stripos($oldLocale, '=') > 0)) {
41
            parse_str(str_replace(';', '&', $oldLocale), $locales);
42
            $oldLocale = array_values($locales);
43
        }
44
        setlocale(LC_ALL, $oldLocale);
45
    }
46
    
47
    /**
48
     * Replace non-letter or non-digits by -
49
     * Trim trailing -
50
     */
51
    public static function cleanString(string $string, string $delimiter): string
52
    {
53
        // replace non letter or non digits by -
54
        $string = preg_replace('#[^\pL\d]+#u', '-', $string);
55
        
56
        // Trim trailing -
57
        $string = trim($string, '-');
58
        $clean = preg_replace('~[^-\w]+~', '', $string);
59
        $clean = strtolower($clean);
60
        $clean = preg_replace('#[\/_|+ -]+#', $delimiter, $clean);
61
        return trim($clean, $delimiter);
62
    }
63
}
64