Passed
Push — master ( ece3c1...a0e7b2 )
by Florian
02:27
created

IdentifierHelper   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
eloc 16
c 1
b 0
f 1
dl 0
loc 34
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B getHumanReadableIdentifier() 0 26 8
1
<?php
2
3
/*
4
 * This file is part of the TheAlternativeZurich/events project.
5
 *
6
 * (c) Florian Moser <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace App\Helper;
13
14
class IdentifierHelper
15
{
16
    /**
17
     * transforms text to human readable URL
18
     * only outputs lowercase alphanummeric string, invalid characters are replaced by -.
19
     *
20
     * min length 10, max length 100
21
     */
22
    public static function getHumanReadableIdentifier(string $text): string
23
    {
24
        $lowercase = strtolower($text);
25
26
        $result = '';
27
        for ($i = 0; $i < strlen($text); ++$i) {
28
            $character = $lowercase[$i];
29
            //0-9, a-z
30
            if (($character >= 48 && $character <= 57) ||
31
                ($character >= 97 && $character <= 122)) {
32
                $result .= $character;
33
            } else {
34
                $result .= '-';
35
            }
36
        }
37
38
        if (strlen($result) > 100) {
39
            $result = substr($result, 0, 100); // make max length
40
            $result = substr($result, 0, strrpos($result, '-')); // cut off last word
41
        }
42
43
        if (strlen($result) < 10) {
44
            $result .= RandomHelper::generateHumanReadableRandom(10, '-');
45
        }
46
47
        return $result;
48
    }
49
}
50