Conditions | 8 |
Paths | 12 |
Total Lines | 28 |
Code Lines | 16 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
1 | <?php |
||
22 | public static function getHumanReadableIdentifier(string $text): string |
||
23 | { |
||
24 | $lowercase = strtolower(preg_replace('/([a-z])([A-Z])/', '$1-$2', $text)); |
||
25 | |||
26 | $result = ''; |
||
27 | for ($i = 0; $i < strlen($lowercase); ++$i) { |
||
28 | $character = $lowercase[$i]; |
||
29 | $characterValue = ord($character); |
||
30 | |||
31 | //0-9, a-z |
||
32 | if (($characterValue >= 48 && $characterValue <= 57) || |
||
33 | ($characterValue >= 97 && $characterValue <= 122)) { |
||
34 | $result .= $character; |
||
35 | } else { |
||
36 | $result .= '-'; |
||
37 | } |
||
38 | } |
||
39 | |||
40 | if (strlen($result) > 100) { |
||
41 | $result = substr($result, 0, 100); // make max length |
||
42 | $result = substr($result, 0, strrpos($result, '-')); // cut off last word |
||
43 | } |
||
44 | |||
45 | if (strlen($result) < 10) { |
||
46 | $result .= RandomHelper::generateHumanReadableRandom(10, '-'); |
||
47 | } |
||
48 | |||
49 | return trim($result); |
||
50 | } |
||
52 |