|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ArangoClient; |
|
6
|
|
|
|
|
7
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
|
8
|
|
|
|
|
9
|
|
|
class DatabaseClient |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var Connector |
|
13
|
|
|
*/ |
|
14
|
|
|
protected Connector $connector; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Documents constructor. |
|
18
|
|
|
* @param Connector $connector |
|
19
|
|
|
*/ |
|
20
|
8 |
|
public function __construct(Connector $connector) |
|
21
|
|
|
{ |
|
22
|
8 |
|
$this->connector = $connector; |
|
23
|
8 |
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @return array<mixed> |
|
27
|
|
|
* @throws GuzzleException|Exceptions\ArangoDbException |
|
28
|
|
|
*/ |
|
29
|
1 |
|
public function read(): array |
|
30
|
|
|
{ |
|
31
|
1 |
|
return (array) $this->connector->request('get', '/_api/database/current'); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return array<mixed> |
|
36
|
|
|
* |
|
37
|
|
|
* @throws GuzzleException|Exceptions\ArangoDbException |
|
38
|
|
|
*/ |
|
39
|
8 |
|
public function listDatabases(): array |
|
40
|
|
|
{ |
|
41
|
8 |
|
return (array) $this->connector->request('get', '/_api/database'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param $database |
|
46
|
|
|
* @return bool |
|
47
|
|
|
* @throws Exceptions\ArangoDbException |
|
48
|
|
|
* @throws GuzzleException |
|
49
|
|
|
*/ |
|
50
|
8 |
|
public function exists($database): bool |
|
51
|
|
|
{ |
|
52
|
8 |
|
$databaseList = $this->listDatabases(); |
|
53
|
8 |
|
return in_array($database, $databaseList); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return array<mixed> |
|
58
|
|
|
* |
|
59
|
|
|
* @throws GuzzleException|Exceptions\ArangoDbException |
|
60
|
|
|
*/ |
|
61
|
1 |
|
public function listMyDatabases(): array |
|
62
|
|
|
{ |
|
63
|
1 |
|
return (array) $this->connector->request('get', '/_api/database/user'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param string $name |
|
68
|
|
|
* @param null $options |
|
|
|
|
|
|
69
|
|
|
* @param null $users |
|
|
|
|
|
|
70
|
|
|
* @return bool |
|
71
|
|
|
* @throws Exceptions\ArangoDbException |
|
72
|
|
|
* @throws GuzzleException |
|
73
|
|
|
*/ |
|
74
|
3 |
|
public function create(string $name, $options = null, $users = null): bool |
|
75
|
|
|
{ |
|
76
|
3 |
|
$database = json_encode((object)['name' => $name, 'options' => $options, 'users' => $users]); |
|
77
|
|
|
|
|
78
|
3 |
|
return (bool) $this->connector->request('post', '/_api/database', ['body' => $database]); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param string $name |
|
83
|
|
|
* @return bool |
|
84
|
|
|
* @throws Exceptions\ArangoDbException |
|
85
|
|
|
* @throws GuzzleException |
|
86
|
|
|
*/ |
|
87
|
1 |
|
public function delete(string $name): bool |
|
88
|
|
|
{ |
|
89
|
1 |
|
$uri = '/_api/database/' . $name; |
|
90
|
|
|
|
|
91
|
1 |
|
return (bool) $this->connector->request('delete', $uri); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|