-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathOptions.php
More file actions
168 lines (150 loc) · 4.82 KB
/
Options.php
File metadata and controls
168 lines (150 loc) · 4.82 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/*
* This file is part of Berlioz framework.
*
* @license https://opensource.org/licenses/MIT MIT License
* @copyright 2022 Ronan GIRON
* @author Ronan GIRON <https://github.com/ElGigi>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code, to the root.
*/
declare(strict_types=1);
namespace Berlioz\Http\Client;
use Berlioz\Http\Client\Cookies\CookiesManager;
use Closure;
class Options
{
private const DEFAULT_HEADERS = [
'Accept' => ['text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'],
'User-Agent' => ['Berlioz Client/2.0'],
'Accept-Language' => ['fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3'],
'Accept-Encoding' => ['gzip, deflate'],
'Connection' => ['close'],
];
private array $userDefined;
public function __construct(
// Default adapter
public ?string $adapter = null,
// Base of URI for URI requests
public ?string $baseUri = null,
// If client follow locations
public int|false $followLocation = 5,
// Sleep time between each request
public int $sleepTime = 0,
// File where log requests/responses
public ?string $logFile = null,
// Throw exceptions on HTTP error cases
public bool $exceptions = true,
// Retry on network exception
public int|false $retry = 3,
// Time between retries (in milliseconds)
public int $retryTime = 1000,
// NULL: to use default cookie manager, FALSE: to not use cookies, a CookieManager object to use
public CookiesManager|false|null $cookies = null,
// History size (int)
public int|float $history = INF,
// Callback on each request
public ?Closure $callback = null,
// Callback on exception
public ?Closure $callbackException = null,
// Default headers
public array $headers = self::DEFAULT_HEADERS,
// HTTP context
public ?HttpContext $context = null,
...$userDefined,
) {
$this->userDefined = $userDefined;
}
public static function make(array|self|null $options = null, ?self $initial = null): self
{
// None defined
if (null === $options) {
if (null !== $initial) {
return clone $initial;
}
return new self();
}
// Array of options
if (is_array($options)) {
$new = new self(
adapter: $initial?->adapter,
baseUri: $initial?->baseUri,
followLocation: $initial?->followLocation ?? 5,
sleepTime: $initial?->sleepTime ?? 0,
logFile: $initial?->logFile,
exceptions: $initial?->exceptions ?? true,
cookies: $initial?->cookies,
history: $initial?->history ?? INF,
callback: $initial?->callback,
callbackException: $initial?->callbackException,
headers: $initial?->headers ?? self::DEFAULT_HEADERS,
context: $initial?->context ? clone $initial->context : null,
);
foreach ($options as $key => $value) {
$new->$key = match ($key) {
'headers' => array_replace(
self::normalizeHeaders($new->headers),
self::normalizeHeaders((array)$value),
),
'context' => HttpContext::make($value, $new->context),
default => $value
};
}
return $new;
}
return $options;
}
/**
* Get user defined options.
*
* @param string $name
*
* @return mixed
*/
public function __get(string $name): mixed
{
return $this->userDefined[$name] ?? null;
}
/**
* Set user defined option.
*
* @param string $name
* @param mixed $value
*
* @return void
*/
public function __set(string $name, mixed $value): void
{
$this->userDefined[$name] = $value;
}
/**
* Normalize headers.
*
* @param array $headers
*
* @return array
*/
public static function normalizeHeaders(array $headers): array
{
$final = [];
foreach ($headers as $name => $value) {
$name = static::normalizeHeaderName($name);
$final[$name] = (array)$value;
}
array_walk_recursive($final, fn(&$value) => $value = (string)$value);
unset($value);
return $final;
}
/**
* Normalize headers.
*
* @param string $name
*
* @return string
*/
public static function normalizeHeaderName(string $name): string
{
return ucwords(strtolower($name), ' -_');
}
}