UtilService::toAscii()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 1
dl 0
loc 13
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Copyright (c) 2017 Francois-Xavier Soubirou.
5
 *
6
 * This file is part of ci-report.
7
 *
8
 * ci-report is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * ci-report is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with ci-report. If not, see <http://www.gnu.org/licenses/>.
20
 */
21
declare(strict_types=1);
22
23
namespace App\Service;
24
25
/**
26
 * Util service class.
27
 *
28
 * @category  ci-report app
29
 *
30
 * @author    Francois-Xavier Soubirou <[email protected]>
31
 * @copyright 2017 Francois-Xavier Soubirou
32
 * @license   http://www.gnu.org/licenses/   GPLv3
33
 *
34
 * @see      https://www.ci-report.io
35
 */
36
class UtilService
37
{
38
    /**
39
     * Slugify a string.
40
     * Code from : http://ourcodeworld.com/articles/read/253/creating-url-slugs-properly-in-php-including-transliteration-support-for-utf-8.
41
     *
42
     * @param string $string String to convert
43
     *
44
     * @return string
45
     */
46
    public function toAscii(string $string): string
47
    {
48
        $clean1 = htmlentities($string, ENT_QUOTES, 'UTF-8');
49
        $clean2 = preg_replace(
50
            '~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i',
51
            '$1',
52
            $clean1
53
        );
54
        $clean3 = html_entity_decode($clean2, ENT_QUOTES, 'UTF-8');
55
        $clean4 = preg_replace('~[^0-9a-z]+~i', '-', $clean3);
56
        $clean5 = strtolower(trim($clean4, '-'));
57
58
        return $clean5;
59
    }
60
61
    /**
62
     * Generate a token.
63
     *
64
     * @return string
65
     */
66
    public function generateToken(): string
67
    {
68
        $part1 = bin2hex(random_bytes(6));
69
        $part2 = bin2hex(random_bytes(6));
70
        $part3 = bin2hex(random_bytes(6));
71
72
        return $part1.'-'.$part2.'-'.$part3;
73
    }
74
}
75