-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRequestBuilder.php
150 lines (130 loc) · 3.82 KB
/
RequestBuilder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
declare(strict_types=1);
namespace Core\Request;
use Core\Authentication\Auth;
use Core\Client;
use Core\Utils\CoreHelper;
use Core\Utils\XmlSerializer;
use CoreInterfaces\Core\Format;
use CoreInterfaces\Core\Request\ParamInterface;
use CoreInterfaces\Http\RetryOption;
class RequestBuilder
{
private $requestMethod;
private $path;
/**
* @var string|null
*/
private $server;
private $retryOption = RetryOption::USE_GLOBAL_SETTINGS;
private $allowContentType = true;
/**
* @var ParamInterface[]
*/
private $parameters = [];
/**
* @var callable
*/
private $bodySerializer = [CoreHelper::class, 'serialize'];
private $bodyFormat = Format::JSON;
/**
* @var Auth|null
*/
private $auth;
public function __construct(string $requestMethod, string $path)
{
$this->requestMethod = $requestMethod;
$this->path = $path;
}
/**
* The server URL to be set for the request.
*/
public function server(string $server): self
{
$this->server = $server;
return $this;
}
/**
* Sets the retryOption value that is to be set for the request on creation.
*/
public function retryOption(string $retryOption): self
{
$this->retryOption = $retryOption;
return $this;
}
/**
* Disables setting of allowContentType for request on creation.
*/
public function disableContentType(): self
{
$this->allowContentType = false;
return $this;
}
/**
* @param Auth|string ...$auths
* @return $this
*/
public function auth(...$auths): self
{
$this->auth = Auth::or(...$auths);
return $this;
}
/**
* Parameters to be set on request creation.
*/
public function parameters(ParamInterface ...$parameters): self
{
$this->parameters = array_merge($this->parameters, $parameters);
return $this;
}
/**
* Sets body format to xml and serializes the body to xml.
*/
public function bodyXml(string $rootName): self
{
$this->bodyFormat = Format::XML;
$this->bodySerializer = function ($value) use ($rootName): string {
return (new XmlSerializer([]))->serialize($rootName, $value);
};
return $this;
}
/**
* Sets body format to xml and serializes the body to xml.
*/
public function bodyXmlArray(string $rootName, string $itemName): self
{
$this->bodyFormat = Format::XML;
$this->bodySerializer = function ($value) use ($rootName, $itemName): string {
return (new XmlSerializer([]))->serializeArray($rootName, $itemName, $value);
};
return $this;
}
/**
* Sets body format to xml and serializes the body to xml.
*/
public function bodyXmlMap(string $rootName): self
{
$this->bodyFormat = Format::XML;
$this->bodySerializer = function ($value) use ($rootName): string {
return (new XmlSerializer([]))->serializeMap($rootName, $value);
};
return $this;
}
/**
* Initializes a new Request object with the properties set within RequestBuilder.
*/
public function build(CLient $coreClient): Request
{
$request = $coreClient->getGlobalRequest($this->server);
$request->appendPath($this->path);
$request->setHttpMethod($this->requestMethod);
$request->setRetryOption($this->retryOption);
$request->shouldAddContentType($this->allowContentType);
$coreClient->validateParameters($this->parameters)->apply($request);
if (isset($this->auth)) {
$coreClient->validateAuth($this->auth)->apply($request);
}
$request->setBodyFormat($this->bodyFormat, $this->bodySerializer);
return $request;
}
}