|
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\Type; |
|
13
|
|
|
|
|
14
|
|
|
use ArangoDb\Guard\Guard; |
|
15
|
|
|
use ArangoDb\Http\Url; |
|
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 Batch implements BatchType |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var Type[] |
|
25
|
|
|
*/ |
|
26
|
|
|
private $types = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var Guard[] |
|
30
|
|
|
*/ |
|
31
|
|
|
private $guards; |
|
32
|
|
|
|
|
33
|
6 |
|
public function __construct(Type ...$types) |
|
34
|
|
|
{ |
|
35
|
6 |
|
foreach ($types as $key => $type) { |
|
36
|
6 |
|
if ($type instanceof GuardSupport && null !== ($guard = $type->guard())) { |
|
37
|
3 |
|
if (null !== $guard->contentId()) { |
|
38
|
2 |
|
$key = $guard->contentId(); |
|
39
|
|
|
} |
|
40
|
3 |
|
$this->guards[] = $guard; |
|
41
|
|
|
} |
|
42
|
6 |
|
$this->types[$key] = $type; |
|
43
|
|
|
} |
|
44
|
6 |
|
} |
|
45
|
|
|
|
|
46
|
6 |
|
public static function fromTypes(Type ...$types): BatchType |
|
47
|
|
|
{ |
|
48
|
6 |
|
return new self(...$types); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
6 |
|
public function toRequest( |
|
52
|
|
|
RequestFactoryInterface $requestFactory, |
|
53
|
|
|
StreamFactoryInterface $streamFactory |
|
54
|
|
|
): RequestInterface { |
|
55
|
6 |
|
$body = ''; |
|
56
|
|
|
|
|
57
|
6 |
|
$boundary = '--' . self::MIME_BOUNDARY . self::EOL; |
|
58
|
6 |
|
$boundary .= 'Content-Type: application/x-arango-batchpart' . self::EOL; |
|
59
|
|
|
|
|
60
|
6 |
|
foreach ($this->types as $key => $type) { |
|
61
|
6 |
|
$body .= $boundary; |
|
62
|
6 |
|
$body .= 'Content-Id: ' . $key . self::BODY_SEPARATOR; |
|
63
|
|
|
|
|
64
|
6 |
|
$body .= $this->typeToString($type, $requestFactory, $streamFactory) . self::EOL; |
|
65
|
|
|
} |
|
66
|
6 |
|
$body .= '--' . self::MIME_BOUNDARY . '--' . self::BODY_SEPARATOR; |
|
67
|
|
|
|
|
68
|
6 |
|
$request = $requestFactory->createRequest(RequestMethodInterface::METHOD_POST, Url::BATCH); |
|
69
|
6 |
|
$request = $request->withHeader('Content-Type', 'multipart/form-data'); |
|
70
|
6 |
|
$request = $request->withHeader('boundary', self::MIME_BOUNDARY); |
|
71
|
|
|
|
|
72
|
6 |
|
$request->getBody()->write($body); |
|
73
|
6 |
|
$request->getBody()->rewind(); |
|
74
|
|
|
|
|
75
|
6 |
|
return $request; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
5 |
|
public function guards(): ?array |
|
79
|
|
|
{ |
|
80
|
5 |
|
return $this->guards; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Builds the batch request body |
|
85
|
|
|
* |
|
86
|
|
|
* @param Type $type |
|
87
|
|
|
* @param RequestFactoryInterface $requestFactory |
|
88
|
|
|
* @param StreamFactoryInterface $streamFactory |
|
89
|
|
|
* @return string |
|
90
|
|
|
*/ |
|
91
|
6 |
|
private function typeToString( |
|
92
|
|
|
Type $type, |
|
93
|
|
|
RequestFactoryInterface $requestFactory, |
|
94
|
|
|
StreamFactoryInterface $streamFactory |
|
95
|
|
|
): string { |
|
96
|
6 |
|
$request = $type->toRequest($requestFactory, $streamFactory); |
|
97
|
6 |
|
$body = $request->getBody()->getContents(); |
|
98
|
|
|
|
|
99
|
6 |
|
if ('' !== $body) { |
|
100
|
6 |
|
$body = self::EOL . self::EOL . $body; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
6 |
|
$body = $request->getMethod() . ' ' |
|
104
|
6 |
|
. $request->getUri()->__toString() |
|
105
|
6 |
|
. ' HTTP/' . $request->getProtocolVersion() |
|
106
|
6 |
|
. $body; |
|
107
|
|
|
|
|
108
|
6 |
|
return $body; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|