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-2019 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; |
13
|
|
|
|
14
|
|
|
use ArangoDb\Exception\InvalidArgumentException; |
15
|
|
|
use ArangoDb\Exception\LogicException; |
16
|
|
|
use ArangoDb\Guard\Guard; |
17
|
|
|
use ArangoDb\Http\JsonStream; |
18
|
|
|
use ArangoDb\Type\BatchType; |
19
|
|
|
use Countable; |
20
|
|
|
use Iterator; |
21
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
22
|
|
|
use Psr\Http\Message\ResponseInterface; |
23
|
|
|
|
24
|
|
|
final class BatchResult implements Countable, Iterator |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* responses |
28
|
|
|
* |
29
|
|
|
* @var ResponseInterface[] |
30
|
|
|
*/ |
31
|
|
|
private $responses = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var ResponseFactoryInterface |
35
|
|
|
*/ |
36
|
|
|
private $responseFactory; |
37
|
|
|
|
38
|
3 |
|
private function __construct(ResponseFactoryInterface $responseFactory) |
39
|
|
|
{ |
40
|
3 |
|
$this->responseFactory = $responseFactory; |
41
|
3 |
|
} |
42
|
|
|
|
43
|
4 |
|
public static function fromResponse( |
44
|
|
|
ResponseInterface $batchResponse, |
45
|
|
|
ResponseFactoryInterface $responseFactory |
46
|
|
|
): BatchResult { |
47
|
4 |
|
if ('multipart/form-data' !== ($batchResponse->getHeader('Content-Type')[0] ?? '')) { |
48
|
1 |
|
throw new InvalidArgumentException('Provided $batchResponse must have content type "multipart/form-data".'); |
49
|
|
|
} |
50
|
|
|
|
51
|
3 |
|
$batches = explode( |
52
|
3 |
|
'--' . BatchType::MIME_BOUNDARY . BatchType::EOL, |
53
|
3 |
|
trim($batchResponse->getBody()->getContents(), '--' . BatchType::MIME_BOUNDARY . '--') |
54
|
|
|
); |
55
|
|
|
|
56
|
3 |
|
$self = new self($responseFactory); |
57
|
|
|
|
58
|
3 |
|
foreach ($batches as $batch) { |
59
|
3 |
|
$data = HttpHelper::parseMessage($batch); |
60
|
3 |
|
[$httpCode, $headers, $body] = HttpHelper::parseMessage($data[2] ?? ''); |
61
|
|
|
|
62
|
3 |
|
$response = $self->responseFactory->createResponse($httpCode); |
63
|
|
|
|
64
|
3 |
|
foreach ($headers as $headerName => $header) { |
65
|
3 |
|
$response = $response->withAddedHeader($headerName, $header); |
66
|
|
|
} |
67
|
3 |
|
$response = $response->withBody(new JsonStream($body)); |
68
|
|
|
|
69
|
3 |
|
if (isset($data[1]['Content-Id'][0])) { |
70
|
3 |
|
$self->responses[$data[1]['Content-Id'][0]] = $response; |
71
|
|
|
} else { |
72
|
|
|
$self->responses[] = $response; |
73
|
|
|
} |
74
|
|
|
} |
75
|
3 |
|
return $self; |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
public function validateBatch(BatchType $batch): void |
79
|
|
|
{ |
80
|
1 |
|
$guards = $batch->guards(); |
81
|
|
|
|
82
|
1 |
|
if ($guards === null) { |
83
|
|
|
throw new LogicException('No guards are provided in Batch.'); |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
$this->validate(... $guards); |
87
|
1 |
|
} |
88
|
|
|
|
89
|
3 |
|
public function validate(Guard ...$guards): void |
90
|
|
|
{ |
91
|
3 |
|
foreach ($guards as $guard) { |
92
|
3 |
|
if ($guard->contentId() === null) { |
93
|
1 |
|
foreach ($this->responses as $response) { |
94
|
1 |
|
$guard($response); |
95
|
|
|
} |
96
|
1 |
|
continue; |
97
|
|
|
} |
98
|
2 |
|
if (null !== ($response = $this->responses[$guard->contentId()] ?? null)) { |
99
|
2 |
|
$guard($response); |
100
|
|
|
} |
101
|
|
|
} |
102
|
3 |
|
} |
103
|
|
|
|
104
|
|
|
public function response(string $contentId): ?ResponseInterface |
105
|
|
|
{ |
106
|
|
|
return $this->responses[$contentId] ?? null; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function responses(): array |
110
|
|
|
{ |
111
|
|
|
return $this->responses; |
112
|
|
|
} |
113
|
|
|
|
114
|
1 |
|
public function count(): int |
115
|
|
|
{ |
116
|
1 |
|
return count($this->responses); |
117
|
|
|
} |
118
|
|
|
|
119
|
1 |
|
public function current() |
120
|
|
|
{ |
121
|
1 |
|
return current($this->responses); |
122
|
|
|
} |
123
|
|
|
|
124
|
1 |
|
public function next() |
125
|
|
|
{ |
126
|
1 |
|
next($this->responses); |
127
|
1 |
|
} |
128
|
|
|
|
129
|
1 |
|
public function key() |
130
|
|
|
{ |
131
|
1 |
|
return key($this->responses); |
132
|
|
|
} |
133
|
|
|
|
134
|
1 |
|
public function valid() |
135
|
|
|
{ |
136
|
1 |
|
return $this->key() !== null; |
137
|
|
|
} |
138
|
|
|
|
139
|
1 |
|
public function rewind() |
140
|
|
|
{ |
141
|
1 |
|
reset($this->responses); |
142
|
1 |
|
} |
143
|
|
|
} |
144
|
|
|
|