1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Sandro Keil (https://sandro-keil.de) |
4
|
|
|
* |
5
|
|
|
* @link http://github.com/sandrokeil/arangodb-php-client for the canonical source repository |
6
|
|
|
* @copyright Copyright (c) 2018-2020 Sandro Keil |
7
|
|
|
* @license http://github.com/sandrokeil/arangodb-php-client/blob/master/LICENSE.md New BSD License |
8
|
|
|
*/ |
9
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
namespace ArangoDb\Type; |
12
|
|
|
|
13
|
|
|
use ArangoDb\Guard\Guard; |
14
|
|
|
use ArangoDb\Http\Url; |
15
|
|
|
use ArangoDb\Util\Json; |
16
|
|
|
use Fig\Http\Message\RequestMethodInterface; |
17
|
|
|
use Psr\Http\Message\RequestFactoryInterface; |
18
|
|
|
use Psr\Http\Message\RequestInterface; |
19
|
|
|
use Psr\Http\Message\StreamFactoryInterface; |
20
|
|
|
|
21
|
|
|
final class Database implements DatabaseType |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var string|null |
25
|
|
|
*/ |
26
|
|
|
private $name; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $options; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $uri; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private $method; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Guard |
45
|
|
|
* |
46
|
|
|
* @var Guard |
47
|
|
|
*/ |
48
|
|
|
private $guard; |
49
|
|
|
|
50
|
|
|
private function __construct( |
51
|
|
|
?string $name, |
52
|
|
|
string $uri = '', |
53
|
|
|
string $method = RequestMethodInterface::METHOD_GET, |
54
|
|
|
array $options = [] |
55
|
|
|
) { |
56
|
|
|
$this->name = $name; |
57
|
|
|
$this->uri = $uri; |
58
|
|
|
$this->method = $method; |
59
|
|
|
$this->options = $options; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public static function create(string $databaseName, array $options = []): DatabaseType |
63
|
|
|
{ |
64
|
|
|
$options['name'] = $databaseName; |
65
|
|
|
return new self($databaseName, '', RequestMethodInterface::METHOD_POST, $options); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public static function delete(string $databaseName): DatabaseType |
69
|
|
|
{ |
70
|
|
|
return new self($databaseName, '/' . $databaseName, RequestMethodInterface::METHOD_DELETE); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public static function info(): DatabaseType |
74
|
|
|
{ |
75
|
|
|
return new self('', '/current'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public static function listAccessible(): DatabaseType |
79
|
|
|
{ |
80
|
|
|
return new self('', '/user'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public static function listAll(): DatabaseType |
84
|
|
|
{ |
85
|
|
|
return new self(''); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function toRequest( |
89
|
|
|
RequestFactoryInterface $requestFactory, |
90
|
|
|
StreamFactoryInterface $streamFactory |
91
|
|
|
): RequestInterface { |
92
|
|
|
$request = $requestFactory->createRequest($this->method, Url::DATABASE . $this->uri); |
93
|
|
|
|
94
|
|
|
if (0 === count($this->options)) { |
95
|
|
|
return $request; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $request->withBody($streamFactory->createStream(Json::encode($this->options))); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function useGuard(Guard $guard): Type |
102
|
|
|
{ |
103
|
|
|
$this->guard = $guard; |
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function guard(): ?Guard |
108
|
|
|
{ |
109
|
|
|
return $this->guard; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|