1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the jyggen/curl library |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @copyright Copyright (c) Jonas Stendahl <[email protected]> |
9
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
10
|
|
|
* @link https://jyggen.com/projects/jyggen-curl Documentation |
11
|
|
|
* @link https://packagist.org/packages/jyggen/curl Packagist |
12
|
|
|
* @link https://github.com/jyggen/curl GitHub |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Jyggen\Curl; |
16
|
|
|
|
17
|
|
|
use Jyggen\Curl\RequestInterface; |
18
|
|
|
use Symfony\Component\HttpFoundation\HeaderBag as SymfonyHeaderBag; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* A container for HTTP headers. |
22
|
|
|
*/ |
23
|
|
|
class HeaderBag extends SymfonyHeaderBag |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* The request this bag belongs to. |
27
|
|
|
* |
28
|
|
|
* @var RequestInterface |
29
|
|
|
*/ |
30
|
|
|
protected $request; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Constructs a `HeaderBag` instance. |
34
|
|
|
* |
35
|
|
|
* @param array $headers |
36
|
|
|
* @param RequestInterface $request |
37
|
|
|
*/ |
38
|
|
|
public function __construct(array $headers, RequestInterface $request) |
39
|
|
|
{ |
40
|
|
|
$this->request = $request; |
41
|
|
|
parent::__construct($headers); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Removes a header. |
46
|
|
|
* |
47
|
|
|
* @param string $key |
48
|
|
|
*/ |
49
|
|
|
public function remove($key) |
50
|
|
|
{ |
51
|
|
|
parent::remove($key); |
52
|
|
|
$this->updateRequest(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Sets a header by name. |
57
|
|
|
* |
58
|
|
|
* @param string $key |
59
|
|
|
* @param string|array $values |
60
|
|
|
* @param bool $replace |
61
|
|
|
*/ |
62
|
|
|
public function set($key, $values, $replace = true) |
63
|
|
|
{ |
64
|
|
|
parent::set($key, $values, $replace); |
65
|
|
|
$this->updateRequest(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Updates the headers in the associated request. |
70
|
|
|
*/ |
71
|
|
|
protected function updateRequest() |
72
|
|
|
{ |
73
|
|
|
$headers = []; |
74
|
|
|
foreach ($this->all() as $key => $values) { |
75
|
|
|
foreach ($values as $value) { |
76
|
|
|
$headers[] = $key.': '.$value; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$this->request->setOption(CURLOPT_HTTPHEADER, $headers); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|