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