1 | <?php |
||
28 | abstract class ResponseAbstract implements ResponseInterface{ |
||
29 | |||
30 | /** |
||
31 | * @var resource |
||
32 | */ |
||
33 | protected $curl; |
||
34 | |||
35 | /** |
||
36 | * @var \stdClass |
||
37 | */ |
||
38 | protected $curl_info; |
||
39 | |||
40 | /** |
||
41 | * @var \stdClass |
||
42 | */ |
||
43 | protected $response_headers; |
||
44 | |||
45 | /** |
||
46 | * @var \stdClass |
||
47 | */ |
||
48 | protected $response_error; |
||
49 | |||
50 | /** |
||
51 | * @var mixed |
||
52 | */ |
||
53 | protected $response_body; |
||
54 | |||
55 | /** |
||
56 | * Response constructor. |
||
57 | * |
||
58 | * @param resource $curl |
||
59 | * |
||
60 | * @throws \chillerlan\TinyCurl\ResponseException |
||
61 | */ |
||
62 | public function __construct($curl){ |
||
63 | |||
64 | if(!is_resource($curl)){ |
||
65 | throw new ResponseException('no cURL handle given'); |
||
66 | } |
||
67 | |||
68 | $this->curl = $curl; |
||
69 | $this->curl_info = new stdClass; |
||
70 | $this->response_error = new stdClass; |
||
71 | $this->response_headers = new stdClass; |
||
72 | |||
73 | $this->exec(); |
||
74 | } |
||
75 | |||
76 | /** @inheritdoc */ |
||
77 | public function __get($property){ |
||
78 | |||
79 | switch($property){ |
||
80 | case 'body': |
||
81 | return $this->getBody(); |
||
82 | case 'info': |
||
83 | return $this->curl_info; |
||
84 | case 'json': |
||
85 | return json_decode($this->response_body); |
||
86 | case 'json_array': |
||
87 | return json_decode($this->response_body, true); |
||
88 | case 'error': |
||
89 | return $this->response_error; |
||
90 | case 'headers': |
||
91 | return $this->response_headers; |
||
92 | case 'headers_array': |
||
93 | return $this->response_headers_array((array)$this->response_headers); |
||
94 | default: |
||
95 | return false; |
||
96 | } |
||
97 | |||
98 | } |
||
99 | |||
100 | /** |
||
101 | * executes the cURL call, fills self::$response_body and calls self::getInfo() |
||
102 | * |
||
103 | * @return void |
||
104 | */ |
||
105 | abstract protected function exec(); |
||
106 | |||
107 | /** |
||
108 | * @param resource $curl |
||
109 | * @param string $header_line |
||
110 | * |
||
111 | * @return int |
||
112 | * |
||
113 | * @link http://php.net/manual/function.curl-setopt.php CURLOPT_HEADERFUNCTION |
||
114 | */ |
||
115 | protected function headerLine(/** @noinspection PhpUnusedParameterInspection */$curl, $header_line){ |
||
131 | |||
132 | /** |
||
133 | * @return \stdClass |
||
134 | */ |
||
135 | protected function getBody(){ |
||
144 | |||
145 | /** |
||
146 | * @return void |
||
147 | */ |
||
148 | protected function getInfo(){ |
||
161 | |||
162 | /** |
||
163 | * @param array $response_headers |
||
164 | * |
||
165 | * @return array |
||
166 | */ |
||
167 | protected function response_headers_array(array $response_headers):array { |
||
176 | |||
177 | } |
||
178 |