Total Complexity | 7 |
Total Lines | 42 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | <?php |
||
8 | class Gtid |
||
9 | { |
||
10 | private $intervals = []; |
||
11 | private $sid; |
||
12 | |||
13 | /** |
||
14 | * @throws GtidException |
||
15 | */ |
||
16 | 6 | public function __construct(string $gtid) |
|
17 | { |
||
18 | 6 | if (false === (bool)preg_match('/^([0-9a-fA-F]{8}(?:-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12})((?::[0-9-]+)+)$/', $gtid, $matches)) { |
|
19 | 1 | throw new GtidException(GtidException::INCORRECT_GTID_MESSAGE, GtidException::INCORRECT_GTID_CODE); |
|
20 | } |
||
21 | |||
22 | 5 | $this->sid = $matches[1]; |
|
23 | 5 | foreach (array_filter(explode(':', $matches[2])) as $k) { |
|
24 | 5 | $this->intervals[] = explode('-', $k); |
|
25 | } |
||
26 | 5 | $this->sid = str_replace('-', '', $this->sid); |
|
27 | 5 | } |
|
28 | |||
29 | 2 | public function getEncoded(): string |
|
30 | { |
||
31 | 2 | $buffer = pack('H*', $this->sid); |
|
32 | 2 | $buffer .= BinaryDataReader::pack64bit(count($this->intervals)); |
|
33 | |||
34 | 2 | foreach ($this->intervals as $interval) { |
|
35 | 2 | if (count($interval) !== 1) { |
|
36 | 2 | $buffer .= BinaryDataReader::pack64bit((int)$interval[0]); |
|
37 | 2 | $buffer .= BinaryDataReader::pack64bit((int)$interval[1]); |
|
38 | } else { |
||
39 | 2 | $buffer .= BinaryDataReader::pack64bit((int)$interval[0]); |
|
40 | 2 | $buffer .= BinaryDataReader::pack64bit($interval[0] + 1); |
|
41 | } |
||
42 | } |
||
43 | |||
44 | 2 | return $buffer; |
|
45 | } |
||
46 | |||
47 | 2 | public function getEncodedLength(): int |
|
50 | } |
||
51 | } |