1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* identity (https://github.com/phpgears/identity). |
5
|
|
|
* Identity objects for PHP. |
6
|
|
|
* |
7
|
|
|
* @license MIT |
8
|
|
|
* @link https://github.com/phpgears/identity |
9
|
|
|
* @author Julián Gutiérrez <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Gears\Identity; |
15
|
|
|
|
16
|
|
|
use Gears\Identity\Exception\InvalidIdentityException; |
17
|
|
|
use Ramsey\Uuid\Exception\InvalidUuidStringException; |
18
|
|
|
use Ramsey\Uuid\Uuid; |
19
|
|
|
use Tuupola\Base62; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Base62 UUID identity. |
23
|
|
|
*/ |
24
|
|
|
class Base62UuidIdentity extends AbstractIdentity |
25
|
|
|
{ |
26
|
|
|
private const UUID_HEX_LENGTH = 32; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
* |
31
|
|
|
* @throws InvalidIdentityException |
32
|
|
|
*/ |
33
|
|
|
final public static function fromString(string $value) |
34
|
|
|
{ |
35
|
|
|
$decoded = \bin2hex((new Base62())->decode($value)); |
36
|
|
|
if (\strlen($decoded) !== static::UUID_HEX_LENGTH) { |
37
|
|
|
throw new InvalidIdentityException( |
38
|
|
|
\sprintf('Provided identity value "%s" is not a valid bas62 UUID', $value) |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
try { |
43
|
|
|
$uuid = Uuid::fromString( |
44
|
|
|
\sprintf('%s%s-%s-%s-%s-%s%s%s', ...\str_split($decoded, 4)) |
45
|
|
|
); |
46
|
|
|
} catch (InvalidUuidStringException $exception) { |
47
|
|
|
throw new InvalidIdentityException( |
48
|
|
|
\sprintf('Provided identity value "%s" is not a valid bas62 UUID', $value), |
49
|
|
|
0, |
50
|
|
|
$exception |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if ($uuid->getVariant() !== Uuid::RFC_4122 || !\in_array($uuid->getVersion(), \range(1, 5), true)) { |
55
|
|
|
throw new InvalidIdentityException( |
56
|
|
|
\sprintf('Provided identity value "%s" is not a valid bas62 UUID', $value) |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return new static($value); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get identity from UUID string. |
65
|
|
|
* |
66
|
|
|
* @param string $value |
67
|
|
|
* |
68
|
|
|
* @return mixed|static |
|
|
|
|
69
|
|
|
*/ |
70
|
|
|
final public static function fromUuid(string $value) |
71
|
|
|
{ |
72
|
|
|
try { |
73
|
|
|
$uuid = Uuid::fromString($value); |
74
|
|
|
} catch (InvalidUuidStringException $exception) { |
75
|
|
|
throw new InvalidIdentityException( |
76
|
|
|
\sprintf('Provided identity value "%s" is not a valid UUID', $value), |
77
|
|
|
0, |
78
|
|
|
$exception |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if ($uuid->getVariant() !== Uuid::RFC_4122 || !\in_array($uuid->getVersion(), \range(1, 5), true)) { |
83
|
|
|
throw new InvalidIdentityException( |
84
|
|
|
\sprintf('Provided identity value "%s" is not a valid UUID', $value) |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** @var string $binaryUuid */ |
89
|
|
|
$binaryUuid = \hex2bin(\str_replace('-', '', $uuid->toString())); |
|
|
|
|
90
|
|
|
|
91
|
|
|
return new static((new Base62())->encode($binaryUuid)); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.