1 | <?php |
||
2 | |||
3 | namespace AlibabaCloud\Client\Result; |
||
4 | |||
5 | use Countable; |
||
6 | use Exception; |
||
7 | use ArrayAccess; |
||
8 | use IteratorAggregate; |
||
9 | use InvalidArgumentException; |
||
10 | use GuzzleHttp\Psr7\Response; |
||
11 | use Psr\Http\Message\ResponseInterface; |
||
12 | use AlibabaCloud\Client\Request\Request; |
||
13 | use AlibabaCloud\Client\Traits\HasDataTrait; |
||
14 | |||
15 | /** |
||
16 | * Result from Alibaba Cloud |
||
17 | * |
||
18 | * @property string|null RequestId |
||
19 | * |
||
20 | * @package AlibabaCloud\Client\Result |
||
21 | */ |
||
22 | class Result extends Response implements ArrayAccess, IteratorAggregate, Countable |
||
23 | { |
||
24 | use HasDataTrait; |
||
25 | |||
26 | /** |
||
27 | * Instance of the request. |
||
28 | * |
||
29 | * @var Request |
||
30 | */ |
||
31 | protected $request; |
||
32 | |||
33 | /** |
||
34 | * Result constructor. |
||
35 | * |
||
36 | * @param ResponseInterface $response |
||
37 | * @param Request $request |
||
38 | */ |
||
39 | 74 | public function __construct(ResponseInterface $response, Request $request = null) |
|
40 | { |
||
41 | 74 | parent::__construct( |
|
42 | 74 | $response->getStatusCode(), |
|
43 | 74 | $response->getHeaders(), |
|
44 | 74 | $response->getBody(), |
|
45 | 74 | $response->getProtocolVersion(), |
|
46 | 74 | $response->getReasonPhrase() |
|
47 | 74 | ); |
|
48 | |||
49 | 74 | $this->request = $request; |
|
50 | |||
51 | 74 | $this->resolveData(); |
|
52 | 74 | } |
|
53 | |||
54 | 74 | private function resolveData() |
|
55 | { |
||
56 | 74 | $content = $this->getBody()->getContents(); |
|
57 | |||
58 | 74 | switch ($this->getRequestFormat()) { |
|
59 | 74 | case 'JSON': |
|
60 | 71 | $result_data = $this->jsonToArray($content); |
|
61 | 71 | break; |
|
62 | 3 | case 'XML': |
|
63 | 1 | $result_data = $this->xmlToArray($content); |
|
64 | 1 | break; |
|
65 | 2 | case 'RAW': |
|
66 | 1 | $result_data = $this->jsonToArray($content); |
|
67 | 1 | break; |
|
68 | 1 | default: |
|
69 | 1 | $result_data = $this->jsonToArray($content); |
|
70 | 74 | } |
|
71 | |||
72 | 74 | if (!$result_data) { |
|
0 ignored issues
–
show
|
|||
73 | 15 | $result_data = []; |
|
74 | 15 | } |
|
75 | |||
76 | 74 | $this->dot($result_data); |
|
77 | 74 | } |
|
78 | |||
79 | /** |
||
80 | * @return string |
||
81 | */ |
||
82 | 74 | private function getRequestFormat() |
|
83 | { |
||
84 | 74 | return ($this->request instanceof Request) |
|
0 ignored issues
–
show
|
|||
85 | 74 | ? \strtoupper($this->request->format) |
|
86 | 74 | : 'JSON'; |
|
87 | } |
||
88 | |||
89 | /** |
||
90 | * @param string $response |
||
91 | * |
||
92 | * @return array |
||
93 | */ |
||
94 | 73 | private function jsonToArray($response) |
|
95 | { |
||
96 | try { |
||
97 | 73 | return \GuzzleHttp\json_decode($response, true); |
|
98 | } catch (InvalidArgumentException $exception) { |
||
99 | return []; |
||
100 | } |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * @param string $string |
||
105 | * |
||
106 | * @return array |
||
107 | */ |
||
108 | 1 | private function xmlToArray($string) |
|
109 | { |
||
110 | try { |
||
111 | 1 | return json_decode(json_encode(simplexml_load_string($string)), true); |
|
112 | 1 | } catch (Exception $exception) { |
|
113 | 1 | return []; |
|
114 | } |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * @return string |
||
119 | */ |
||
120 | 1 | public function __toString() |
|
121 | { |
||
122 | 1 | return (string)$this->getBody(); |
|
123 | } |
||
124 | |||
125 | /** |
||
126 | * @return Request |
||
127 | */ |
||
128 | 32 | public function getRequest() |
|
129 | { |
||
130 | 32 | return $this->request; |
|
131 | } |
||
132 | |||
133 | /** |
||
134 | * @codeCoverageIgnore |
||
135 | * @return Response |
||
136 | * @deprecated |
||
137 | */ |
||
138 | public function getResponse() |
||
139 | { |
||
140 | return $this; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * @return bool |
||
145 | */ |
||
146 | 62 | public function isSuccess() |
|
147 | { |
||
148 | 62 | return 200 <= $this->getStatusCode() |
|
149 | 62 | && 300 > $this->getStatusCode(); |
|
150 | } |
||
151 | } |
||
152 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.