HttpOptions.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 $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 = '')
  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)
  41. {
  42. $this->options['auth_bearer'] = $token;
  43. return $this;
  44. }
  45. /**
  46. * @return $this
  47. */
  48. public function setQuery(array $query)
  49. {
  50. $this->options['query'] = $query;
  51. return $this;
  52. }
  53. /**
  54. * @return $this
  55. */
  56. public function setHeaders(iterable $headers)
  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($body)
  67. {
  68. $this->options['body'] = $body;
  69. return $this;
  70. }
  71. /**
  72. * @param mixed $json
  73. *
  74. * @return $this
  75. */
  76. public function setJson($json)
  77. {
  78. $this->options['json'] = $json;
  79. return $this;
  80. }
  81. /**
  82. * @return $this
  83. */
  84. public function setUserData($data)
  85. {
  86. $this->options['user_data'] = $data;
  87. return $this;
  88. }
  89. /**
  90. * @return $this
  91. */
  92. public function setMaxRedirects(int $max)
  93. {
  94. $this->options['max_redirects'] = $max;
  95. return $this;
  96. }
  97. /**
  98. * @return $this
  99. */
  100. public function setHttpVersion(string $version)
  101. {
  102. $this->options['http_version'] = $version;
  103. return $this;
  104. }
  105. /**
  106. * @return $this
  107. */
  108. public function setBaseUri(string $uri)
  109. {
  110. $this->options['base_uri'] = $uri;
  111. return $this;
  112. }
  113. /**
  114. * @return $this
  115. */
  116. public function buffer(bool $buffer)
  117. {
  118. $this->options['buffer'] = $buffer;
  119. return $this;
  120. }
  121. /**
  122. * @return $this
  123. */
  124. public function setOnProgress(callable $callback)
  125. {
  126. $this->options['on_progress'] = $callback;
  127. return $this;
  128. }
  129. /**
  130. * @return $this
  131. */
  132. public function resolve(array $hostIps)
  133. {
  134. $this->options['resolve'] = $hostIps;
  135. return $this;
  136. }
  137. /**
  138. * @return $this
  139. */
  140. public function setProxy(string $proxy)
  141. {
  142. $this->options['proxy'] = $proxy;
  143. return $this;
  144. }
  145. /**
  146. * @return $this
  147. */
  148. public function setNoProxy(string $noProxy)
  149. {
  150. $this->options['no_proxy'] = $noProxy;
  151. return $this;
  152. }
  153. /**
  154. * @return $this
  155. */
  156. public function setTimeout(float $timeout)
  157. {
  158. $this->options['timeout'] = $timeout;
  159. return $this;
  160. }
  161. /**
  162. * @return $this
  163. */
  164. public function bindTo(string $bindto)
  165. {
  166. $this->options['bindto'] = $bindto;
  167. return $this;
  168. }
  169. /**
  170. * @return $this
  171. */
  172. public function verifyPeer(bool $verify)
  173. {
  174. $this->options['verify_peer'] = $verify;
  175. return $this;
  176. }
  177. /**
  178. * @return $this
  179. */
  180. public function verifyHost(bool $verify)
  181. {
  182. $this->options['verify_host'] = $verify;
  183. return $this;
  184. }
  185. /**
  186. * @return $this
  187. */
  188. public function setCaFile(string $cafile)
  189. {
  190. $this->options['cafile'] = $cafile;
  191. return $this;
  192. }
  193. /**
  194. * @return $this
  195. */
  196. public function setCaPath(string $capath)
  197. {
  198. $this->options['capath'] = $capath;
  199. return $this;
  200. }
  201. /**
  202. * @return $this
  203. */
  204. public function setLocalCert(string $cert)
  205. {
  206. $this->options['local_cert'] = $cert;
  207. return $this;
  208. }
  209. /**
  210. * @return $this
  211. */
  212. public function setLocalPk(string $pk)
  213. {
  214. $this->options['local_pk'] = $pk;
  215. return $this;
  216. }
  217. /**
  218. * @return $this
  219. */
  220. public function setPassphrase(string $passphrase)
  221. {
  222. $this->options['passphrase'] = $passphrase;
  223. return $this;
  224. }
  225. /**
  226. * @return $this
  227. */
  228. public function setCiphers(string $ciphers)
  229. {
  230. $this->options['ciphers'] = $ciphers;
  231. return $this;
  232. }
  233. /**
  234. * @param string|array $fingerprint
  235. *
  236. * @return $this
  237. */
  238. public function setPeerFingerprint($fingerprint)
  239. {
  240. $this->options['peer_fingerprint'] = $fingerprint;
  241. return $this;
  242. }
  243. /**
  244. * @return $this
  245. */
  246. public function capturePeerCertChain(bool $capture)
  247. {
  248. $this->options['capture_peer_cert_chain'] = $capture;
  249. return $this;
  250. }
  251. /**
  252. * @return $this
  253. */
  254. public function setExtra(string $name, $value)
  255. {
  256. $this->options['extra'][$name] = $value;
  257. return $this;
  258. }
  259. }