1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Sandro Keil (https://sandro-keil.de) |
4
|
|
|
* |
5
|
|
|
* @link http://github.com/sandrokeil/arangodb-php-client for the canonical source repository |
6
|
|
|
* @copyright Copyright (c) 2018-2019 Sandro Keil |
7
|
|
|
* @license http://github.com/sandrokeil/arangodb-php-client/blob/master/LICENSE.md New BSD License |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace ArangoDb; |
11
|
|
|
|
12
|
|
|
use ArangoDb\Exception\LogicException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Immutable client options array container |
16
|
|
|
*/ |
17
|
|
|
final class ClientOptions implements \ArrayAccess |
18
|
|
|
{ |
19
|
|
|
// connection options |
20
|
|
|
public const OPTION_ENDPOINT = 'endpoint'; |
21
|
|
|
public const OPTION_DATABASE = 'database'; |
22
|
|
|
public const OPTION_TIMEOUT = 'timeout'; |
23
|
|
|
public const OPTION_CONNECTION = 'Connection'; |
24
|
|
|
public const OPTION_RECONNECT = 'Reconnect'; |
25
|
|
|
|
26
|
|
|
// connection default options |
27
|
|
|
public const DEFAULT_CONNECTION = 'Keep-Alive'; |
28
|
|
|
public const DEFAULT_TIMEOUT = 30; |
29
|
|
|
|
30
|
|
|
// auth options |
31
|
|
|
public const OPTION_AUTH_USER = 'AuthUser'; |
32
|
|
|
public const OPTION_AUTH_PASSWD = 'AuthPasswd'; |
33
|
|
|
public const OPTION_AUTH_TYPE = 'AuthType'; |
34
|
|
|
|
35
|
|
|
// auth default options |
36
|
|
|
public const DEFAULT_AUTH_TYPE = 'Basic'; |
37
|
|
|
|
38
|
|
|
// ssl options |
39
|
|
|
public const OPTION_VERIFY_CERT = 'verifyCert'; |
40
|
|
|
public const OPTION_VERIFY_CERT_NAME = 'verifyCertName'; |
41
|
|
|
public const OPTION_ALLOW_SELF_SIGNED = 'allowSelfSigned'; |
42
|
|
|
public const OPTION_CIPHERS = 'ciphers'; |
43
|
|
|
|
44
|
|
|
// ssl default options |
45
|
|
|
public const DEFAULT_VERIFY_CERT = false; |
46
|
|
|
public const DEFAULT_VERIFY_CERT_NAME = false; |
47
|
|
|
public const DEFAULT_ALLOW_SELF_SIGNED = true; |
48
|
|
|
public const DEFAULT_CIPHERS = 'DEFAULT'; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* The current options |
52
|
|
|
* |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
private $options; |
56
|
|
|
|
57
|
|
|
public function __construct(array $options) |
58
|
|
|
{ |
59
|
|
|
$this->options = array_merge(self::getDefaults(), $options); |
60
|
|
|
$this->validate(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function toArray(): array |
64
|
|
|
{ |
65
|
|
|
return $this->options; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Returns supported authorization types |
70
|
|
|
* |
71
|
|
|
* @return string[] |
72
|
|
|
*/ |
73
|
|
|
private static function getSupportedAuthTypes(): array |
74
|
|
|
{ |
75
|
|
|
return ['Basic']; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Returns supported connection types |
80
|
|
|
* |
81
|
|
|
* @return string[] |
82
|
|
|
*/ |
83
|
|
|
private static function getSupportedConnectionTypes(): array |
84
|
|
|
{ |
85
|
|
|
return ['Close', 'Keep-Alive']; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
private static function getDefaults(): array |
89
|
|
|
{ |
90
|
|
|
return [ |
91
|
|
|
self::OPTION_ENDPOINT => '', |
92
|
|
|
self::OPTION_TIMEOUT => self::DEFAULT_TIMEOUT, |
93
|
|
|
self::OPTION_CONNECTION => self::DEFAULT_CONNECTION, |
94
|
|
|
self::OPTION_VERIFY_CERT => self::DEFAULT_VERIFY_CERT, |
95
|
|
|
self::OPTION_VERIFY_CERT_NAME => self::DEFAULT_VERIFY_CERT_NAME, |
96
|
|
|
self::OPTION_ALLOW_SELF_SIGNED => self::DEFAULT_ALLOW_SELF_SIGNED, |
97
|
|
|
self::OPTION_CIPHERS => self::DEFAULT_CIPHERS, |
98
|
|
|
self::OPTION_RECONNECT => false, |
99
|
|
|
self::OPTION_DATABASE => '_system', |
100
|
|
|
]; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
private function validate(): void |
104
|
|
|
{ |
105
|
|
|
$this->options[self::OPTION_ENDPOINT] = preg_replace( |
106
|
|
|
['/^http:/', '/^https:/'], |
107
|
|
|
['tcp:', 'ssl:'], |
108
|
|
|
$this->options[self::OPTION_ENDPOINT] |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
if (! isset($this->options[self::OPTION_ENDPOINT])) { |
112
|
|
|
throw new LogicException('Endpoint not specified'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if (isset($this->options[self::OPTION_AUTH_TYPE]) |
116
|
|
|
&& ! in_array($this->options[self::OPTION_AUTH_TYPE], self::getSupportedAuthTypes(), true) |
117
|
|
|
) { |
118
|
|
|
throw new LogicException('Unsupported authorization method: ' . $this->options[self::OPTION_AUTH_TYPE]); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if (isset($this->options[self::OPTION_CONNECTION]) |
122
|
|
|
&& ! in_array($this->options[self::OPTION_CONNECTION], self::getSupportedConnectionTypes(), true) |
123
|
|
|
) { |
124
|
|
|
throw new LogicException('Unsupported connection value: ' . $this->options[self::OPTION_CONNECTION]); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param mixed $offset |
130
|
|
|
* @param mixed $value |
131
|
|
|
*/ |
132
|
|
|
public function offsetSet($offset, $value): void |
133
|
|
|
{ |
134
|
|
|
throw new LogicException('Immutable object. Value for "' . $offset . '" not set.'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param mixed $offset |
139
|
|
|
* @return bool |
140
|
|
|
*/ |
141
|
|
|
public function offsetExists($offset): bool |
142
|
|
|
{ |
143
|
|
|
return isset($this->options[$offset]); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param mixed $offset |
148
|
|
|
*/ |
149
|
|
|
public function offsetUnset($offset): void |
150
|
|
|
{ |
151
|
|
|
throw new LogicException('Immutable object. Value for "' . $offset . '" not unset.'); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param mixed $offset |
156
|
|
|
* @return mixed |
157
|
|
|
*/ |
158
|
|
|
public function offsetGet($offset) |
159
|
|
|
{ |
160
|
|
|
if (! array_key_exists($offset, $this->options)) { |
161
|
|
|
throw new LogicException('Invalid option ' . $offset); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $this->options[$offset]; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|