| 1 | <?php |
||
| 20 | abstract class IdentifierSuffixer |
||
| 21 | { |
||
| 22 | public const VALID_IDENTIFIER_FORMAT = '/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]+$/'; |
||
| 23 | public const DEFAULT_IDENTIFIER = 'g'; |
||
| 24 | |||
| 25 | final private function __construct() |
||
| 26 | { |
||
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Generates a valid unique identifier from the given name, |
||
| 31 | * with a suffix attached to it |
||
| 32 | */ |
||
| 33 | public static function getIdentifier(string $name) : string |
||
| 34 | 28 | { |
|
| 35 | /** @var string|null $salt */ |
||
| 36 | 28 | static $salt; |
|
| 37 | |||
| 38 | 28 | $salt = $salt ?? $salt = self::loadBaseHashSalt(); |
|
| 39 | 28 | $suffix = substr(sha1($name . $salt), 0, 5); |
|
| 40 | |||
| 41 | 28 | if (! preg_match(self::VALID_IDENTIFIER_FORMAT, $name)) { |
|
| 42 | 8 | return self::DEFAULT_IDENTIFIER . $suffix; |
|
| 43 | } |
||
| 44 | |||
| 45 | 20 | return $name . $suffix; |
|
| 46 | } |
||
| 47 | |||
| 48 | private static function loadBaseHashSalt() : string |
||
| 52 | } |
||
| 53 |