|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Manticoresearch; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Manticore response object |
|
8
|
|
|
* Stores result array, time and errors |
|
9
|
|
|
* @category ManticoreSearch |
|
10
|
|
|
* @package ManticoreSearch |
|
11
|
|
|
* @author Adrian Nuta <[email protected]> |
|
12
|
|
|
* @link https://manticoresearch.com |
|
13
|
|
|
*/ |
|
14
|
|
|
use Manticoresearch\Exceptions\RuntimeException; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class Response |
|
18
|
|
|
* @package Manticoresearch |
|
19
|
|
|
*/ |
|
20
|
|
|
class Response |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* execution time to get the response |
|
24
|
|
|
* @var integer|float |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $time; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* raw response as string |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $string; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* information about request |
|
36
|
|
|
* @var array |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $transportInfo; |
|
39
|
|
|
|
|
40
|
|
|
protected $status; |
|
41
|
|
|
/** |
|
42
|
|
|
* response as array |
|
43
|
|
|
* @var array |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $response; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* additional params as array |
|
49
|
|
|
* @var array |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $params; |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
public function __construct($responseString, $status = null, $params = []) |
|
55
|
|
|
{ |
|
56
|
|
|
if (is_array($responseString)) { |
|
57
|
|
|
$this->response = $responseString; |
|
58
|
|
|
} else { |
|
59
|
|
|
$this->string = $responseString; |
|
60
|
|
|
} |
|
61
|
|
|
$this->status = $status; |
|
62
|
|
|
$this->params = $params; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/* |
|
66
|
|
|
* Return response |
|
67
|
|
|
* @return array |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getResponse() |
|
70
|
|
|
{ |
|
71
|
|
|
if (null === $this->response) { |
|
72
|
|
|
$this->response = json_decode($this->string, true); |
|
73
|
|
|
if (json_last_error() !== JSON_ERROR_NONE) { |
|
74
|
|
|
if (json_last_error() === JSON_ERROR_UTF8 && $this->stripBadUtf8()) { |
|
75
|
|
|
$this->response = json_decode(preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $this->string), true); |
|
76
|
|
|
} else { |
|
77
|
|
|
throw new RuntimeException('fatal error while trying to decode JSON response'); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if (empty($this->response)) { |
|
82
|
|
|
$this->response = []; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
return $this->response; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* check if strip_bad_utf8 as been set to true |
|
90
|
|
|
* @return boolean |
|
91
|
|
|
*/ |
|
92
|
|
|
private function stripBadUtf8() |
|
93
|
|
|
{ |
|
94
|
|
|
return !empty($this->transportInfo['body']) && !empty($this->transportInfo['body']['strip_bad_utf8']); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/* |
|
98
|
|
|
* Check whenever response has error |
|
99
|
|
|
* @return bool |
|
100
|
|
|
*/ |
|
101
|
|
|
public function hasError() |
|
102
|
|
|
{ |
|
103
|
|
|
$response = $this->getResponse(); |
|
104
|
|
|
if (is_array($response)) { |
|
|
|
|
|
|
105
|
|
|
foreach ($response as $r) { |
|
106
|
|
|
if (isset($r['error']) && $r['error'] !== '') { |
|
107
|
|
|
return true; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
return (isset($response['error']) && $response['error'] !== '') || |
|
112
|
|
|
(isset($response['errors']) && $response['errors'] !== false); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/* |
|
116
|
|
|
* Return error |
|
117
|
|
|
* @return false|string |
|
118
|
|
|
*/ |
|
119
|
|
|
public function getError() |
|
120
|
|
|
{ |
|
121
|
|
|
$response = $this->getResponse(); |
|
122
|
|
|
if (isset($response['error'])) { |
|
123
|
|
|
return json_encode($response['error'], true); |
|
|
|
|
|
|
124
|
|
|
} elseif (isset($response['errors'])) { |
|
125
|
|
|
return json_encode($response['errors'], true); |
|
126
|
|
|
} elseif (is_array($response)) { |
|
127
|
|
|
$errors = ""; |
|
128
|
|
|
foreach ($response as $r) { |
|
129
|
|
|
if (isset($r['error']) && $r['error'] !== '') { |
|
130
|
|
|
$errors .= json_encode($r['error'], true); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
return $errors; |
|
134
|
|
|
} else { |
|
135
|
|
|
return ''; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/* |
|
140
|
|
|
* set execution time |
|
141
|
|
|
* @param int|float $time |
|
142
|
|
|
* @return $this |
|
143
|
|
|
*/ |
|
144
|
|
|
public function setTime($time) |
|
145
|
|
|
{ |
|
146
|
|
|
$this->time = $time; |
|
147
|
|
|
return $this; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/* |
|
151
|
|
|
* returns execution time |
|
152
|
|
|
* @return mixed |
|
153
|
|
|
*/ |
|
154
|
|
|
public function getTime() |
|
155
|
|
|
{ |
|
156
|
|
|
return $this->time; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* set request info |
|
161
|
|
|
* @param array $info |
|
162
|
|
|
* @return $this |
|
163
|
|
|
*/ |
|
164
|
|
|
public function setTransportInfo($info) |
|
165
|
|
|
{ |
|
166
|
|
|
$this->transportInfo = $info; |
|
167
|
|
|
return $this; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* get request info |
|
172
|
|
|
* @return array |
|
173
|
|
|
*/ |
|
174
|
|
|
public function getTransportInfo() |
|
175
|
|
|
{ |
|
176
|
|
|
return $this->transportInfo; |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|