1
|
|
|
<?php |
2
|
|
|
namespace Haridarshan\Instagram; |
3
|
|
|
|
4
|
|
|
use GuzzleHttp\Psr7\Response; |
5
|
|
|
use Haridarshan\Instagram\Exceptions\InstagramResponseException; |
6
|
|
|
|
7
|
|
|
class InstagramResponse |
8
|
|
|
{ |
|
|
|
|
9
|
|
|
/** @var int $status_code */ |
10
|
|
|
private $statusCode; |
11
|
|
|
|
12
|
|
|
/** @var string $protocol */ |
13
|
|
|
private $protocol = '1.1'; |
14
|
|
|
|
15
|
|
|
/** @var array $headers */ |
16
|
|
|
private $headers = []; |
17
|
|
|
|
18
|
|
|
/** @var bool */ |
19
|
|
|
private $isPagination = false; |
20
|
|
|
|
21
|
|
|
/** @var object */ |
22
|
|
|
private $pagination; |
23
|
|
|
|
24
|
|
|
/** @var bool */ |
25
|
|
|
private $isMetaData = false; |
26
|
|
|
|
27
|
|
|
/** @var object */ |
28
|
|
|
private $metaData; |
29
|
|
|
|
30
|
|
|
/** @var object */ |
31
|
|
|
private $data; |
32
|
|
|
|
33
|
|
|
/** @var object $body */ |
34
|
|
|
private $body; |
35
|
|
|
|
36
|
|
|
/* |
37
|
|
|
* @param Response $response |
38
|
|
|
* @throws InstagramResponseException |
39
|
|
|
*/ |
40
|
|
|
public function __construct(Response $response) |
41
|
|
|
{ |
42
|
|
|
if ($response instanceof Response) { |
43
|
|
|
$this->setParams($response); |
44
|
|
|
} else { |
45
|
|
|
throw new InstagramResponseException('Bad Request: Response is not valid instance of GuzzleHttp\Psr7\Response', 404); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/* |
50
|
|
|
* Set Values to the class members |
51
|
|
|
* @param Response $response |
52
|
|
|
* @return void |
53
|
|
|
*/ |
54
|
|
|
private function setParams($response) |
55
|
|
|
{ |
56
|
|
|
$this->protocol = $response->getProtocolVersion(); |
57
|
|
|
$this->statusCode = (int) $response->getStatusCode(); |
58
|
|
|
$this->headers = $response->getHeaders(); |
59
|
|
|
$this->body = json_decode($response->getBody()->getContents()); |
60
|
|
|
$this->extractBodyParts(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
private function extractBodyParts() |
64
|
|
|
{ |
65
|
|
|
if (isset($this->body->pagination)) { |
66
|
|
|
$this->isPagination = true; |
67
|
|
|
$this->pagination = $this->body->pagination; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if (isset($this->body->meta)) { |
71
|
|
|
$this->isMetaData = true; |
72
|
|
|
$this->metaData = $this->body->meta; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$this->data = $this->body->data; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/* |
79
|
|
|
* Get response |
80
|
|
|
* @return object|string |
81
|
|
|
*/ |
82
|
|
|
public function getBody() |
83
|
|
|
{ |
84
|
|
|
return $this->body; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/* |
88
|
|
|
* Get Status Code |
89
|
|
|
* @return int |
90
|
|
|
*/ |
91
|
|
|
public function getStatusCode() |
92
|
|
|
{ |
93
|
|
|
return $this->statusCode; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/* |
97
|
|
|
* Get specific header |
98
|
|
|
* @param string $header |
99
|
|
|
* @retrun string |
100
|
|
|
*/ |
101
|
|
|
public function getHeader($header) |
102
|
|
|
{ |
103
|
|
|
return isset($this->headers[$header]) ? $this->headers[$header] : []; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/* |
107
|
|
|
* Get all headers |
108
|
|
|
* @retrun array |
109
|
|
|
*/ |
110
|
|
|
public function getHeaders() |
111
|
|
|
{ |
112
|
|
|
return $this->headers; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/* |
116
|
|
|
* Get data from body |
117
|
|
|
* @return object |
118
|
|
|
*/ |
119
|
|
|
public function getData() |
120
|
|
|
{ |
121
|
|
|
return $this->data; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/* |
125
|
|
|
* Get Meta data |
126
|
|
|
* @return object |
127
|
|
|
*/ |
128
|
|
|
public function getMetaData() |
129
|
|
|
{ |
130
|
|
|
return $this->metaData; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/* |
134
|
|
|
* Get Meta data |
135
|
|
|
* @return object |
136
|
|
|
*/ |
137
|
|
|
public function getPagination() |
138
|
|
|
{ |
139
|
|
|
return $this->pagination; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/* |
143
|
|
|
* Is Meta Data Present |
144
|
|
|
* @return bool |
145
|
|
|
*/ |
146
|
|
|
public function isMetaDataSet() |
147
|
|
|
{ |
148
|
|
|
return $this->isMetaData; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/* |
152
|
|
|
* Is Pagination present |
153
|
|
|
* @return bool |
154
|
|
|
*/ |
155
|
|
|
public function isPaginationSet() |
156
|
|
|
{ |
157
|
|
|
return $this->isPagination; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/* |
161
|
|
|
* Get Protocol version |
162
|
|
|
* @return string |
163
|
|
|
*/ |
164
|
|
|
public function getProtocol() |
165
|
|
|
{ |
166
|
|
|
return $this->protocol; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|