1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ArangoClient\Schema; |
6
|
|
|
|
7
|
|
|
use ArangoClient\ArangoClient; |
8
|
|
|
use ArangoClient\Exceptions\ArangoException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @see https://www.arangodb.com/docs/stable/http/gharial-management.html |
12
|
|
|
*/ |
13
|
|
|
trait ManagesGraphs |
14
|
|
|
{ |
15
|
|
|
protected ArangoClient $arangoClient; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @see https://www.arangodb.com/docs/stable/http/gharial-management.html#create-a-graph |
19
|
|
|
* |
20
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
21
|
|
|
* |
22
|
|
|
* @param string $name |
23
|
|
|
* @param array<mixed> $config |
24
|
|
|
* @param bool|null $waitForSync |
25
|
|
|
* @return mixed |
26
|
|
|
* @throws ArangoException |
27
|
|
|
*/ |
28
|
12 |
|
public function createGraph( |
29
|
|
|
string $name, |
30
|
|
|
array $config = [], |
31
|
|
|
$waitForSync = false |
32
|
|
|
) { |
33
|
12 |
|
$options = []; |
34
|
12 |
|
$options['query']['waitForSync'] = (int) $waitForSync; |
35
|
12 |
|
$options['body'] = $config; |
36
|
12 |
|
$options['body']['name'] = $name; |
37
|
|
|
|
38
|
12 |
|
$result = $this->arangoClient->request('post', '/_api/gharial', $options); |
39
|
|
|
|
40
|
12 |
|
return $result['graph']; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @see https://www.arangodb.com/docs/stable/http/gharial-management.html#list-all-graphs |
45
|
|
|
* |
46
|
|
|
* @return array<mixed> |
47
|
|
|
* @throws ArangoException |
48
|
|
|
*/ |
49
|
11 |
|
public function getGraphs(): array |
50
|
|
|
{ |
51
|
11 |
|
$results = $this->arangoClient->request( |
52
|
11 |
|
'get', |
53
|
11 |
|
'/_api/gharial' |
54
|
|
|
); |
55
|
|
|
|
56
|
11 |
|
return (array) $results['graphs']; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Check for graph existence in current DB. |
61
|
|
|
* |
62
|
|
|
* @param string $name |
63
|
|
|
* @return bool |
64
|
|
|
* @throws ArangoException |
65
|
|
|
*/ |
66
|
10 |
|
public function hasGraph(string $name): bool |
67
|
|
|
{ |
68
|
10 |
|
$results = $this->getGraphs(); |
69
|
|
|
|
70
|
10 |
|
return array_search($name, array_column($results, '_key'), true) !== false; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @see https://www.arangodb.com/docs/stable/http/gharial-management.html#get-a-graph |
75
|
|
|
* |
76
|
|
|
* @param string $name |
77
|
|
|
* @return array<mixed> |
78
|
|
|
* @throws ArangoException |
79
|
|
|
*/ |
80
|
1 |
|
public function getGraph(string $name): array |
81
|
|
|
{ |
82
|
1 |
|
$uri = '/_api/gharial/' . $name; |
83
|
|
|
|
84
|
1 |
|
return (array) $this->arangoClient->request('get', $uri)['graph']; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @see https://www.arangodb.com/docs/stable/http/gharial-management.html#drop-a-graph |
89
|
|
|
* |
90
|
|
|
* @param string $name |
91
|
|
|
* @return bool |
92
|
|
|
* @throws ArangoException |
93
|
|
|
*/ |
94
|
12 |
|
public function deleteGraph(string $name): bool |
95
|
|
|
{ |
96
|
12 |
|
$uri = '/_api/gharial/' . $name; |
97
|
|
|
|
98
|
12 |
|
return (bool) $this->arangoClient->request('delete', $uri); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @see https://www.arangodb.com/docs/stable/http/gharial-management.html#list-vertex-collections |
103
|
|
|
* |
104
|
|
|
* @param string $name |
105
|
|
|
* @return array<mixed> |
106
|
|
|
* @throws ArangoException |
107
|
|
|
*/ |
108
|
1 |
|
public function getGraphVertices(string $name): array |
109
|
|
|
{ |
110
|
1 |
|
$uri = '/_api/gharial/' . $name . '/vertex'; |
111
|
|
|
|
112
|
1 |
|
return (array) $this->arangoClient->request('get', $uri)['collections']; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @see https://www.arangodb.com/docs/stable/http/gharial-management.html#add-vertex-collection |
117
|
|
|
* |
118
|
|
|
* @param string $name |
119
|
|
|
* @param string $vertex |
120
|
|
|
* @return array<mixed> |
121
|
|
|
* @throws ArangoException |
122
|
|
|
*/ |
123
|
1 |
|
public function addGraphVertex(string $name, string $vertex): array |
124
|
|
|
{ |
125
|
1 |
|
$uri = '/_api/gharial/' . $name . '/vertex'; |
126
|
|
|
|
127
|
|
|
$options = [ |
128
|
|
|
'body' => [ |
129
|
1 |
|
'collection' => $vertex |
130
|
|
|
] |
131
|
|
|
]; |
132
|
1 |
|
return (array) $this->arangoClient->request('post', $uri, $options)['graph']; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @see https://www.arangodb.com/docs/stable/http/gharial-management.html#add-vertex-collection |
138
|
|
|
* |
139
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
140
|
|
|
* |
141
|
|
|
* @param string $name |
142
|
|
|
* @param string $vertex |
143
|
|
|
* @param bool $dropCollection |
144
|
|
|
* @return array<mixed> |
145
|
|
|
* @throws ArangoException |
146
|
|
|
*/ |
147
|
1 |
|
public function removeGraphVertex(string $name, string $vertex, bool $dropCollection = false): array |
148
|
|
|
{ |
149
|
1 |
|
$uri = '/_api/gharial/' . $name . '/vertex/' . $vertex; |
150
|
|
|
|
151
|
1 |
|
$options = []; |
152
|
1 |
|
$options['query']['dropCollection'] = $dropCollection; |
153
|
|
|
|
154
|
1 |
|
return (array) $this->arangoClient->request('delete', $uri, $options)['graph']; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @see https://www.arangodb.com/docs/stable/http/gharial-management.html#list-edge-definitions |
159
|
|
|
* |
160
|
|
|
* @param string $name |
161
|
|
|
* @return array<mixed> |
162
|
|
|
* @throws ArangoException |
163
|
|
|
*/ |
164
|
1 |
|
public function getGraphEdges(string $name): array |
165
|
|
|
{ |
166
|
1 |
|
$uri = '/_api/gharial/' . $name . '/edge'; |
167
|
|
|
|
168
|
1 |
|
return (array) $this->arangoClient->request('get', $uri)['collections']; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @see https://www.arangodb.com/docs/stable/http/gharial-management.html#add-edge-definition |
173
|
|
|
* |
174
|
|
|
* @param string $name |
175
|
|
|
* @param array<mixed> $edgeDefinition |
176
|
|
|
* @return array<mixed> |
177
|
|
|
* @throws ArangoException |
178
|
|
|
*/ |
179
|
1 |
|
public function addGraphEdge(string $name, array $edgeDefinition): array |
180
|
|
|
{ |
181
|
1 |
|
$uri = '/_api/gharial/' . $name . '/edge'; |
182
|
|
|
|
183
|
|
|
$options = [ |
184
|
1 |
|
'body' => $edgeDefinition |
185
|
|
|
]; |
186
|
1 |
|
return (array) $this->arangoClient->request('post', $uri, $options)['graph']; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @see https://www.arangodb.com/docs/stable/http/gharial-management.html#replace-an-edge-definition |
191
|
|
|
* |
192
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
193
|
|
|
* |
194
|
|
|
* @param string $name |
195
|
|
|
* @param string $edge |
196
|
|
|
* @param array<mixed> $edgeDefinition |
197
|
|
|
* @param bool $dropCollection |
198
|
|
|
* @param bool $waitForSync |
199
|
|
|
* @return array<mixed> |
200
|
|
|
* @throws ArangoException |
201
|
|
|
*/ |
202
|
1 |
|
public function replaceGraphEdge( |
203
|
|
|
string $name, |
204
|
|
|
string $edge, |
205
|
|
|
array $edgeDefinition, |
206
|
|
|
bool $dropCollection = false, |
207
|
|
|
bool $waitForSync = false |
208
|
|
|
): array { |
209
|
1 |
|
$uri = '/_api/gharial/' . $name . '/edge/' . $edge . '#definition'; |
210
|
|
|
|
211
|
1 |
|
$options = []; |
212
|
1 |
|
$options['query']['waitForSync'] = $waitForSync; |
213
|
1 |
|
$options['query']['dropCollection'] = $dropCollection; |
214
|
1 |
|
$options['body'] = $edgeDefinition; |
215
|
|
|
|
216
|
1 |
|
return (array) $this->arangoClient->request('put', $uri, $options)['graph']; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @see https://www.arangodb.com/docs/stable/http/gharial-management.html#remove-an-edge-definition-from-the-graph |
221
|
|
|
* |
222
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
223
|
|
|
* |
224
|
|
|
* @param string $name |
225
|
|
|
* @param string $edge |
226
|
|
|
* @param bool $dropCollection |
227
|
|
|
* @param bool $waitForSync |
228
|
|
|
* @return array<mixed> |
229
|
|
|
* @throws ArangoException |
230
|
|
|
*/ |
231
|
1 |
|
public function removeGraphEdge( |
232
|
|
|
string $name, |
233
|
|
|
string $edge, |
234
|
|
|
bool $dropCollection = true, |
235
|
|
|
bool $waitForSync = false |
236
|
|
|
): array { |
237
|
1 |
|
$uri = '/_api/gharial/' . $name . '/edge/' . $edge . '#definition'; |
238
|
|
|
|
239
|
1 |
|
$options = []; |
240
|
1 |
|
$options['query']['waitForSync'] = $waitForSync; |
241
|
1 |
|
$options['query']['dropCollection'] = $dropCollection; |
242
|
|
|
|
243
|
1 |
|
return (array) $this->arangoClient->request('delete', $uri, $options)['graph']; |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|