1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phossa Project |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category Library |
8
|
|
|
* @package Phossa2\Uuid |
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
10
|
|
|
* @license http://mit-license.org/ MIT License |
11
|
|
|
* @link http://www.phossa.com/ |
12
|
|
|
*/ |
13
|
|
|
/*# declare(strict_types=1); */ |
14
|
|
|
|
15
|
|
|
namespace Phossa2\Uuid\Traits; |
16
|
|
|
|
17
|
|
|
use Phossa2\Uuid\Exception\LogicException; |
18
|
|
|
use Phossa2\Uuid\Interfaces\UtilityInterface; |
19
|
|
|
use Phossa2\Uuid\Message\Message; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* UtilityTrait |
23
|
|
|
* |
24
|
|
|
* @package Phossa2\Uuid |
25
|
|
|
* @author Hong Zhang <[email protected]> |
26
|
|
|
* @see UtilityInterface |
27
|
|
|
* @version 2.0.0 |
28
|
|
|
* @since 2.0.0 added |
29
|
|
|
*/ |
30
|
|
|
trait UtilityTrait |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* GMP supported ? |
34
|
|
|
* @var bool |
35
|
|
|
* @access protected |
36
|
|
|
*/ |
37
|
|
|
protected static $gmp; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritDoc} |
41
|
|
|
*/ |
42
|
|
|
public static function isValid(/*# string */ $uuid)/*# : bool */ |
43
|
|
|
{ |
44
|
|
|
$pattern = '~^' . substr(self::VERSION, 0, 1) . '[0-9a-f]{31}$~'; |
45
|
|
|
return is_string($uuid) && (bool) preg_match($pattern, $uuid); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritDoc} |
50
|
|
|
*/ |
51
|
|
|
public static function info(/*# string */ $uuid)/*# : array */ |
52
|
|
|
{ |
53
|
|
|
if (static::isValid($uuid)) { |
54
|
|
|
return [ |
55
|
|
|
'version' => substr($uuid, 0, 1), |
56
|
|
|
'type' => substr($uuid, 1, 4), |
57
|
|
|
'shard' => substr($uuid, 20, 4), |
58
|
|
|
'vendor' => substr($uuid, 24, 4), |
59
|
|
|
'remain' => substr($uuid, 28, 4) |
60
|
|
|
]; |
61
|
|
|
} else { |
62
|
|
|
throw new LogicException( |
63
|
|
|
Message::get(Message::UUID_INVALID, $uuid), |
64
|
|
|
Message::UUID_INVALID |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritDoc} |
71
|
|
|
*/ |
72
|
|
View Code Duplication |
public static function encode( |
73
|
|
|
/*# string */ $uuid, |
74
|
|
|
/*# string */ $base = self::BASE56 |
75
|
|
|
)/*# : string */ { |
76
|
|
|
if (static::isValid($uuid)) { |
77
|
|
|
return static::convertBase($uuid, self::BASE16, $base); |
78
|
|
|
} |
79
|
|
|
throw new LogicException( |
80
|
|
|
Message::get(Message::UUID_INVALID, $uuid), |
81
|
|
|
Message::UUID_INVALID |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritDoc} |
87
|
|
|
*/ |
88
|
|
View Code Duplication |
public static function decode( |
|
|
|
|
89
|
|
|
/*# string */ $string, |
90
|
|
|
/*# string */ $base = self::BASE56 |
91
|
|
|
)/*# : string */ { |
92
|
|
|
$uuid = static::convertBase($string, $base, self::BASE16); |
93
|
|
|
if (static::isValid($uuid)) { |
94
|
|
|
return $uuid; |
95
|
|
|
} |
96
|
|
|
throw new LogicException( |
97
|
|
|
Message::get(Message::UUID_DECODE_FAIL, $string), |
98
|
|
|
Message::UUID_DECODE_FAIL |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Convert numerical string between bases |
104
|
|
|
* |
105
|
|
|
* @param string $input |
106
|
|
|
* @param string $fromBase |
107
|
|
|
* @param string $toBase |
108
|
|
|
* @return string |
109
|
|
|
* @access protected |
110
|
|
|
* @static |
111
|
|
|
*/ |
112
|
|
|
protected static function convertBase( |
113
|
|
|
/*# string */ $input, |
114
|
|
|
/*# string */ $fromBase, |
115
|
|
|
/*# string */ $toBase |
116
|
|
|
)/*# : string */ { |
117
|
|
|
if ($fromBase === $toBase) { |
118
|
|
|
return $input; |
119
|
|
|
} elseif ($fromBase === self::BASE10) { |
120
|
|
|
return static::fromBase10($input, $toBase); |
121
|
|
|
} elseif ($toBase === self::BASE10) { |
122
|
|
|
return static::toBase10($input, $fromBase); |
123
|
|
|
} else { |
124
|
|
|
return static::fromBase10(static::toBase10($input, $fromBase), $toBase); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Convert to decimal string |
130
|
|
|
* |
131
|
|
|
* @param string $input |
132
|
|
|
* @param string $fromBase |
133
|
|
|
* @return string |
134
|
|
|
* @access protected |
135
|
|
|
*/ |
136
|
|
|
protected static function toBase10( |
137
|
|
|
/*# string */ $input, |
138
|
|
|
/*# string */ $fromBase |
139
|
|
|
)/*# string */ { |
140
|
|
|
$len = strlen($fromBase); |
141
|
|
|
$res = '0'; |
142
|
|
|
foreach (str_split($input) as $char) { |
143
|
|
|
$res = bcadd((int) strpos($fromBase, $char), bcmul($res, $len)); |
144
|
|
|
} |
145
|
|
|
return $res; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Convert from decimal string |
150
|
|
|
* |
151
|
|
|
* @param string $input |
152
|
|
|
* @param string $toBase |
153
|
|
|
* @return string |
154
|
|
|
* @access protected |
155
|
|
|
*/ |
156
|
|
|
protected static function fromBase10( |
157
|
|
|
/*# string */ $input, |
158
|
|
|
/*# string */ $toBase |
159
|
|
|
)/*# string */ { |
160
|
|
|
$len = strlen($toBase); |
161
|
|
|
$res = ''; |
162
|
|
|
do { |
163
|
|
|
$digit = bcmod($input, $len); |
164
|
|
|
$res = $toBase[(int) $digit] . $res; |
165
|
|
|
$input = bcdiv($input, $len, 0); |
166
|
|
|
} while($input != '0'); |
167
|
|
|
return $res; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.