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
|
|
|
private $constants; |
|
|
|
|
18
|
|
|
|
19
|
|
|
public function __construct($value) |
20
|
|
|
{ |
21
|
|
|
parent::__construct($value); |
22
|
|
|
// $this->constants = new Constant(); |
|
|
|
|
23
|
|
|
$this->init() |
24
|
|
|
->setOption($this->headers) |
25
|
|
|
->setOption($this->timeout) |
26
|
|
|
->setOption($this->connectTimeout); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function exec() |
30
|
|
|
{ |
31
|
|
|
return curl_exec($this->getValue()); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
private function init() |
35
|
|
|
{ |
36
|
|
|
if (! $this->value = curl_init($this->getValue())) { |
37
|
|
|
throw new \Exception; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function getOptions() |
44
|
|
|
{ |
45
|
|
|
return $this->options; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function getOption($option) |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
return isset($this->options[$option]) ? $this->options[$option] : null; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param $option |
55
|
|
|
* @param bool|true $value |
56
|
|
|
* @return $this |
57
|
|
|
*/ |
58
|
|
|
public function setOption($option, $value = true) |
59
|
|
|
{ |
60
|
|
|
if (curl_setopt($this->getValue(), $option, $value)) { |
61
|
|
|
$this->options[Constant::init()->curl($option)] = $value; |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function errorNumber() |
68
|
|
|
{ |
69
|
|
|
return curl_errno($this->getValue()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function error() |
73
|
|
|
{ |
74
|
|
|
return curl_error($this->getValue()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function info() |
78
|
|
|
{ |
79
|
|
|
return curl_getinfo($this->getValue()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function __destruct() |
83
|
|
|
{ |
84
|
|
|
curl_close($this->value); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.