1 | <?php |
||||
2 | |||||
3 | namespace seregazhuk\PinterestBot\Api; |
||||
4 | |||||
5 | use seregazhuk\PinterestBot\Api\Contracts\PaginatedResponse; |
||||
6 | use function seregazhuk\PinterestBot\get_array_data; |
||||
7 | |||||
8 | class Response implements PaginatedResponse |
||||
9 | { |
||||
10 | /** |
||||
11 | * @var mixed |
||||
12 | */ |
||||
13 | protected $data = []; |
||||
14 | |||||
15 | /** |
||||
16 | * @var Error |
||||
17 | */ |
||||
18 | protected $lastError; |
||||
19 | |||||
20 | /** |
||||
21 | * @var array|null |
||||
22 | */ |
||||
23 | protected $clientInfo; |
||||
24 | |||||
25 | /** |
||||
26 | * @param mixed $data |
||||
27 | */ |
||||
28 | public function __construct($data = null) |
||||
29 | { |
||||
30 | $this->lastError = new Error(); |
||||
31 | |||||
32 | if ($data) { |
||||
33 | $this->fill($data); |
||||
34 | } |
||||
35 | } |
||||
36 | |||||
37 | /** |
||||
38 | * @param mixed $data |
||||
39 | * @return $this |
||||
40 | */ |
||||
41 | public function fill($data) |
||||
42 | { |
||||
43 | $this->data = $data; |
||||
44 | |||||
45 | return $this |
||||
46 | ->fillError() |
||||
47 | ->fillClientInfo(); |
||||
48 | } |
||||
49 | |||||
50 | /** |
||||
51 | * @param string $json |
||||
52 | * @return static |
||||
53 | */ |
||||
54 | public function fillFromJson($json) |
||||
55 | { |
||||
56 | return $this->fill(json_decode($json, true)); |
||||
57 | } |
||||
58 | |||||
59 | /** |
||||
60 | * Check if specified data exists in response. |
||||
61 | * |
||||
62 | * @param null $key |
||||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||||
63 | * @return array|bool |
||||
64 | */ |
||||
65 | public function getResponseData($key = null) |
||||
66 | { |
||||
67 | if ($this->hasErrors()) { |
||||
68 | return false; |
||||
69 | } |
||||
70 | |||||
71 | return $this->parseResponseData($key); |
||||
72 | } |
||||
73 | |||||
74 | /** |
||||
75 | * @param string $key |
||||
76 | * @param null|mixed $default |
||||
77 | * @return mixed |
||||
78 | */ |
||||
79 | public function getData($key = '', $default = null) |
||||
80 | { |
||||
81 | return get_array_data($key, $this->data, $default); |
||||
82 | } |
||||
83 | |||||
84 | /** |
||||
85 | * @param string $key |
||||
86 | * @return bool |
||||
87 | */ |
||||
88 | public function hasData($key = '') |
||||
89 | { |
||||
90 | return get_array_data($key, $this->data) !== null; |
||||
91 | } |
||||
92 | |||||
93 | /** |
||||
94 | * Parse data from Pinterest Api response. |
||||
95 | * Data is stored in ['resource_response']['data'] array. |
||||
96 | * |
||||
97 | * @param string|null $key |
||||
98 | * @return bool|array |
||||
99 | */ |
||||
100 | protected function parseResponseData($key = null) |
||||
101 | { |
||||
102 | $responseData = get_array_data('resource_response.data', $this->data); |
||||
103 | |||||
104 | if (!$responseData) { |
||||
105 | return false; |
||||
106 | } |
||||
107 | |||||
108 | return $key ? |
||||
109 | get_array_data($key, $responseData) : |
||||
0 ignored issues
–
show
It seems like
$responseData can also be of type true ; however, parameter $data of seregazhuk\PinterestBot\get_array_data() does only seem to accept array , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
110 | $responseData; |
||||
111 | } |
||||
112 | |||||
113 | /** |
||||
114 | * Checks if response is empty. |
||||
115 | * |
||||
116 | * @return bool |
||||
117 | */ |
||||
118 | public function isEmpty() |
||||
119 | { |
||||
120 | return empty($this->getResponseData()); |
||||
121 | } |
||||
122 | |||||
123 | /** |
||||
124 | * @return bool |
||||
125 | */ |
||||
126 | public function isOk() |
||||
127 | { |
||||
128 | return !$this->hasErrors(); |
||||
129 | } |
||||
130 | |||||
131 | /** |
||||
132 | * Check for error info in api response and save it. |
||||
133 | * |
||||
134 | * @return bool |
||||
135 | */ |
||||
136 | public function hasErrors() |
||||
137 | { |
||||
138 | return !$this->lastError->isEmpty(); |
||||
139 | } |
||||
140 | |||||
141 | /** |
||||
142 | * Parse bookmarks from response. |
||||
143 | * |
||||
144 | * @return array |
||||
145 | */ |
||||
146 | public function getBookmarks() |
||||
147 | { |
||||
148 | $bookmarks = $this->getRawBookmarksData(); |
||||
149 | |||||
150 | if (empty($bookmarks)) { |
||||
151 | return []; |
||||
152 | } |
||||
153 | |||||
154 | if ($bookmarks[0] === '-end-') { |
||||
155 | return []; |
||||
156 | } |
||||
157 | |||||
158 | return $bookmarks; |
||||
0 ignored issues
–
show
|
|||||
159 | } |
||||
160 | |||||
161 | /** |
||||
162 | * @return bool |
||||
163 | */ |
||||
164 | public function hasBookmarks() |
||||
165 | { |
||||
166 | return (bool) $this->getBookmarks(); |
||||
167 | } |
||||
168 | |||||
169 | protected function getRawBookmarksData() |
||||
170 | { |
||||
171 | return $this->getData('resource.options.bookmarks', []); |
||||
172 | } |
||||
173 | |||||
174 | /** |
||||
175 | * Checks Pinterest API paginated response, and parses data |
||||
176 | * with bookmarks info from it. |
||||
177 | * |
||||
178 | * @return array |
||||
179 | */ |
||||
180 | public function getPaginationData() |
||||
181 | { |
||||
182 | if ($this->hasErrors()) { |
||||
183 | return []; |
||||
184 | } |
||||
185 | |||||
186 | if ($data = $this->getResponseData()) { |
||||
187 | return [ |
||||
188 | 'data' => $data, |
||||
189 | 'bookmarks' => $this->getBookmarks(), |
||||
190 | ]; |
||||
191 | } |
||||
192 | |||||
193 | return []; |
||||
194 | } |
||||
195 | |||||
196 | /** |
||||
197 | * @return string|null |
||||
198 | */ |
||||
199 | public function getLastErrorText() |
||||
200 | { |
||||
201 | return $this->lastError->getText(); |
||||
202 | } |
||||
203 | |||||
204 | /** |
||||
205 | * @return array|null |
||||
206 | */ |
||||
207 | public function getClientInfo() |
||||
208 | { |
||||
209 | return $this->clientInfo; |
||||
210 | } |
||||
211 | |||||
212 | /** |
||||
213 | * @return mixed |
||||
214 | */ |
||||
215 | public function getRawData() |
||||
216 | { |
||||
217 | return $this->data; |
||||
218 | } |
||||
219 | |||||
220 | public function clear() |
||||
221 | { |
||||
222 | return $this->fill([]); |
||||
223 | } |
||||
224 | |||||
225 | protected function fillError() |
||||
226 | { |
||||
227 | $errorData = get_array_data('resource_response.error', $this->data); |
||||
228 | |||||
229 | $this->lastError = new Error($errorData); |
||||
0 ignored issues
–
show
It seems like
$errorData can also be of type boolean ; however, parameter $errorData of seregazhuk\PinterestBot\Api\Error::__construct() does only seem to accept array|null , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
230 | |||||
231 | return $this; |
||||
232 | } |
||||
233 | |||||
234 | protected function fillClientInfo() |
||||
235 | { |
||||
236 | $this->clientInfo = get_array_data('client_context', $this->data); |
||||
0 ignored issues
–
show
It seems like
get_array_data('client_context', $this->data) can also be of type boolean . However, the property $clientInfo is declared as type array|null . Maybe add an additional type check?
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly. For example, imagine you have a variable Either this assignment is in error or a type check should be added for that assignment. class Id
{
public $id;
public function __construct($id)
{
$this->id = $id;
}
}
class Account
{
/** @var Id $id */
public $id;
}
$account_id = false;
if (starsAreRight()) {
$account_id = new Id(42);
}
$account = new Account();
if ($account instanceof Id)
{
$account->id = $account_id;
}
![]() |
|||||
237 | |||||
238 | return $this; |
||||
239 | } |
||||
240 | } |
||||
241 |