1 <?php
2
3 /**
4 * ArangoDB PHP client: endpoint
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 * Endpoint specification
15 *
16 * An endpoint contains the server location the client connects to
17 * the following endpoint types are currently supported (more to be added later):
18 * <ul>
19 * <li> tcp://host:port for tcp connections
20 * <li> unix://socket for UNIX sockets (provided the server supports this)
21 * <li> ssl://host:port for SSL connections (provided the server supports this)
22 * </ul>
23 *
24 * Note: SSL support is added in ArangoDB server 1.1<br>
25 *
26 * <br>
27 *
28 * @package triagens\ArangoDb
29 * @since 0.2
30 */
31 class Endpoint
32 {
33 /**
34 * Current endpoint value
35 *
36 * @var string
37 */
38 private $_value;
39
40 /**
41 * TCP endpoint type
42 */
43 const TYPE_TCP = 'tcp';
44
45 /**
46 * SSL endpoint type
47 */
48 const TYPE_SSL = 'ssl';
49
50 /**
51 * UNIX socket endpoint type
52 */
53 const TYPE_UNIX = 'unix';
54
55 /**
56 * Regexp for TCP endpoints
57 */
58 const REGEXP_TCP = '/^tcp:\/\/(.+?):(\d+)\/?$/';
59
60 /**
61 * Regexp for SSL endpoints
62 */
63 const REGEXP_SSL = '/^ssl:\/\/(.+?):(\d+)\/?$/';
64
65 /**
66 * Regexp for UNIX socket endpoints
67 */
68 const REGEXP_UNIX = '/^unix:\/\/(.+)$/';
69
70 /**
71 * Endpoint index
72 */
73 const ENTRY_ENDPOINT = 'endpoint';
74
75 /**
76 * Databases index
77 */
78 const ENTRY_DATABASES = 'databases';
79
80
81 /**
82 * Create a new endpoint
83 *
84 * @param string $value - endpoint specification
85 *
86 * @throws ClientException
87 *
88 */
89 public function __construct($value)
90 {
91 if (!self::isValid($value)) {
92 throw new ClientException(sprintf("invalid endpoint specification '%s'", $value));
93 }
94
95 $this->_value = $value;
96 }
97
98 /**
99 * Return a string representation of the endpoint
100 *
101 * @magic
102 *
103 * @return string - string representation of the endpoint
104 */
105 public function __toString()
106 {
107 return $this->_value;
108 }
109
110 /**
111 * Return the type of an endpoint
112 *
113 * @param string $value - endpoint specification value
114 *
115 * @return string - endpoint type
116 */
117 public static function getType($value)
118 {
119 if (preg_match(self::REGEXP_TCP, $value)) {
120 return self::TYPE_TCP;
121 }
122
123 if (preg_match(self::REGEXP_SSL, $value)) {
124 return self::TYPE_SSL;
125 }
126
127 if (preg_match(self::REGEXP_UNIX, $value)) {
128 return self::TYPE_UNIX;
129 }
130
131 return null;
132 }
133
134 /**
135 * Return the host name of an endpoint
136 *
137 * @param string $value - endpoint specification value
138 *
139 * @return string - host name
140 */
141 public static function getHost($value)
142 {
143 if (preg_match(self::REGEXP_TCP, $value, $matches)) {
144 return $matches[1];
145 }
146
147 if (preg_match(self::REGEXP_SSL, $value, $matches)) {
148 return $matches[1];
149 }
150
151 return null;
152 }
153
154 /**
155 * check whether an endpoint specification is valid
156 *
157 * @param string $value - endpoint specification value
158 *
159 * @return bool - true if endpoint specification is valid, false otherwise
160 */
161 public static function isValid($value)
162 {
163 if (!is_string($value)) {
164 return false;
165 }
166
167 $type = self::getType($value);
168
169 return !($type === null);
170 }
171
172
173 /**
174 * List endpoints
175 *
176 * This will list the endpoints that are configured on the server
177 *
178 * @param Connection $connection - the connection to be used
179 *
180 * @link https://docs.arangodb.com/HTTP/Endpoints/index.html
181 * @return array $responseArray - The response array.
182 * @throws \triagens\ArangoDb\Exception
183 */
184 public static function listEndpoints(Connection $connection)
185 {
186 $response = $connection->get(Urls::URL_ENDPOINT);
187
188 return $response->getJson();
189 }
190 }
191