1 <?php
2
3 4 5 6 7 8 9
10
11 namespace triagens\ArangoDb;
12
13 14 15 16 17 18 19 20 21 22 23
24 class ConnectionOptions implements
25 \ArrayAccess
26 {
27 28 29 30 31
32 private $_values = [];
33
34 35 36 37 38
39 private $_endpoint;
40
41 42 43
44 const OPTION_ENDPOINT = 'endpoint';
45
46 47 48
49 const OPTION_HOST = 'host';
50
51 52 53
54 const OPTION_PORT = 'port';
55
56 57 58
59 const OPTION_TIMEOUT = 'timeout';
60
61 62 63
64 const OPTION_TRACE = 'trace';
65
66 67 68
69 const OPTION_VERIFY_CERT = 'verifyCert';
70
71 72 73
74 const OPTION_ALLOW_SELF_SIGNED = 'allowSelfSigned';
75
76 77 78
79 const OPTION_CIPHERS = 'ciphers';
80
81 82 83
84 const OPTION_ENHANCED_TRACE = 'enhancedTrace';
85
86 87 88
89 const OPTION_CREATE = 'createCollection';
90
91 92 93
94 const OPTION_REVISION = 'rev';
95
96 97 98
99 const OPTION_UPDATE_POLICY = 'policy';
100
101 102 103
104 const OPTION_UPDATE_KEEPNULL = 'keepNull';
105
106 107 108
109 const OPTION_REPLACE_POLICY = 'policy';
110
111 112 113
114 const OPTION_DELETE_POLICY = 'policy';
115
116 117 118
119 const OPTION_WAIT_SYNC = 'waitForSync';
120
121 122 123
124 const OPTION_LIMIT = 'limit';
125
126 127 128
129 const OPTION_SKIP = 'skip';
130
131 132 133
134 const OPTION_BATCHSIZE = 'batchSize';
135
136 137 138
139 const OPTION_JOURNAL_SIZE = 'journalSize';
140
141 142 143
144 const OPTION_IS_SYSTEM = 'isSystem';
145
146 147 148
149 const OPTION_IS_VOLATILE = 'isVolatile';
150
151 152 153
154 const OPTION_AUTH_USER = 'AuthUser';
155
156 157 158
159 const OPTION_AUTH_PASSWD = 'AuthPasswd';
160
161 162 163
164 const OPTION_AUTH_TYPE = 'AuthType';
165
166 167 168
169 const OPTION_CONNECTION = 'Connection';
170
171 172 173
174 const OPTION_RECONNECT = 'Reconnect';
175
176 177 178
179 const OPTION_BATCH = 'Batch';
180
181 182 183
184 const OPTION_BATCHPART = 'BatchPart';
185
186 187 188
189 const OPTION_DATABASE = 'database';
190
191 192 193
194 const OPTION_CHECK_UTF8_CONFORM = 'CheckUtf8Conform';
195
196 197 198 199 200 201 202 203
204 public function __construct(array $options)
205 {
206 $this->_values = array_merge(self::getDefaults(), $options);
207 $this->validate();
208 }
209
210 211 212 213 214
215 public function getAll()
216 {
217 return $this->_values;
218 }
219
220 221 222 223 224 225 226 227 228 229
230 public function offsetSet($offset, $value)
231 {
232 $this->_values[$offset] = $value;
233 $this->validate();
234 }
235
236 237 238 239 240 241 242
243 public function offsetExists($offset)
244 {
245 return isset($this->_values[$offset]);
246 }
247
248 249 250 251 252 253 254 255 256
257 public function offsetUnset($offset)
258 {
259 unset($this->_values[$offset]);
260 $this->validate();
261 }
262
263 264 265 266 267 268 269 270 271
272 public function offsetGet($offset)
273 {
274 if (!array_key_exists($offset, $this->_values)) {
275 throw new ClientException('Invalid option ' . $offset);
276 }
277
278 return $this->_values[$offset];
279 }
280
281 282 283 284 285 286
287 public function getEndpoint()
288 {
289 if ($this->_endpoint === null) {
290
291 $this->_endpoint = new Endpoint($this->_values[self::OPTION_ENDPOINT]);
292 }
293
294 return $this->_endpoint;
295 }
296
297 298 299 300 301
302 private static function getDefaults()
303 {
304 return [
305 self::OPTION_ENDPOINT => null,
306 self::OPTION_HOST => null,
307 self::OPTION_PORT => DefaultValues::DEFAULT_PORT,
308 self::OPTION_TIMEOUT => DefaultValues::DEFAULT_TIMEOUT,
309 self::OPTION_CREATE => DefaultValues::DEFAULT_CREATE,
310 self::OPTION_UPDATE_POLICY => DefaultValues::DEFAULT_UPDATE_POLICY,
311 self::OPTION_REPLACE_POLICY => DefaultValues::DEFAULT_REPLACE_POLICY,
312 self::OPTION_DELETE_POLICY => DefaultValues::DEFAULT_DELETE_POLICY,
313 self::OPTION_REVISION => null,
314 self::OPTION_WAIT_SYNC => DefaultValues::DEFAULT_WAIT_SYNC,
315 self::OPTION_BATCHSIZE => null,
316 self::OPTION_JOURNAL_SIZE => DefaultValues::DEFAULT_JOURNAL_SIZE,
317 self::OPTION_IS_SYSTEM => false,
318 self::OPTION_IS_VOLATILE => DefaultValues::DEFAULT_IS_VOLATILE,
319 self::OPTION_CONNECTION => DefaultValues::DEFAULT_CONNECTION,
320 self::OPTION_TRACE => null,
321 self::OPTION_ENHANCED_TRACE => false,
322 self::OPTION_VERIFY_CERT => DefaultValues::DEFAULT_VERIFY_CERT,
323 self::OPTION_ALLOW_SELF_SIGNED => DefaultValues::DEFAULT_ALLOW_SELF_SIGNED,
324 self::OPTION_CIPHERS => DefaultValues::DEFAULT_CIPHERS,
325 self::OPTION_AUTH_USER => null,
326 self::OPTION_AUTH_PASSWD => null,
327 self::OPTION_AUTH_TYPE => null,
328 self::OPTION_RECONNECT => false,
329 self::OPTION_BATCH => false,
330 self::OPTION_BATCHPART => false,
331 self::OPTION_DATABASE => '_system',
332 self::OPTION_CHECK_UTF8_CONFORM => DefaultValues::DEFAULT_CHECK_UTF8_CONFORM
333 ];
334 }
335
336 337 338 339 340
341 private static function getSupportedAuthTypes()
342 {
343 return ['Basic'];
344 }
345
346 347 348 349 350
351 private static function getSupportedConnectionTypes()
352 {
353 return ['Close', 'Keep-Alive'];
354 }
355
356 357 358 359 360 361
362 private function validate()
363 {
364 if (isset($this->_values[self::OPTION_HOST]) && !is_string($this->_values[self::OPTION_HOST])) {
365 throw new ClientException('host should be a string');
366 }
367
368 if (isset($this->_values[self::OPTION_PORT]) && !is_int($this->_values[self::OPTION_PORT])) {
369 throw new ClientException('port should be an integer');
370 }
371
372
373 if (isset($this->_values[self::OPTION_HOST], $this->_values[self::OPTION_ENDPOINT])) {
374 throw new ClientException('must not specify both host and endpoint');
375 } else {
376 if (isset($this->_values[self::OPTION_HOST]) && !isset($this->_values[self::OPTION_ENDPOINT])) {
377
378 $this->_values[self::OPTION_ENDPOINT] = 'tcp://' . $this->_values[self::OPTION_HOST] . ':' . $this->_values[self::OPTION_PORT];
379 }
380 }
381
382
383 $this->getEndpoint();
384
385 $type = Endpoint::getType($this->_values[self::OPTION_ENDPOINT]);
386 if ($type === Endpoint::TYPE_UNIX) {
387
388 $this->_values[self::OPTION_PORT] = 0;
389 } elseif ($type === Endpoint::TYPE_SSL) {
390
391 $this->_values[self::OPTION_PORT] = 0;
392 }
393
394 if (isset($this->_values[self::OPTION_AUTH_TYPE]) && !in_array(
395 $this->_values[self::OPTION_AUTH_TYPE],
396 self::getSupportedAuthTypes(), true
397 )
398 ) {
399 throw new ClientException('unsupported authorization method');
400 }
401
402 if (isset($this->_values[self::OPTION_CONNECTION]) && !in_array(
403 $this->_values[self::OPTION_CONNECTION],
404 self::getSupportedConnectionTypes(), true
405 )
406 ) {
407 throw new ClientException(
408 sprintf(
409 "unsupported connection value '%s'",
410 $this->_values[self::OPTION_CONNECTION]
411 )
412 );
413 }
414
415 UpdatePolicy::validate($this->_values[self::OPTION_UPDATE_POLICY]);
416 UpdatePolicy::validate($this->_values[self::OPTION_REPLACE_POLICY]);
417 UpdatePolicy::validate($this->_values[self::OPTION_DELETE_POLICY]);
418 }
419 }
420