HttpOptions.php 5.8 KB

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