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/collection.html |
12
|
|
|
*/ |
13
|
|
|
trait ManagesCollections |
14
|
|
|
{ |
15
|
|
|
protected ArangoClient $arangoClient; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* |
19
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
20
|
|
|
* |
21
|
|
|
* @param bool $excludeSystemCollections |
22
|
|
|
* @return array<mixed> |
23
|
|
|
* @throws ArangoException |
24
|
|
|
*/ |
25
|
22 |
|
public function getCollections(bool $excludeSystemCollections = false): array |
26
|
|
|
{ |
27
|
22 |
|
$results = $this->arangoClient->request( |
28
|
22 |
|
'get', |
29
|
22 |
|
'/_api/collection', |
30
|
|
|
[ |
31
|
|
|
'query' => [ |
32
|
22 |
|
'excludeSystem' => $excludeSystemCollections |
33
|
|
|
] |
34
|
|
|
] |
35
|
|
|
); |
36
|
|
|
|
37
|
22 |
|
return (array) $results['result']; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Check for collection existence in current DB. |
42
|
|
|
* |
43
|
|
|
* @param string $collection |
44
|
|
|
* @return bool |
45
|
|
|
* @throws ArangoException |
46
|
|
|
*/ |
47
|
15 |
|
public function hasCollection(string $collection): bool |
48
|
|
|
{ |
49
|
15 |
|
$results = $this->getCollections(); |
50
|
15 |
|
return array_search($collection, array_column($results, 'name'), true) !== false; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @see https://www.arangodb.com/docs/stable/http/collection-getting.html#return-information-about-a-collection |
55
|
|
|
* |
56
|
|
|
* @param string $name |
57
|
|
|
* @return array<mixed> |
58
|
|
|
* @throws ArangoException |
59
|
|
|
*/ |
60
|
1 |
|
public function getCollection(string $name): array |
61
|
|
|
{ |
62
|
1 |
|
$uri = '/_api/collection/' . $name; |
63
|
1 |
|
$result = $this->arangoClient->request('get', $uri); |
64
|
1 |
|
return $this->sanitizeRequestMetadata($result); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @see https://www.arangodb.com/docs/stable/http/collection-getting.html#read-properties-of-a-collection |
69
|
|
|
* |
70
|
|
|
* @param string $collection |
71
|
|
|
* @return array<mixed> |
72
|
|
|
* @throws ArangoException |
73
|
|
|
*/ |
74
|
1 |
|
public function getCollectionProperties(string $collection): array |
75
|
|
|
{ |
76
|
1 |
|
$uri = '/_api/collection/' . $collection . '/properties'; |
77
|
1 |
|
$result = $this->arangoClient->request('get', $uri); |
78
|
1 |
|
return $this->sanitizeRequestMetadata($result); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @see https://www.arangodb.com/docs/stable/http/collection-getting.html#return-number-of-documents-in-a-collection |
83
|
|
|
* |
84
|
|
|
* @param string $collection |
85
|
|
|
* @return array<mixed> |
86
|
|
|
* @throws ArangoException |
87
|
|
|
*/ |
88
|
1 |
|
public function getCollectionDocumentCount(string $collection): array |
89
|
|
|
{ |
90
|
1 |
|
$uri = '/_api/collection/' . $collection . '/count'; |
91
|
1 |
|
$result = $this->arangoClient->request('get', $uri); |
92
|
1 |
|
return $this->sanitizeRequestMetadata($result); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @see https://www.arangodb.com/docs/stable/http/collection-getting.html#return-statistics-for-a-collection |
98
|
|
|
* |
99
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
100
|
|
|
* |
101
|
|
|
* @param string $collection |
102
|
|
|
* @param bool $details |
103
|
|
|
* @return array<mixed> |
104
|
|
|
* @throws ArangoException |
105
|
|
|
*/ |
106
|
2 |
|
public function getCollectionStatistics(string $collection, bool $details = false): array |
107
|
|
|
{ |
108
|
2 |
|
$uri = '/_api/collection/' . $collection . '/figures'; |
109
|
2 |
|
$result = $this->arangoClient->request( |
110
|
2 |
|
'get', |
111
|
|
|
$uri, |
112
|
|
|
[ |
113
|
|
|
'query' => [ |
114
|
2 |
|
'details' => $details |
115
|
|
|
] |
116
|
|
|
] |
117
|
|
|
); |
118
|
2 |
|
return $this->sanitizeRequestMetadata($result); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param string $collection |
123
|
|
|
* @param array<mixed> $config |
124
|
|
|
* @param int|null $waitForSyncReplication |
125
|
|
|
* @param int|null $enforceReplicationFactor |
126
|
|
|
* @return bool |
127
|
|
|
* @throws ArangoException |
128
|
|
|
*/ |
129
|
14 |
|
public function createCollection( |
130
|
|
|
string $collection, |
131
|
|
|
array $config = [], |
132
|
|
|
$waitForSyncReplication = null, |
133
|
|
|
$enforceReplicationFactor = null |
134
|
|
|
): bool { |
135
|
14 |
|
$collection = json_encode((object) array_merge($config, ['name' => $collection])); |
136
|
|
|
|
137
|
14 |
|
$options = ['body' => $collection]; |
138
|
14 |
|
if (isset($waitForSyncReplication)) { |
139
|
|
|
$options['query']['waitForSyncReplication'] = $waitForSyncReplication; |
140
|
|
|
} |
141
|
14 |
|
if (isset($enforceReplicationFactor)) { |
142
|
|
|
$options['query']['enforceReplicationFactor'] = $enforceReplicationFactor; |
143
|
|
|
} |
144
|
|
|
|
145
|
14 |
|
return (bool) $this->arangoClient->request('post', '/_api/collection', $options); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param string $name |
150
|
|
|
* @param array<mixed> $config |
151
|
|
|
* @return array<mixed> |
152
|
|
|
* @throws ArangoException |
153
|
|
|
*/ |
154
|
1 |
|
public function updateCollection(string $name, array $config = []): array |
155
|
|
|
{ |
156
|
1 |
|
$uri = '/_api/collection/' . $name . '/properties'; |
157
|
|
|
|
158
|
1 |
|
$config = json_encode((object) $config); |
159
|
1 |
|
$options = ['body' => $config]; |
160
|
|
|
|
161
|
1 |
|
$result = $this->arangoClient->request('put', $uri, $options); |
162
|
1 |
|
return $this->sanitizeRequestMetadata($result); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param string $old |
167
|
|
|
* @param string $new |
168
|
|
|
* @return array<mixed> |
169
|
|
|
* @throws ArangoException |
170
|
|
|
*/ |
171
|
1 |
|
public function renameCollection(string $old, string $new): array |
172
|
|
|
{ |
173
|
1 |
|
$uri = '/_api/collection/' . $old . '/rename'; |
174
|
|
|
|
175
|
1 |
|
$newName = json_encode((object) ['name' => $new]); |
176
|
1 |
|
$options = ['body' => $newName]; |
177
|
|
|
|
178
|
1 |
|
$result = $this->arangoClient->request('put', $uri, $options); |
179
|
1 |
|
return $this->sanitizeRequestMetadata($result); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param string $name |
184
|
|
|
* @return bool |
185
|
|
|
* @throws ArangoException |
186
|
|
|
*/ |
187
|
14 |
|
public function deleteCollection(string $name): bool |
188
|
|
|
{ |
189
|
14 |
|
$uri = '/_api/collection/' . $name; |
190
|
|
|
|
191
|
14 |
|
return (bool) $this->arangoClient->request('delete', $uri); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|