HttpOptions.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. /**
  22. * @var mixed[]
  23. */
  24. private $options = [];
  25. public function toArray()
  26. {
  27. return $this->options;
  28. }
  29. /**
  30. * @return $this
  31. * @param string $user
  32. * @param string $password
  33. */
  34. public function setAuthBasic($user, $password = '')
  35. {
  36. $this->options['auth_basic'] = $user;
  37. if ('' !== $password) {
  38. $this->options['auth_basic'] .= ':'.$password;
  39. }
  40. return $this;
  41. }
  42. /**
  43. * @return $this
  44. * @param string $token
  45. */
  46. public function setAuthBearer($token)
  47. {
  48. $this->options['auth_bearer'] = $token;
  49. return $this;
  50. }
  51. /**
  52. * @return $this
  53. * @param mixed[] $query
  54. */
  55. public function setQuery($query)
  56. {
  57. $this->options['query'] = $query;
  58. return $this;
  59. }
  60. /**
  61. * @return $this
  62. * @param mixed[] $headers
  63. */
  64. public function setHeaders($headers)
  65. {
  66. $this->options['headers'] = $headers;
  67. return $this;
  68. }
  69. /**
  70. * @param mixed $body
  71. *
  72. * @return $this
  73. */
  74. public function setBody($body)
  75. {
  76. $this->options['body'] = $body;
  77. return $this;
  78. }
  79. /**
  80. * @return $this
  81. * @param mixed $json
  82. */
  83. public function setJson($json)
  84. {
  85. $this->options['json'] = $json;
  86. return $this;
  87. }
  88. /**
  89. * @return $this
  90. * @param mixed $data
  91. */
  92. public function setUserData($data)
  93. {
  94. $this->options['user_data'] = $data;
  95. return $this;
  96. }
  97. /**
  98. * @return $this
  99. * @param int $max
  100. */
  101. public function setMaxRedirects($max)
  102. {
  103. $this->options['max_redirects'] = $max;
  104. return $this;
  105. }
  106. /**
  107. * @return $this
  108. * @param string $version
  109. */
  110. public function setHttpVersion($version)
  111. {
  112. $this->options['http_version'] = $version;
  113. return $this;
  114. }
  115. /**
  116. * @return $this
  117. * @param string $uri
  118. */
  119. public function setBaseUri($uri)
  120. {
  121. $this->options['base_uri'] = $uri;
  122. return $this;
  123. }
  124. /**
  125. * @return $this
  126. * @param mixed[] $vars
  127. */
  128. public function setVars($vars)
  129. {
  130. $this->options['vars'] = $vars;
  131. return $this;
  132. }
  133. /**
  134. * @return $this
  135. * @param bool $buffer
  136. */
  137. public function buffer($buffer)
  138. {
  139. $this->options['buffer'] = $buffer;
  140. return $this;
  141. }
  142. /**
  143. * @return $this
  144. * @param callable $callback
  145. */
  146. public function setOnProgress($callback)
  147. {
  148. $this->options['on_progress'] = $callback;
  149. return $this;
  150. }
  151. /**
  152. * @return $this
  153. * @param mixed[] $hostIps
  154. */
  155. public function resolve($hostIps)
  156. {
  157. $this->options['resolve'] = $hostIps;
  158. return $this;
  159. }
  160. /**
  161. * @return $this
  162. * @param string $proxy
  163. */
  164. public function setProxy($proxy)
  165. {
  166. $this->options['proxy'] = $proxy;
  167. return $this;
  168. }
  169. /**
  170. * @return $this
  171. * @param string $noProxy
  172. */
  173. public function setNoProxy($noProxy)
  174. {
  175. $this->options['no_proxy'] = $noProxy;
  176. return $this;
  177. }
  178. /**
  179. * @return $this
  180. * @param float $timeout
  181. */
  182. public function setTimeout($timeout)
  183. {
  184. $this->options['timeout'] = $timeout;
  185. return $this;
  186. }
  187. /**
  188. * @return $this
  189. * @param float $maxDuration
  190. */
  191. public function setMaxDuration($maxDuration)
  192. {
  193. $this->options['max_duration'] = $maxDuration;
  194. return $this;
  195. }
  196. /**
  197. * @return $this
  198. * @param string $bindto
  199. */
  200. public function bindTo($bindto)
  201. {
  202. $this->options['bindto'] = $bindto;
  203. return $this;
  204. }
  205. /**
  206. * @return $this
  207. * @param bool $verify
  208. */
  209. public function verifyPeer($verify)
  210. {
  211. $this->options['verify_peer'] = $verify;
  212. return $this;
  213. }
  214. /**
  215. * @return $this
  216. * @param bool $verify
  217. */
  218. public function verifyHost($verify)
  219. {
  220. $this->options['verify_host'] = $verify;
  221. return $this;
  222. }
  223. /**
  224. * @return $this
  225. * @param string $cafile
  226. */
  227. public function setCaFile($cafile)
  228. {
  229. $this->options['cafile'] = $cafile;
  230. return $this;
  231. }
  232. /**
  233. * @return $this
  234. * @param string $capath
  235. */
  236. public function setCaPath($capath)
  237. {
  238. $this->options['capath'] = $capath;
  239. return $this;
  240. }
  241. /**
  242. * @return $this
  243. * @param string $cert
  244. */
  245. public function setLocalCert($cert)
  246. {
  247. $this->options['local_cert'] = $cert;
  248. return $this;
  249. }
  250. /**
  251. * @return $this
  252. * @param string $pk
  253. */
  254. public function setLocalPk($pk)
  255. {
  256. $this->options['local_pk'] = $pk;
  257. return $this;
  258. }
  259. /**
  260. * @return $this
  261. * @param string $passphrase
  262. */
  263. public function setPassphrase($passphrase)
  264. {
  265. $this->options['passphrase'] = $passphrase;
  266. return $this;
  267. }
  268. /**
  269. * @return $this
  270. * @param string $ciphers
  271. */
  272. public function setCiphers($ciphers)
  273. {
  274. $this->options['ciphers'] = $ciphers;
  275. return $this;
  276. }
  277. /**
  278. * @return $this
  279. * @param string|mixed[] $fingerprint
  280. */
  281. public function setPeerFingerprint($fingerprint)
  282. {
  283. $this->options['peer_fingerprint'] = $fingerprint;
  284. return $this;
  285. }
  286. /**
  287. * @return $this
  288. * @param bool $capture
  289. */
  290. public function capturePeerCertChain($capture)
  291. {
  292. $this->options['capture_peer_cert_chain'] = $capture;
  293. return $this;
  294. }
  295. /**
  296. * @return $this
  297. * @param mixed $value
  298. * @param string $name
  299. */
  300. public function setExtra($name, $value)
  301. {
  302. $this->options['extra'][$name] = $value;
  303. return $this;
  304. }
  305. }