1 | <?php |
||
32 | final class Client |
||
33 | { |
||
34 | private $handler; |
||
35 | private $spaces = []; |
||
36 | |||
37 | 258 | public function __construct(Handler $handler) |
|
41 | |||
42 | public static function fromDefaults() : self |
||
49 | |||
50 | public static function fromOptions(array $options) : self |
||
51 | { |
||
52 | $connectionOptions = []; |
||
53 | |||
54 | if (isset($options['connect_timeout'])) { |
||
55 | $connectionOptions['connect_timeout'] = $options['connect_timeout']; |
||
56 | } |
||
57 | if (isset($options['socket_timeout'])) { |
||
58 | $connectionOptions['socket_timeout'] = $options['socket_timeout']; |
||
59 | } |
||
60 | if (isset($options['tcp_nodelay'])) { |
||
61 | $connectionOptions['tcp_nodelay'] = $options['tcp_nodelay']; |
||
62 | } |
||
63 | |||
64 | $handler = new DefaultHandler( |
||
65 | new StreamConnection($options['uri'] ?? StreamConnection::DEFAULT_URI, $connectionOptions), |
||
66 | new PurePacker() |
||
67 | ); |
||
68 | |||
69 | if (isset($options['username'])) { |
||
70 | $handler = new MiddlewareHandler( |
||
71 | new AuthMiddleware($options['username'], $options['password'] ?? ''), |
||
72 | $handler |
||
73 | ); |
||
74 | } |
||
75 | if (isset($options['max_retries'])) { |
||
76 | $handler = new MiddlewareHandler( |
||
77 | RetryMiddleware::linear($options['max_retries']), |
||
78 | $handler |
||
79 | ); |
||
80 | } |
||
81 | |||
82 | return new self($handler); |
||
83 | } |
||
84 | |||
85 | public static function fromDsn(string $dsn) : self |
||
99 | |||
100 | public function withMiddleware(Middleware $middleware, Middleware ...$middlewares) : self |
||
111 | |||
112 | 12 | public function getHandler() : Handler |
|
116 | |||
117 | 54 | public function ping() : void |
|
121 | |||
122 | 116 | public function getSpace(string $spaceName) : Space |
|
132 | |||
133 | 122 | public function getSpaceById(int $spaceId) : Space |
|
141 | |||
142 | 10 | public function call(string $funcName, ...$args) : array |
|
148 | |||
149 | 8 | public function executeQuery(string $sql, array $params = []) : SqlQueryResult |
|
150 | { |
||
151 | 8 | $request = new Execute($sql, $params); |
|
152 | 8 | $response = $this->handler->handle($request); |
|
153 | |||
154 | 8 | return new SqlQueryResult( |
|
155 | 8 | $response->getBodyField(IProto::DATA), |
|
156 | 8 | $response->getBodyField(IProto::METADATA) |
|
157 | ); |
||
158 | } |
||
159 | |||
160 | 8 | public function executeUpdate(string $sql, array $params = []) : int |
|
161 | { |
||
162 | 8 | $request = new Execute($sql, $params); |
|
163 | |||
164 | 8 | return $this->handler->handle($request)->getBodyField(IProto::SQL_INFO)[0]; |
|
165 | } |
||
166 | |||
167 | 58 | public function evaluate(string $expr, ...$args) : array |
|
173 | |||
174 | 4 | public function flushSpaces() : void |
|
178 | |||
179 | 116 | private function getSpaceIdByName(string $spaceName) : int |
|
190 | } |
||
191 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.