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/views.html |
12
|
|
|
*/ |
13
|
|
|
trait ManagesViews |
14
|
|
|
{ |
15
|
|
|
protected ArangoClient $arangoClient; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @see https://www.arangodb.com/docs/stable/http/views-arangosearch.html#create-an-arangosearch-view |
19
|
|
|
* |
20
|
|
|
* @param array<mixed> $view |
21
|
|
|
* @return array<mixed> |
22
|
|
|
* @throws ArangoException |
23
|
|
|
*/ |
24
|
8 |
|
public function createView(array $view): array |
25
|
|
|
{ |
26
|
8 |
|
$view['type'] = isset($view['type']) ? (string) $view['type'] : 'arangosearch'; |
27
|
|
|
|
28
|
8 |
|
$uri = '/_api/view#' . $view['type']; |
29
|
8 |
|
$body = json_encode((object) $view); |
30
|
|
|
|
31
|
8 |
|
$results = $this->arangoClient->request('post', $uri, ['body' => $body]); |
32
|
|
|
|
33
|
8 |
|
return $this->sanitizeRequestMetadata($results); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @see https://www.arangodb.com/docs/stable/http/views-arangosearch.html#drops-a-view |
38
|
|
|
* |
39
|
|
|
* @param string $name |
40
|
|
|
* @return bool |
41
|
|
|
* @throws ArangoException |
42
|
|
|
*/ |
43
|
8 |
|
public function deleteView(string $name): bool |
44
|
|
|
{ |
45
|
8 |
|
$uri = '/_api/view/' . $name; |
46
|
|
|
|
47
|
8 |
|
return (bool) $this->arangoClient->request('delete', $uri); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @see https://www.arangodb.com/docs/stable/http/views-arangosearch.html#list-all-views |
52
|
|
|
* |
53
|
|
|
* @return array<mixed> |
54
|
|
|
* @throws ArangoException |
55
|
|
|
*/ |
56
|
8 |
|
public function getViews(): array |
57
|
|
|
{ |
58
|
8 |
|
$results = $this->arangoClient->request('get', '/_api/view'); |
59
|
|
|
|
60
|
8 |
|
return (array) $results['result']; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Check for view existence |
65
|
|
|
* |
66
|
|
|
* @param string $name |
67
|
|
|
* @return bool |
68
|
|
|
* @throws ArangoException |
69
|
|
|
*/ |
70
|
8 |
|
public function hasView(string $name): bool |
71
|
|
|
{ |
72
|
8 |
|
$views = $this->getViews(); |
73
|
|
|
|
74
|
8 |
|
return array_search($name, array_column($views, 'name'), true) !== false; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @see https://www.arangodb.com/docs/stable/http/views-arangosearch.html#return-information-about-a-view |
79
|
|
|
* |
80
|
|
|
* @param string $name |
81
|
|
|
* @return array<mixed> |
82
|
|
|
* @throws ArangoException |
83
|
|
|
*/ |
84
|
1 |
|
public function getView(string $name): array |
85
|
|
|
{ |
86
|
1 |
|
$result = $this->getViewProperties($name); |
87
|
|
|
|
88
|
1 |
|
return $this->sanitizeRequestMetadata($result); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @see https://www.arangodb.com/docs/stable/http/views-arangosearch.html#read-properties-of-a-view |
93
|
|
|
* |
94
|
|
|
* @param string $name |
95
|
|
|
* @return array<mixed> |
96
|
|
|
* @throws ArangoException |
97
|
|
|
*/ |
98
|
2 |
|
public function getViewProperties(string $name): array |
99
|
|
|
{ |
100
|
2 |
|
$uri = '/_api/view/' . $name . '/properties'; |
101
|
|
|
|
102
|
2 |
|
$result = $this->arangoClient->request('get', $uri); |
103
|
|
|
|
104
|
2 |
|
return $this->sanitizeRequestMetadata($result); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $old |
109
|
|
|
* @param string $new |
110
|
|
|
* @return array<mixed> |
111
|
|
|
* @throws ArangoException |
112
|
|
|
*/ |
113
|
1 |
|
public function renameView(string $old, string $new): array |
114
|
|
|
{ |
115
|
1 |
|
$uri = '/_api/view/' . $old . '/rename'; |
116
|
|
|
|
117
|
1 |
|
$body = json_encode((object) ['name' => $new]); |
118
|
1 |
|
$options = ['body' => $body]; |
119
|
|
|
|
120
|
1 |
|
$result = $this->arangoClient->request('put', $uri, $options); |
121
|
|
|
|
122
|
1 |
|
return $this->sanitizeRequestMetadata($result); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @see https://www.arangodb.com/docs/stable/http/views-arangosearch.html#partially-changes-properties-of-an-arangosearch-view |
127
|
|
|
* |
128
|
|
|
* @param string $name |
129
|
|
|
* @param array<mixed> $properties |
130
|
|
|
* @return array<mixed> |
131
|
|
|
* @throws ArangoException |
132
|
|
|
*/ |
133
|
1 |
|
public function updateView(string $name, array $properties): array |
134
|
|
|
{ |
135
|
|
|
// PrimarySort & primarySortCompression are immutable and will throw if we try to change it. |
136
|
|
|
// Use replaceView if you want to update these properties. |
137
|
1 |
|
if (isset($properties['primarySort'])) { |
138
|
1 |
|
unset($properties['primarySort']); |
139
|
|
|
} |
140
|
1 |
|
if (isset($properties['primarySortCompression'])) { |
141
|
|
|
unset($properties['primarySortCompression']); |
142
|
|
|
} |
143
|
|
|
|
144
|
1 |
|
$properties['type'] = isset($properties['type']) ? (string) $properties['type'] : 'arangosearch'; |
145
|
|
|
|
146
|
1 |
|
$uri = '/_api/view/' . $name . '/properties#' . $properties['type']; |
147
|
1 |
|
$body = json_encode((object) $properties); |
148
|
1 |
|
$options = ['body' => $body]; |
149
|
|
|
|
150
|
1 |
|
$result = $this->arangoClient->request('patch', $uri, $options); |
151
|
1 |
|
return $this->sanitizeRequestMetadata($result); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Replace an existing view. Use this to change immutable fields like primarySort. Note that |
156
|
|
|
* this is just a shorthand for delete(old)/create(new). ArangoDB will have to rebuild the view data. |
157
|
|
|
* |
158
|
|
|
* @see https://www.arangodb.com/docs/stable/http/views-arangosearch.html#create-an-arangosearch-view |
159
|
|
|
* |
160
|
|
|
* @param string $name |
161
|
|
|
* @param array<mixed> $newView |
162
|
|
|
* @return false|array<mixed> |
163
|
|
|
* @throws ArangoException |
164
|
|
|
*/ |
165
|
1 |
|
public function replaceView(string $name, array $newView) |
166
|
|
|
{ |
167
|
1 |
|
if (! $this->hasView($name)) { |
168
|
|
|
return false; |
169
|
|
|
} |
170
|
1 |
|
$this->deleteView($name); |
171
|
|
|
|
172
|
|
|
// Enforce the view name |
173
|
1 |
|
$newView['name'] = $name; |
174
|
|
|
|
175
|
1 |
|
return $this->createView($newView); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|