1 | <?php |
||
32 | final class Client |
||
33 | { |
||
34 | private $connection; |
||
35 | private $packer; |
||
36 | private $salt; |
||
37 | private $username; |
||
38 | private $password; |
||
39 | private $spaces = []; |
||
40 | |||
41 | 129 | public function __construct(Connection $connection, Packer $packer) |
|
42 | { |
||
43 | 129 | $this->connection = $connection; |
|
44 | 129 | $this->packer = $packer; |
|
45 | 129 | } |
|
46 | |||
47 | 2 | public function getConnection() : Connection |
|
48 | { |
||
49 | 2 | return $this->connection; |
|
50 | } |
||
51 | |||
52 | 2 | public function getPacker() : Packer |
|
56 | |||
57 | 123 | public function connect() : void |
|
58 | { |
||
59 | 123 | $this->salt = $this->connection->open(); |
|
60 | |||
61 | 102 | if ($this->username) { |
|
62 | 2 | $this->authenticate($this->username, $this->password); |
|
63 | } |
||
64 | 102 | } |
|
65 | |||
66 | 2 | public function disconnect() : void |
|
67 | { |
||
68 | 2 | $this->connection->close(); |
|
69 | 2 | $this->salt = null; |
|
70 | 2 | } |
|
71 | |||
72 | 6 | public function isDisconnected() : bool |
|
73 | { |
||
74 | 6 | return $this->connection->isClosed() || !$this->salt; |
|
|
|||
75 | } |
||
76 | |||
77 | 6 | public function authenticate(string $username, string $password = '') : BinaryResponse |
|
78 | { |
||
79 | 6 | if ($this->isDisconnected()) { |
|
80 | 4 | $this->salt = $this->connection->open(); |
|
81 | } |
||
82 | |||
83 | 6 | $request = new AuthenticateRequest($this->salt, $username, $password); |
|
84 | 6 | $rawResponse = $this->sendRequest($request); |
|
85 | |||
86 | 3 | $this->username = $username; |
|
87 | 3 | $this->password = $password; |
|
88 | |||
89 | 3 | $this->flushSpaces(); |
|
90 | |||
91 | 3 | return BinaryResponse::createFromRaw($rawResponse); |
|
92 | } |
||
93 | |||
94 | 6 | public function ping() : BinaryResponse |
|
100 | |||
101 | 60 | public function getSpace(string $spaceName) : Space |
|
102 | { |
||
103 | 60 | if (isset($this->spaces[$spaceName])) { |
|
104 | 3 | return $this->spaces[$spaceName]; |
|
105 | } |
||
106 | |||
107 | 60 | $spaceId = $this->getSpaceIdByName($spaceName); |
|
108 | |||
109 | 58 | return $this->spaces[$spaceName] = $this->spaces[$spaceId] = new Space($this, $spaceId); |
|
110 | } |
||
111 | |||
112 | 63 | public function getSpaceById(int $spaceId) : Space |
|
113 | { |
||
114 | 63 | if (isset($this->spaces[$spaceId])) { |
|
115 | 2 | return $this->spaces[$spaceId]; |
|
116 | } |
||
117 | |||
118 | 63 | return $this->spaces[$spaceId] = new Space($this, $spaceId); |
|
119 | } |
||
120 | |||
121 | 5 | public function call(string $funcName, ...$args) : BinaryResponse |
|
127 | |||
128 | 4 | public function executeQuery(string $sql, array $params = []) : SqlQueryResponse |
|
134 | |||
135 | 4 | public function executeUpdate(string $sql, array $params = []) : SqlUpdateResponse |
|
141 | |||
142 | 27 | public function evaluate(string $expr, array $args = []) : BinaryResponse |
|
143 | { |
||
144 | 27 | $request = new EvaluateRequest($expr, $args); |
|
145 | |||
146 | 27 | return BinaryResponse::createFromRaw($this->sendRequest($request)); |
|
147 | } |
||
148 | |||
149 | 5 | public function flushSpaces() : void |
|
150 | { |
||
153 | |||
154 | 106 | public function sendRequest(Request $request) : RawResponse |
|
173 | |||
174 | 60 | private function getSpaceIdByName(string $spaceName) : int |
|
186 | } |
||
187 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: