Completed
Pull Request — master (#266)
by Sergey
02:26
created

Response::fillClientInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api;
4
5
use seregazhuk\PinterestBot\Helpers\ArrayHelper;
6
use seregazhuk\PinterestBot\Api\Contracts\PaginatedResponse;
7
8
class Response implements PaginatedResponse
9
{
10
    /**
11
     * @var mixed
12
     */
13
    protected $data = [];
14
15
    /**
16
     * @var array|null
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
        if($data) $this->fill($data);
31
    }
32
33
    /**
34
     * @param mixed $data
35
     * @return $this
36
     */
37
    public function fill($data)
38
    {
39
        $this->data = $data;
40
41
        return $this
42
            ->fillError()
43
            ->fillClientInfo();
44
    }
45
46
    /**
47
     * @param string $json
48
     * @return static
49
     */
50
    public function fillFromJson($json)
51
    {
52
        return $this->fill(json_decode($json, true));
53
    }
54
55
    /**
56
     * Check if specified data exists in response.
57
     *
58
     * @param null  $key
59
     * @return array|bool
60
     */
61
    public function getResponseData($key = null)
62
    {
63
        if ($this->hasErrors()) return false;
64
65
        return $this->parseResponseData($key);
66
    }
67
68
    /**
69
     * @param string $key
70
     * @param null $default
71
     * @return mixed
72
     */
73
    public function getData($key = '', $default = null)
74
    {
75
        return ArrayHelper::getValueByKey($key, $this->data, $default);
76
    }
77
78
    /**
79
     * @param string $key
80
     * @return bool
81
     */
82
    public function hasData($key = '')
83
    {
84
        return !is_null(ArrayHelper::getValueByKey($key, $this->data));
85
    }
86
87
    /**
88
     * Parse data from Pinterest Api response.
89
     * Data is stored in ['resource_response']['data'] array.
90
     *
91
     * @param string $key
92
     * @return bool|array
93
     */
94
    protected function parseResponseData($key)
95
    {
96
        $responseData = ArrayHelper::getValueByKey('resource_response.data', $this->data);
97
98
        if (!$responseData) return false;
99
100
        return $key ?
101
            ArrayHelper::getValueByKey($key, $responseData) :
102
            $responseData;
103
    }
104
105
    /**
106
     * Checks if response is empty.
107
     *
108
     * @return bool
109
     */
110
    public function isEmpty()
111
    {
112
        return empty($this->getResponseData());
113
    }
114
115
    /**
116
     * @return bool
117
     */
118
    public function isOk()
119
    {
120
        return !$this->hasErrors();
121
    }
122
123
    /**
124
     * Check for error info in api response and save
125
     * it.
126
     *
127
     * @return bool
128
     */
129
    public function hasErrors()
130
    {
131
        return !empty($this->lastError);
132
    }
133
134
    /**
135
     * Parse bookmarks from response.
136
     *
137
     * @return array
138
     */
139
    public function getBookmarks()
140
    {
141
        $bookmarks = $this->getRawBookmarksData();
142
143
        if (empty($bookmarks)) return [];
144
145
        if ($bookmarks[0] == '-end-') return [];
146
147
        return $bookmarks;
148
    }
149
150
    /**
151
     * @return bool
152
     */
153
    public function hasBookmarks()
154
    {
155
        return !! $this->getBookmarks();
156
    }
157
158
    protected function getRawBookmarksData()
159
    {
160
        return $this->getData('resource.options.bookmarks', []);
161
    }
162
163
    /**
164
     * Checks Pinterest API paginated response, and parses data
165
     * with bookmarks info from it.
166
     *
167
     * @return array
168
     */
169
    public function getPaginationData()
170
    {
171
        if ($this->hasErrors()) return [];
172
173
        if ($data = $this->getResponseData()) {
174
            return [
175
                'data'      => $data,
176
                'bookmarks' => $this->getBookmarks(),
177
            ];
178
        }
179
180
        return [];
181
    }
182
183
    /**
184
     * @return array
185
     */
186
    public function getLastError()
187
    {
188
        return $this->lastError;
189
    }
190
191
    /**
192
     * @return array|null
193
     */
194
    public function getClientInfo()
195
    {
196
        return $this->clientInfo;
197
    }
198
199
    /**
200
     * @return mixed
201
     */
202
    public function getRawData()
203
    {
204
        return $this->data;
205
    }
206
207
    public function clear()
208
    {
209
        return $this->fill([]);
210
    }
211
212
    protected function fillError()
213
    {
214
        $error = ArrayHelper::getValueByKey('resource_response.error', $this->data);
215
216
        if ($error) $this->lastError = $error;
217
218
        return $this;
219
    }
220
221
    protected function fillClientInfo()
222
    {
223
        $this->clientInfo = ArrayHelper::getValueByKey('client_context', $this->data);
224
225
        return $this;
226
    }
227
}
228