1 | <?php |
||
18 | final class Greeting |
||
19 | { |
||
20 | public const SIZE_BYTES = 128; |
||
21 | |||
22 | /** @var string */ |
||
23 | private $greeting; |
||
24 | |||
25 | /** @var string|null */ |
||
26 | private $salt; |
||
27 | |||
28 | /** @var string|null */ |
||
29 | private $serverVersion; |
||
30 | |||
31 | /** @var bool */ |
||
32 | private $unknown = false; |
||
33 | |||
34 | /** |
||
35 | * @param string $greeting |
||
36 | */ |
||
37 | 33 | private function __construct($greeting) |
|
41 | |||
42 | 33 | public static function parse(string $greeting) : self |
|
43 | { |
||
44 | 33 | if (0 === \strpos($greeting, 'Tarantool')) { |
|
45 | 33 | return new self($greeting); |
|
46 | } |
||
47 | |||
48 | throw new UnexpectedResponse('Unable to recognize Tarantool server'); |
||
49 | } |
||
50 | |||
51 | public static function unknown() : self |
||
52 | { |
||
53 | $self = new self(''); |
||
54 | $self->unknown = true; |
||
55 | |||
56 | return $self; |
||
57 | } |
||
58 | |||
59 | 3 | public function getSalt() : string |
|
60 | { |
||
61 | 3 | if (null !== $this->salt) { |
|
62 | return $this->salt; |
||
63 | } |
||
64 | |||
65 | 3 | if ($this->unknown) { |
|
66 | throw new \BadMethodCallException('Salt is unknown for persistent connections'); |
||
67 | } |
||
68 | |||
69 | 3 | if (false === $salt = \base64_decode(\substr($this->greeting, 64, 44), true)) { |
|
70 | throw new UnexpectedResponse('Unable to decode salt'); |
||
71 | } |
||
72 | |||
73 | 3 | $salt = \substr($salt, 0, 20); |
|
74 | |||
75 | 3 | if (isset($salt[19])) { |
|
76 | 3 | return $this->salt = $salt; |
|
77 | } |
||
78 | |||
79 | throw new UnexpectedResponse('Salt is too short'); |
||
80 | } |
||
81 | |||
82 | 30 | public function getServerVersion() : string |
|
94 | |||
95 | public function equals(?self $greeting) : bool |
||
96 | { |
||
97 | if (!$greeting || $greeting->unknown) { |
||
107 | } |
||
108 |