1 <?php
2
3 /**
4 * ArangoDB-PHP-Core client: Endpoint configuration
5 *
6 * Taken from the original ArangoDB-Client, in order to maintain easy migration. Thanks Jan ;)
7 *
8 * @author Frank Mayer
9 * @copyright Copyright 2013-2015, FRANKMAYER.NET, Athens, Greece
10 */
11 // todo 1 Frank Revisit Endpoints Not sure how to handle this in the core level...
12 namespace frankmayer\ArangoDbPhpCore\Config;
13
14 use frankmayer\ArangoDbPhpCore\ClientException;
15
16 /**
17 * Endpoint specification
18 *
19 * An endpoint contains the server location the client connects to
20 * the following endpoint types are currently supported (more to be added later):
21 * - tcp://host:port for tcp connections
22 * - unix://socket for UNIX sockets (provided the server supports this)
23 * - ssl://host:port for SSL connections (provided the server supports this)
24 *
25 * Note: SSL support is added in ArangoDB server 1.1
26 *
27 * @package frankmayer\ArangoDbPhpCore\Config
28 */
29 class Endpoint
30 {
31 /**
32 * Current endpoint value
33 *
34 * @var string
35 */
36 private $_value;
37
38 /**
39 * TCP endpoint type
40 */
41 const TYPE_TCP = 'tcp';
42
43 /**
44 * SSL endpoint type
45 */
46 const TYPE_SSL = 'ssl';
47
48 /**
49 * UNIX socket endpoint type
50 */
51 const TYPE_UNIX = 'unix';
52
53 /**
54 * Regexp for TCP endpoints
55 */
56 const REGEXP_TCP = '/^tcp:\/\/(.+?):(\d+)\/?$/';
57
58 /**
59 * Regexp for SSL endpoints
60 */
61 const REGEXP_SSL = '/^ssl:\/\/(.+?):(\d+)\/?$/';
62
63 /**
64 * Regexp for UNIX socket endpoints
65 */
66 const REGEXP_UNIX = '/^unix:\/\/(.+)$/';
67
68 /**
69 * Create a new endpoint
70 *
71 * @param string $value - endpoint specification
72 *
73 * @throws ClientException
74 * @return \frankmayer\ArangoDbPhpCore\Config\Endpoint
75 */
76 public function __construct($value)
77 {
78 if (!self::isValid($value)) {
79 throw new ClientException(sprintf("invalid endpoint specification '%s'", $value));
80 }
81
82 $this->_value = $value;
83 }
84
85 /**
86 * Return a string representation of the endpoint
87 *
88 * @return string - string representation of the endpoint
89 */
90 public function __toString()
91 {
92 return $this->_value;
93 }
94
95 /**
96 * Return the type of an endpoint
97 *
98 * @param string $value - endpoint specification value
99 *
100 * @return string - endpoint type
101 */
102 public static function getType($value)
103 {
104 if (preg_match(self::REGEXP_TCP, $value)) {
105 return self::TYPE_TCP;
106 }
107
108 if (preg_match(self::REGEXP_SSL, $value)) {
109 return self::TYPE_SSL;
110 }
111
112 if (preg_match(self::REGEXP_UNIX, $value)) {
113 return self::TYPE_UNIX;
114 }
115
116 return null;
117 }
118
119 /**
120 * Return the host name of an endpoint
121 *
122 * @param string $value - endpoint specification value
123 *
124 * @return string - host name
125 */
126 public static function getHost($value)
127 {
128 if (preg_match(self::REGEXP_TCP, $value, $matches)) {
129 return $matches[1];
130 }
131
132 if (preg_match(self::REGEXP_SSL, $value, $matches)) {
133 return $matches[1];
134 }
135
136 return null;
137 }
138
139 /**
140 * check whether an endpoint specification is valid
141 *
142 * @param string $value - endpoint specification value
143 *
144 * @return bool - true if endpoint specification is valid, false otherwise
145 */
146 public static function isValid($value)
147 {
148 if (!is_string($value)) {
149 return false;
150 }
151
152 $type = self::getType($value);
153 if ($type === null) {
154 return false;
155 }
156
157 return true;
158 }
159 }
160