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
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace ArangoDb\Handler; |
13
|
|
|
|
14
|
|
|
use ArangoDb\Exception\GuardErrorException; |
15
|
|
|
use ArangoDb\Exception\UnexpectedResponse; |
16
|
|
|
use ArangoDb\Guard\Guard; |
17
|
|
|
use ArangoDb\Guard\SuccessHttpStatusCode; |
18
|
|
|
use ArangoDb\Http\TypeSupport; |
19
|
|
|
use ArangoDb\Type\Document as DocumentType; |
20
|
|
|
use ArangoDb\Util\Json; |
21
|
|
|
use Psr\Http\Message\ResponseInterface; |
22
|
|
|
|
23
|
|
|
final class Document implements DocumentHandler |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var TypeSupport |
27
|
|
|
**/ |
28
|
|
|
private $client; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Guard |
32
|
|
|
*/ |
33
|
|
|
private $guard; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $documentClass; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param TypeSupport $client |
42
|
|
|
* @param string $documentClass FQCN of the class which implements \ArangoDb\Type\DocumentType |
43
|
|
|
* @param Guard|null $guard |
44
|
|
|
*/ |
45
|
5 |
|
public function __construct( |
46
|
|
|
TypeSupport $client, |
47
|
|
|
string $documentClass = DocumentType::class, |
48
|
|
|
Guard $guard = null |
49
|
|
|
) { |
50
|
5 |
|
$this->client = $client; |
51
|
5 |
|
$this->documentClass = $documentClass; |
52
|
5 |
|
$this->guard = $guard ?? SuccessHttpStatusCode::withoutContentId(); |
53
|
5 |
|
} |
54
|
|
|
|
55
|
5 |
|
public function save(string $collectionName, array $docs, int $flags = 0): string |
56
|
|
|
{ |
57
|
5 |
|
$type = ($this->documentClass)::create($collectionName, $docs, $flags) |
58
|
5 |
|
->useGuard($this->guard); |
59
|
|
|
|
60
|
5 |
|
$response = $this->client->sendType($type); |
61
|
|
|
|
62
|
5 |
|
$data = Json::decode($response->getBody()->getContents()); |
63
|
|
|
|
64
|
5 |
|
if (! isset($data['_id'])) { |
65
|
|
|
throw UnexpectedResponse::forType($type, $response); |
66
|
|
|
} |
67
|
|
|
|
68
|
5 |
|
return $data['_id']; |
69
|
|
|
} |
70
|
|
|
|
71
|
2 |
|
public function get(string $documentId): ResponseInterface |
72
|
|
|
{ |
73
|
2 |
|
$type = ($this->documentClass)::read($documentId)->useGuard($this->guard); |
74
|
|
|
|
75
|
2 |
|
return $this->client->sendType($type); |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
public function getById(string $collectionName, string $id): ResponseInterface |
79
|
|
|
{ |
80
|
1 |
|
return $this->get($collectionName . self::ID_SEPARATOR . $id); |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
public function remove(string $documentId): void |
84
|
|
|
{ |
85
|
2 |
|
$type = ($this->documentClass)::deleteOne($documentId) |
86
|
2 |
|
->useGuard($this->guard); |
87
|
|
|
|
88
|
2 |
|
$this->client->sendType($type); |
89
|
2 |
|
} |
90
|
|
|
|
91
|
1 |
|
public function removeById(string $collectionName, string $id): void |
92
|
|
|
{ |
93
|
1 |
|
$this->remove($collectionName . self::ID_SEPARATOR . $id); |
94
|
1 |
|
} |
95
|
|
|
|
96
|
4 |
|
public function has(string $documentId): bool |
97
|
|
|
{ |
98
|
4 |
|
$type = ($this->documentClass)::readHeader($documentId) |
99
|
4 |
|
->useGuard($this->guard); |
100
|
|
|
|
101
|
|
|
try { |
102
|
4 |
|
$this->client->sendType($type); |
103
|
|
|
|
104
|
4 |
|
return true; |
105
|
2 |
|
} catch (GuardErrorException $e) { |
106
|
2 |
|
return false; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
2 |
|
public function hasById(string $collectionName, string $id): bool |
111
|
|
|
{ |
112
|
2 |
|
return $this->has($collectionName . self::ID_SEPARATOR . $id); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|