Completed
Pull Request — master (#170)
by Sergey
02:57
created

Response::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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