-
-
Notifications
You must be signed in to change notification settings - Fork 189
/
UniqueIdentifierGenerator.php
42 lines (37 loc) · 1009 Bytes
/
UniqueIdentifierGenerator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
declare(strict_types=1);
namespace ProxyManager\Generator\Util;
use function preg_match;
use function str_replace;
use function uniqid;
/**
* Utility class capable of generating unique
* valid class/property/method identifiers
*/
abstract class UniqueIdentifierGenerator
{
public const VALID_IDENTIFIER_FORMAT = '/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]+$/';
public const DEFAULT_IDENTIFIER = 'g';
/**
* Generates a valid unique identifier from the given name
*
* @psalm-return class-string
* @psalm-suppress MoreSpecificReturnType
*/
public static function getIdentifier(string $name) : string
{
/**
* @psalm-suppress LessSpecificReturnStatement
*/
return str_replace(
'.',
'',
uniqid(
preg_match(self::VALID_IDENTIFIER_FORMAT, $name)
? $name
: self::DEFAULT_IDENTIFIER,
true
)
);
}
}