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