1
|
|
|
<?php |
2
|
|
|
namespace Haridarshan\Instagram; |
3
|
|
|
|
4
|
|
|
use GuzzleHttp\Psr7\Response; |
5
|
|
|
use Haridarhan\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
|
|
|
* @return this |
|
|
|
|
39
|
|
|
* @throws InstagramResponseException |
40
|
|
|
*/ |
41
|
|
|
public function __construct(Response $response) |
42
|
|
|
{ |
43
|
|
|
if ($response instanceof Response) { |
44
|
|
|
$this->setParams($response); |
45
|
|
|
} else { |
46
|
|
|
throw new InstagramResponseException('Bad Request: Response is not valid instance of GuzzleHttp\Psr7\Response', 404); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/* |
51
|
|
|
* Set Values to the class members |
52
|
|
|
* @param Response $response |
53
|
|
|
* @return void |
54
|
|
|
*/ |
55
|
|
|
private function setParams($response) |
56
|
|
|
{ |
57
|
|
|
$this->protocol = $response->getProtocolVersion(); |
58
|
|
|
$this->statusCode = (int) $response->getStatusCode(); |
59
|
|
|
$this->headers = $response->getHeaders(); |
60
|
|
|
$this->body = json_decode($response->getBody()->getContents()); |
61
|
|
|
$this->extractBodyParts(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private function extractBodyParts() |
65
|
|
|
{ |
66
|
|
|
if (isset($this->body->pagination)) { |
67
|
|
|
$this->isPagination = true; |
68
|
|
|
$this->pagination = $this->body->pagination; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if (isset($this->body->meta)) { |
72
|
|
|
$this->isMetaData = true; |
73
|
|
|
$this->metaData = $this->body->meta; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$this->data = $this->body->data; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/* |
80
|
|
|
* Get response |
81
|
|
|
* @return object|string |
82
|
|
|
*/ |
83
|
|
|
public function getBody() |
84
|
|
|
{ |
85
|
|
|
return $this->body; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/* |
89
|
|
|
* Get Status Code |
90
|
|
|
* @return int |
91
|
|
|
*/ |
92
|
|
|
public function getStatusCode() |
93
|
|
|
{ |
94
|
|
|
return $this->statusCode; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/* |
98
|
|
|
* Get specific header |
99
|
|
|
* @param string $header |
100
|
|
|
* @retrun string |
101
|
|
|
*/ |
102
|
|
|
public function getHeader($header) |
103
|
|
|
{ |
104
|
|
|
return isset($this->headers[$header]) ? $this->headers[$header] : []; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/* |
108
|
|
|
* Get all headers |
109
|
|
|
* @retrun array |
110
|
|
|
*/ |
111
|
|
|
public function getHeaders() |
112
|
|
|
{ |
113
|
|
|
return $this->headers; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/* |
117
|
|
|
* Get data from body |
118
|
|
|
* @return object |
119
|
|
|
*/ |
120
|
|
|
public function getData() |
121
|
|
|
{ |
122
|
|
|
return $this->body->data; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/* |
126
|
|
|
* Get Meta data |
127
|
|
|
* @return object |
128
|
|
|
*/ |
129
|
|
|
public function getMetaData() |
130
|
|
|
{ |
131
|
|
|
return $this->metaData; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|