| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <[email protected]> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Component\HttpClient; |
| 13 |
|
| 14 |
use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 15 |
|
| 16 |
/** |
| 17 |
* A helper providing autocompletion for available options. |
| 18 |
* |
| 19 |
* @see HttpClientInterface for a description of each options. |
| 20 |
* |
| 21 |
* @author Nicolas Grekas <[email protected]> |
| 22 |
*/ |
| 23 |
class HttpOptions |
| 24 |
{ |
| 25 |
private $options = []; |
| 26 |
|
| 27 |
public function toArray(): array |
| 28 |
{ |
| 29 |
return $this->options; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* @return $this |
| 34 |
*/ |
| 35 |
public function setAuthBasic(string $user, string $password = '') |
| 36 |
{ |
| 37 |
$this->options['auth_basic'] = $user; |
| 38 |
|
| 39 |
if ('' !== $password) { |
| 40 |
$this->options['auth_basic'] .= ':'.$password; |
| 41 |
} |
| 42 |
|
| 43 |
return $this; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* @return $this |
| 48 |
*/ |
| 49 |
public function setAuthBearer(string $token) |
| 50 |
{ |
| 51 |
$this->options['auth_bearer'] = $token; |
| 52 |
|
| 53 |
return $this; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* @return $this |
| 58 |
*/ |
| 59 |
public function setQuery(array $query) |
| 60 |
{ |
| 61 |
$this->options['query'] = $query; |
| 62 |
|
| 63 |
return $this; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* @return $this |
| 68 |
*/ |
| 69 |
public function setHeaders(iterable $headers) |
| 70 |
{ |
| 71 |
$this->options['headers'] = $headers; |
| 72 |
|
| 73 |
return $this; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* @param array|string|resource|\Traversable|\Closure $body |
| 78 |
* |
| 79 |
* @return $this |
| 80 |
*/ |
| 81 |
public function setBody($body) |
| 82 |
{ |
| 83 |
$this->options['body'] = $body; |
| 84 |
|
| 85 |
return $this; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* @param mixed $json |
| 90 |
* |
| 91 |
* @return $this |
| 92 |
*/ |
| 93 |
public function setJson($json) |
| 94 |
{ |
| 95 |
$this->options['json'] = $json; |
| 96 |
|
| 97 |
return $this; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* @return $this |
| 102 |
*/ |
| 103 |
public function setUserData($data) |
| 104 |
{ |
| 105 |
$this->options['user_data'] = $data; |
| 106 |
|
| 107 |
return $this; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* @return $this |
| 112 |
*/ |
| 113 |
public function setMaxRedirects(int $max) |
| 114 |
{ |
| 115 |
$this->options['max_redirects'] = $max; |
| 116 |
|
| 117 |
return $this; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* @return $this |
| 122 |
*/ |
| 123 |
public function setHttpVersion(string $version) |
| 124 |
{ |
| 125 |
$this->options['http_version'] = $version; |
| 126 |
|
| 127 |
return $this; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* @return $this |
| 132 |
*/ |
| 133 |
public function setBaseUri(string $uri) |
| 134 |
{ |
| 135 |
$this->options['base_uri'] = $uri; |
| 136 |
|
| 137 |
return $this; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* @return $this |
| 142 |
*/ |
| 143 |
public function buffer(bool $buffer) |
| 144 |
{ |
| 145 |
$this->options['buffer'] = $buffer; |
| 146 |
|
| 147 |
return $this; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* @return $this |
| 152 |
*/ |
| 153 |
public function setOnProgress(callable $callback) |
| 154 |
{ |
| 155 |
$this->options['on_progress'] = $callback; |
| 156 |
|
| 157 |
return $this; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* @return $this |
| 162 |
*/ |
| 163 |
public function resolve(array $hostIps) |
| 164 |
{ |
| 165 |
$this->options['resolve'] = $hostIps; |
| 166 |
|
| 167 |
return $this; |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* @return $this |
| 172 |
*/ |
| 173 |
public function setProxy(string $proxy) |
| 174 |
{ |
| 175 |
$this->options['proxy'] = $proxy; |
| 176 |
|
| 177 |
return $this; |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* @return $this |
| 182 |
*/ |
| 183 |
public function setNoProxy(string $noProxy) |
| 184 |
{ |
| 185 |
$this->options['no_proxy'] = $noProxy; |
| 186 |
|
| 187 |
return $this; |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* @return $this |
| 192 |
*/ |
| 193 |
public function setTimeout(float $timeout) |
| 194 |
{ |
| 195 |
$this->options['timeout'] = $timeout; |
| 196 |
|
| 197 |
return $this; |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* @return $this |
| 202 |
*/ |
| 203 |
public function setMaxDuration(float $maxDuration) |
| 204 |
{ |
| 205 |
$this->options['max_duration'] = $maxDuration; |
| 206 |
|
| 207 |
return $this; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* @return $this |
| 212 |
*/ |
| 213 |
public function bindTo(string $bindto) |
| 214 |
{ |
| 215 |
$this->options['bindto'] = $bindto; |
| 216 |
|
| 217 |
return $this; |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* @return $this |
| 222 |
*/ |
| 223 |
public function verifyPeer(bool $verify) |
| 224 |
{ |
| 225 |
$this->options['verify_peer'] = $verify; |
| 226 |
|
| 227 |
return $this; |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* @return $this |
| 232 |
*/ |
| 233 |
public function verifyHost(bool $verify) |
| 234 |
{ |
| 235 |
$this->options['verify_host'] = $verify; |
| 236 |
|
| 237 |
return $this; |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* @return $this |
| 242 |
*/ |
| 243 |
public function setCaFile(string $cafile) |
| 244 |
{ |
| 245 |
$this->options['cafile'] = $cafile; |
| 246 |
|
| 247 |
return $this; |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* @return $this |
| 252 |
*/ |
| 253 |
public function setCaPath(string $capath) |
| 254 |
{ |
| 255 |
$this->options['capath'] = $capath; |
| 256 |
|
| 257 |
return $this; |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* @return $this |
| 262 |
*/ |
| 263 |
public function setLocalCert(string $cert) |
| 264 |
{ |
| 265 |
$this->options['local_cert'] = $cert; |
| 266 |
|
| 267 |
return $this; |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 271 |
* @return $this |
| 272 |
*/ |
| 273 |
public function setLocalPk(string $pk) |
| 274 |
{ |
| 275 |
$this->options['local_pk'] = $pk; |
| 276 |
|
| 277 |
return $this; |
| 278 |
} |
| 279 |
|
| 280 |
/** |
| 281 |
* @return $this |
| 282 |
*/ |
| 283 |
public function setPassphrase(string $passphrase) |
| 284 |
{ |
| 285 |
$this->options['passphrase'] = $passphrase; |
| 286 |
|
| 287 |
return $this; |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* @return $this |
| 292 |
*/ |
| 293 |
public function setCiphers(string $ciphers) |
| 294 |
{ |
| 295 |
$this->options['ciphers'] = $ciphers; |
| 296 |
|
| 297 |
return $this; |
| 298 |
} |
| 299 |
|
| 300 |
/** |
| 301 |
* @param string|array $fingerprint |
| 302 |
* |
| 303 |
* @return $this |
| 304 |
*/ |
| 305 |
public function setPeerFingerprint($fingerprint) |
| 306 |
{ |
| 307 |
$this->options['peer_fingerprint'] = $fingerprint; |
| 308 |
|
| 309 |
return $this; |
| 310 |
} |
| 311 |
|
| 312 |
/** |
| 313 |
* @return $this |
| 314 |
*/ |
| 315 |
public function capturePeerCertChain(bool $capture) |
| 316 |
{ |
| 317 |
$this->options['capture_peer_cert_chain'] = $capture; |
| 318 |
|
| 319 |
return $this; |
| 320 |
} |
| 321 |
|
| 322 |
/** |
| 323 |
* @return $this |
| 324 |
*/ |
| 325 |
public function setExtra(string $name, $value) |
| 326 |
{ |
| 327 |
$this->options['extra'][$name] = $value; |
| 328 |
|
| 329 |
return $this; |
| 330 |
} |
| 331 |
} |
| 332 |
|