ArangoDB-PHP API Documentation
  • Namespace
  • Class
  • Deprecated

Namespaces

  • triagens
    • ArangoDb

Classes

  • triagens\ArangoDb\AdminHandler
  • triagens\ArangoDb\AqlUserFunction
  • triagens\ArangoDb\Autoloader
  • triagens\ArangoDb\Batch
  • triagens\ArangoDb\BatchPart
  • triagens\ArangoDb\BindVars
  • triagens\ArangoDb\Collection
  • triagens\ArangoDb\CollectionHandler
  • triagens\ArangoDb\Connection
  • triagens\ArangoDb\ConnectionOptions
  • triagens\ArangoDb\Cursor
  • triagens\ArangoDb\Database
  • triagens\ArangoDb\DefaultValues
  • triagens\ArangoDb\Document
  • triagens\ArangoDb\DocumentHandler
  • triagens\ArangoDb\Edge
  • triagens\ArangoDb\EdgeDefinition
  • triagens\ArangoDb\EdgeHandler
  • triagens\ArangoDb\Endpoint
  • triagens\ArangoDb\Export
  • triagens\ArangoDb\ExportCursor
  • triagens\ArangoDb\Graph
  • triagens\ArangoDb\GraphHandler
  • triagens\ArangoDb\Handler
  • triagens\ArangoDb\HttpHelper
  • triagens\ArangoDb\HttpResponse
  • triagens\ArangoDb\QueryCacheHandler
  • triagens\ArangoDb\QueryHandler
  • triagens\ArangoDb\Statement
  • triagens\ArangoDb\TraceRequest
  • triagens\ArangoDb\TraceResponse
  • triagens\ArangoDb\Transaction
  • triagens\ArangoDb\Traversal
  • triagens\ArangoDb\UpdatePolicy
  • triagens\ArangoDb\UrlHelper
  • triagens\ArangoDb\Urls
  • triagens\ArangoDb\User
  • triagens\ArangoDb\UserHandler
  • triagens\ArangoDb\ValueValidator
  • triagens\ArangoDb\Vertex
  • triagens\ArangoDb\VertexHandler

