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