1
|
|
|
<?php namespace BestServedCold\PhalueObjects\Access;
|
2
|
|
|
|
3
|
|
|
use BestServedCold\PhalueObjects\Utility\Native\Constant;
|
4
|
|
|
use BestServedCold\PhalueObjects\ValueObject;
|
5
|
|
|
|
6
|
|
|
final class Curl extends ValueObject
|
7
|
|
|
{
|
8
|
|
|
public $connectTimeout = CURLOPT_CONNECTTIMEOUT;
|
9
|
|
|
public $followRedirects = CURLOPT_FOLLOWLOCATION;
|
10
|
|
|
public $headers = CURLOPT_HEADER;
|
11
|
|
|
public $maxRedirects = CURLOPT_MAXREDIRS;
|
12
|
|
|
public $noBody = CURLOPT_NOBODY;
|
13
|
|
|
public $returnTransfer = CURLOPT_RETURNTRANSFER;
|
14
|
|
|
public $timeout = CURLOPT_TIMEOUT;
|
15
|
|
|
|
16
|
|
|
private $options = [];
|
17
|
|
|
|
18
|
|
|
public function __construct($value)
|
19
|
|
|
{
|
20
|
|
|
parent::__construct($value);
|
21
|
|
|
$this->init()
|
22
|
|
|
->setOption($this->headers)
|
23
|
|
|
->setOption($this->timeout)
|
24
|
|
|
->setOption($this->connectTimeout);
|
25
|
|
|
}
|
26
|
|
|
|
27
|
|
|
public function exec()
|
28
|
|
|
{
|
29
|
|
|
return curl_exec($this->getValue());
|
30
|
|
|
}
|
31
|
|
|
|
32
|
|
|
private function init()
|
33
|
|
|
{
|
34
|
|
|
if (! $this->value = curl_init($this->getValue())) {
|
35
|
|
|
throw new \Exception;
|
36
|
|
|
}
|
37
|
|
|
|
38
|
|
|
return $this;
|
39
|
|
|
}
|
40
|
|
|
|
41
|
|
|
public function getOptions()
|
42
|
|
|
{
|
43
|
|
|
return $this->options;
|
44
|
|
|
}
|
45
|
|
|
|
46
|
|
|
public function getOption($option)
|
|
|
|
|
47
|
|
|
{
|
48
|
|
|
return isset($this->options[$option]) ? $this->options[$option] : null;
|
49
|
|
|
}
|
50
|
|
|
|
51
|
|
|
/**
|
52
|
|
|
* @param $option
|
53
|
|
|
* @param bool|true $value
|
54
|
|
|
* @return $this
|
55
|
|
|
*/
|
56
|
|
|
public function setOption($option, $value = true)
|
57
|
|
|
{
|
58
|
|
|
if (curl_setopt($this->getValue(), $option, $value)) {
|
59
|
|
|
$this->options[Constant::init()->curl($option)] = $value;
|
|
|
|
|
60
|
|
|
}
|
61
|
|
|
|
62
|
|
|
return $this;
|
63
|
|
|
}
|
64
|
|
|
|
65
|
|
|
public function errorNumber()
|
66
|
|
|
{
|
67
|
|
|
return curl_errno($this->getValue());
|
68
|
|
|
}
|
69
|
|
|
|
70
|
|
|
public function error()
|
71
|
|
|
{
|
72
|
|
|
return curl_error($this->getValue());
|
73
|
|
|
}
|
74
|
|
|
|
75
|
|
|
public function info()
|
76
|
|
|
{
|
77
|
|
|
return curl_getinfo($this->getValue());
|
78
|
|
|
}
|
79
|
|
|
|
80
|
|
|
public function __destruct()
|
81
|
|
|
{
|
82
|
|
|
curl_close($this->value);
|
83
|
|
|
}
|
84
|
|
|
}
|
85
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.