1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Palmtree\Curl; |
4
|
|
|
|
5
|
|
|
use Palmtree\Curl\Exception\InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
class CurlOpts implements \ArrayAccess, \IteratorAggregate |
8
|
|
|
{ |
9
|
|
|
/** @var array */ |
10
|
|
|
public static $defaults = [ |
11
|
|
|
CURLOPT_RETURNTRANSFER => true, |
12
|
|
|
CURLOPT_FOLLOWLOCATION => true, |
13
|
|
|
CURLOPT_AUTOREFERER => true, |
14
|
|
|
CURLOPT_USERAGENT => 'Palmtree\Curl', |
15
|
|
|
CURLOPT_HEADER => true, |
16
|
|
|
]; |
17
|
|
|
/** @var array */ |
18
|
|
|
private $opts = []; |
19
|
|
|
|
20
|
17 |
|
public function __construct(array $opts = []) |
21
|
|
|
{ |
22
|
17 |
|
foreach (\array_replace(self::$defaults, $opts) as $key => $value) { |
23
|
17 |
|
$this->set($key, $value); |
24
|
|
|
} |
25
|
17 |
|
} |
26
|
|
|
|
27
|
12 |
|
public function toArray(): array |
28
|
|
|
{ |
29
|
12 |
|
return $this->opts; |
30
|
|
|
} |
31
|
|
|
|
32
|
17 |
|
public function set(int $key, $value): self |
33
|
|
|
{ |
34
|
17 |
|
if ($key === CURLOPT_HEADER && !$value) { |
35
|
1 |
|
throw new InvalidArgumentException('CURLOPT_HEADER cannot be set to false'); |
36
|
|
|
} |
37
|
|
|
|
38
|
17 |
|
$this->opts[$key] = $value; |
39
|
|
|
|
40
|
17 |
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
2 |
|
public function get(int $key) |
44
|
|
|
{ |
45
|
2 |
|
return $this->opts[$key] ?? null; |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
public function offsetExists($offset) |
49
|
|
|
{ |
50
|
1 |
|
return isset($this->opts[$offset]); |
51
|
|
|
} |
52
|
|
|
|
53
|
1 |
|
public function offsetGet($offset) |
54
|
|
|
{ |
55
|
1 |
|
return $this->get($offset); |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
public function offsetSet($offset, $value) |
59
|
|
|
{ |
60
|
1 |
|
$this->set($offset, $value); |
61
|
1 |
|
} |
62
|
|
|
|
63
|
1 |
|
public function offsetUnset($offset) |
64
|
|
|
{ |
65
|
1 |
|
unset($this->opts[$offset]); |
66
|
1 |
|
} |
67
|
|
|
|
68
|
1 |
|
public function getIterator() |
69
|
|
|
{ |
70
|
1 |
|
return new \ArrayIterator($this->opts); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|