Test Failed
Push — master ( ffc597...4abc3b )
by Julien
12:58 queued 09:01
created

Slug::cleanString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * This file is part of the Zemit Framework.
4
 *
5
 * (c) Zemit Team <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE.txt
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Zemit\Utils;
12
13
use Transliterator;
14
use Zemit\Exception;
15
16
/**
17
 * Class Slug
18
 *
19
 * @author Julien Turbide <[email protected]>
20
 * @copyright Zemit Team <[email protected]>
21
 *
22
 * @since 1.0
23
 * @version 1.0
24
 *
25
 * @package Zemit\Utils
26
 */
27
class Slug
28
{
29
    /**
30
     * Creates a slug to be used for pretty URLs.
31
     *
32
     * @param  string $string
33
     * @param  array  $replace
34
     * @param  string $delimiter
35
     * @return string
36
     *
37
     * @throws Exception
38
     */
39 1
    public static function generate($string, $replace = [], $delimiter = '-')
40
    {
41 1
        if (!extension_loaded('intl')) {
42
            throw new Exception('intl module not loaded');
43
        }
44 1
        if (!extension_loaded('mbstring')) {
45
            throw new Exception('mbstring module not loaded');
46
        }
47
        // Save the old locale and set the new locale to UTF-8
48 1
        $oldLocale = setlocale(LC_ALL, '0');
49 1
        setlocale(LC_ALL, 'en_US.UTF-8');
50
        // Better to replace given $replace array as index => value
51
        // Example $replace['ı' => 'i', 'İ' => 'i'];
52 1
        if (!empty($replace) && is_array($replace)) {
53
            $string = str_replace(array_keys($replace), array_values($replace), $string);
54
        }
55 1
        $transliterator = Transliterator::create('Any-Latin; Latin-ASCII');
56 1
        $string = $transliterator->transliterate(
57 1
            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

57
            /** @scrutinizer ignore-type */ mb_convert_encoding(htmlspecialchars_decode($string), 'UTF-8', 'auto')
Loading history...
58
        );
59 1
        self::restoreLocale($oldLocale);
60 1
        return self::cleanString($string, $delimiter);
61
    }
62
    /**
63
     * Revert back to the old locale
64
     */
65 1
    protected static function restoreLocale($oldLocale)
66
    {
67 1
        if ((stripos($oldLocale, '=') > 0)) {
68 1
            parse_str(str_replace(';', '&', $oldLocale), $loc);
69 1
            $oldLocale = array_values($loc);
70
        }
71 1
        setlocale(LC_ALL, $oldLocale);
72
    }
73 1
    protected static function cleanString($string, $delimiter)
74
    {
75
        // replace non letter or non digits by -
76 1
        $string = preg_replace('#[^\pL\d]+#u', '-', $string);
77
        // Trim trailing -
78 1
        $string = trim($string, '-');
79 1
        $clean = preg_replace('~[^-\w]+~', '', $string);
80 1
        $clean = strtolower($clean);
81 1
        $clean = preg_replace('#[\/_|+ -]+#', $delimiter, $clean);
82 1
        $clean = trim($clean, $delimiter);
83 1
        return $clean;
84
    }
85
}
86