Passed
Push — master ( 7760cd...612ec5 )
by
unknown
05:04
created

Response::stripBadUtf8()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
        return (isset($response['error']) && $response['error'] !== '') ||
105
            (isset($response['errors']) && $response['errors'] !== false);
106
    }
107
108
    /*
109
     * Return error
110
     * @return false|string
111
     */
112
    public function getError()
113
    {
114
        $response = $this->getResponse();
115
        if (isset($response['error'])) {
116
            return json_encode($response['error'], true);
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type integer expected by parameter $flags of json_encode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

116
            return json_encode($response['error'], /** @scrutinizer ignore-type */ true);
Loading history...
117
        } elseif (isset($response['errors'])) {
118
            return json_encode($response['errors'], true);
119
        } else {
120
            return '';
121
        }
122
    }
123
124
    /*
125
     * set execution time
126
     * @param int|float $time
127
     * @return $this
128
     */
129
    public function setTime($time)
130
    {
131
        $this->time = $time;
132
        return $this;
133
    }
134
135
    /*
136
     * returns execution time
137
     * @return mixed
138
     */
139
    public function getTime()
140
    {
141
        return $this->time;
142
    }
143
144
    /**
145
     *  set request info
146
     * @param array $info
147
     * @return $this
148
     */
149
    public function setTransportInfo($info)
150
    {
151
        $this->transportInfo = $info;
152
        return $this;
153
    }
154
155
    /**
156
     * get request info
157
     * @return array
158
     */
159
    public function getTransportInfo()
160
    {
161
        return $this->transportInfo;
162
    }
163
}
164