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