Exceptions

  • triagens\ArangoDb\ClientException
  • triagens\ArangoDb\ConnectException
  • triagens\ArangoDb\Exception
  • triagens\ArangoDb\ServerException
  1 <?php
  2 
  3 /**
  4  * ArangoDB PHP client: connection options
  5  *
  6  * @package   triagens\ArangoDb
  7  * @author    Jan Steemann
  8  * @copyright Copyright 2012, triagens GmbH, Cologne, Germany
  9  */
 10 
 11 namespace triagens\ArangoDb;
 12 
 13 /**
 14  * Simple container class for connection options.
 15  *
 16  * This class also provides the default values for the connection
 17  * options and will perform a simple validation of them.<br>
 18  * It provides array access to its members.<br>
 19  * <br>
 20  *
 21  * @package   triagens\ArangoDb
 22  * @since     0.2
 23  */
 24 class ConnectionOptions implements
 25     \ArrayAccess
 26 {
 27     /**
 28      * The current options
 29      *
 30      * @var array
 31      */
 32     private $_values = [];
 33 
 34     /**
 35      * The connection endpoint object
 36      *
 37      * @var Endpoint
 38      */
 39     private $_endpoint;
 40 
 41     /**
 42      * Endpoint string index constant
 43      */
 44     const OPTION_ENDPOINT = 'endpoint';
 45 
 46     /**
 47      * Host name string index constant (deprecated, use endpoint instead)
 48      */
 49     const OPTION_HOST = 'host';
 50 
 51     /**
 52      * Port number index constant (deprecated, use endpoint instead)
 53      */
 54     const OPTION_PORT = 'port';
 55 
 56     /**
 57      * Timeout value index constant
 58      */
 59     const OPTION_TIMEOUT = 'timeout';
 60 
 61     /**
 62      * Trace function index constant
 63      */
 64     const OPTION_TRACE = 'trace';
 65 
 66     /**
 67      * "verify certificates" index constant
 68      */
 69     const OPTION_VERIFY_CERT = 'verifyCert';
 70 
 71     /**
 72      * "allow self-signed" index constant
 73      */
 74     const OPTION_ALLOW_SELF_SIGNED = 'allowSelfSigned';
 75 
 76     /**
 77      * ciphers allowed to be used in SSL
 78      */
 79     const OPTION_CIPHERS = 'ciphers';
 80 
 81     /**
 82      * Enhanced trace
 83      */
 84     const OPTION_ENHANCED_TRACE = 'enhancedTrace';
 85 
 86     /**
 87      * "Create collections if they don't exist" index constant
 88      */
 89     const OPTION_CREATE = 'createCollection';
 90 
 91     /**
 92      * Update revision constant
 93      */
 94     const OPTION_REVISION = 'rev';
 95 
 96     /**
 97      * Update policy index constant
 98      */
 99     const OPTION_UPDATE_POLICY = 'policy';
100 
101     /**
102      * Update keepNull constant
103      */
104     const OPTION_UPDATE_KEEPNULL = 'keepNull';
105 
106     /**
107      * Replace policy index constant
108      */
109     const OPTION_REPLACE_POLICY = 'policy';
110 
111     /**
112      * Delete policy index constant
113      */
114     const OPTION_DELETE_POLICY = 'policy';
115 
116     /**
117      * Wait for sync index constant
118      */
119     const OPTION_WAIT_SYNC = 'waitForSync';
120 
121     /**
122      * Limit index constant
123      */
124     const OPTION_LIMIT = 'limit';
125 
126     /**
127      * Skip index constant
128      */
129     const OPTION_SKIP = 'skip';
130 
131     /**
132      * Batch size index constant
133      */
134     const OPTION_BATCHSIZE = 'batchSize';
135 
136     /**
137      * Wait for sync index constant
138      */
139     const OPTION_JOURNAL_SIZE = 'journalSize';
140 
141     /**
142      * Wait for sync index constant
143      */
144     const OPTION_IS_SYSTEM = 'isSystem';
145 
146     /**
147      * Wait for sync index constant
148      */
149     const OPTION_IS_VOLATILE = 'isVolatile';
150 
151     /**
152      * Authentication user name
153      */
154     const OPTION_AUTH_USER = 'AuthUser';
155 
156     /**
157      * Authentication password
158      */
159     const OPTION_AUTH_PASSWD = 'AuthPasswd';
160 
161     /**
162      * Authentication type
163      */
164     const OPTION_AUTH_TYPE = 'AuthType';
165 
166     /**
167      * Connection
168      */
169     const OPTION_CONNECTION = 'Connection';
170 
171     /**
172      * Reconnect flag
173      */
174     const OPTION_RECONNECT = 'Reconnect';
175 
176     /**
177      * Batch flag
178      */
179     const OPTION_BATCH = 'Batch';
180 
181     /**
182      * Batchpart flag
183      */
184     const OPTION_BATCHPART = 'BatchPart';
185 
186     /**
187      * Database flag
188      */
189     const OPTION_DATABASE = 'database';
190 
191     /**
192      * UTF-8 CHeck Flag
193      */
194     const OPTION_CHECK_UTF8_CONFORM = 'CheckUtf8Conform';
195 
196     /**
197      * Set defaults, use options provided by client and validate them
198      *
199      *
200      * @param array $options - initial options
201      *
202      * @throws \triagens\ArangoDb\ClientException
203      */
204     public function __construct(array $options)
205     {
206         $this->_values = array_merge(self::getDefaults(), $options);
207         $this->validate();
208     }
209 
210     /**
211      * Get all options
212      *
213      * @return array - all options as an array
214      */
215     public function getAll()
216     {
217         return $this->_values;
218     }
219 
220     /**
221      * Set and validate a specific option, necessary for ArrayAccess
222      *
223      * @throws Exception
224      *
225      * @param string $offset - name of option
226      * @param mixed  $value  - value for option
227      *
228      * @return void
229      */
230     public function offsetSet($offset, $value)
231     {
232         $this->_values[$offset] = $value;
233         $this->validate();
234     }
235 
236     /**
237      * Check whether an option exists, necessary for ArrayAccess
238      *
239      * @param string $offset -name of option
240      *
241      * @return bool - true if option exists, false otherwise
242      */
243     public function offsetExists($offset)
244     {
245         return isset($this->_values[$offset]);
246     }
247 
248     /**
249      * Remove an option and validate, necessary for ArrayAccess
250      *
251      * @throws Exception
252      *
253      * @param string $offset - name of option
254      *
255      * @return void
256      */
257     public function offsetUnset($offset)
258     {
259         unset($this->_values[$offset]);
260         $this->validate();
261     }
262 
263     /**
264      * Get a specific option, necessary for ArrayAccess
265      *
266      * @throws ClientException
267      *
268      * @param string $offset - name of option
269      *
270      * @return mixed - value of option, will throw if option is not set
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      * Get the endpoint object for the connection
283      *
284      * @throws ClientException
285      * @return Endpoint - endpoint object
286      */
287     public function getEndpoint()
288     {
289         if ($this->_endpoint === null) {
290             // will also validate the endpoint
291             $this->_endpoint = new Endpoint($this->_values[self::OPTION_ENDPOINT]);
292         }
293 
294         return $this->_endpoint;
295     }
296 
297     /**
298      * Get the default values for the options
299      *
300      * @return array - array of default connection options
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      * Return the supported authorization types
338      *
339      * @return array - array with supported authorization types
340      */
341     private static function getSupportedAuthTypes()
342     {
343         return ['Basic'];
344     }
345 
346     /**
347      * Return the supported connection types
348      *
349      * @return array - array with supported connection types
350      */
351     private static function getSupportedConnectionTypes()
352     {
353         return ['Close', 'Keep-Alive'];
354     }
355 
356     /**
357      * Validate the options
358      *
359      * @throws ClientException
360      * @return void - will throw if an invalid option value is found
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         // can use either endpoint or host/port
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                 // upgrade host/port to an endpoint
378                 $this->_values[self::OPTION_ENDPOINT] = 'tcp://' . $this->_values[self::OPTION_HOST] . ':' . $this->_values[self::OPTION_PORT];
379             }
380         }
381 
382         // set up a new endpoint, this will also validate it
383         $this->getEndpoint();
384 
385         $type = Endpoint::getType($this->_values[self::OPTION_ENDPOINT]);
386         if ($type === Endpoint::TYPE_UNIX) {
387             // must set port to 0 for UNIX domain sockets
388             $this->_values[self::OPTION_PORT] = 0;
389         } elseif ($type === Endpoint::TYPE_SSL) {
390             // must set port to 0 for SSL connections
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 
ArangoDB-PHP API Documentation API documentation generated by ApiGen