forked from microsoftgraph/msgraph-sdk-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphResponse.php
More file actions
189 lines (174 loc) · 4.5 KB
/
GraphResponse.php
File metadata and controls
189 lines (174 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
/**
* Copyright (c) Microsoft Corporation. All Rights Reserved.
* Licensed under the MIT License. See License in the project root
* for license information.
*
* HttpResponse File
* PHP version 7
*
* @category Library
* @package Microsoft.Graph
* @copyright 2020 Microsoft Corporation
* @license https://opensource.org/licenses/MIT MIT License
* @version GIT: 1.13.0
* @link https://graph.microsoft.io/
*/
namespace Microsoft\Graph\Http;
use Microsoft\Graph\Exception\GraphException;
use Microsoft\Graph\Core\GraphConstants;
/**
* Class GraphResponse
*
* @category Library
* @package Microsoft.Graph
* @license https://opensource.org/licenses/MIT MIT License
* @link https://graph.microsoft.io/
*/
class GraphResponse
{
/**
* The body of the response
*
* @var string
*/
private $_body;
/**
* The body of the response,
* decoded into an array
*
* @var array(string)
*/
private $_decodedBody;
/**
* The headers of the response
*
* @var array(string)
*/
private $_headers;
/**
* The status code of the response
*
* @var string
*/
private $_httpStatusCode;
/**
* Creates a new Graph HTTP response entity
*
* @param object $request The request
* @param string $body The body of the response
* @param string $httpStatusCode The returned status code
* @param array $headers The returned headers
*/
public function __construct($request, $body = null, $httpStatusCode = null, $headers = null)
{
$this->_request = $request;
$this->_body = $body;
$this->_httpStatusCode = $httpStatusCode;
$this->_headers = $headers;
$this->_decodedBody = $this->_decodeBody();
}
/**
* Decode the JSON response into an array
*
* @return array The decoded response
*/
private function _decodeBody()
{
$decodedBody = json_decode($this->_body, true);
if ($decodedBody === null) {
$decodedBody = array();
}
return $decodedBody;
}
/**
* Get the decoded body of the HTTP response
*
* @return array The decoded body
*/
public function getBody()
{
return $this->_decodedBody;
}
/**
* Get the undecoded body of the HTTP response
*
* @return string|null The undecoded body
*/
public function getRawBody()
{
return $this->_body;
}
/**
* Get the status of the HTTP response
*
* @return string|null The HTTP status
*/
public function getStatus()
{
return $this->_httpStatusCode;
}
/**
* Get the headers of the response
*
* @return array|null The response headers
*/
public function getHeaders()
{
return $this->_headers;
}
/**
* Converts the response JSON object to a Graph SDK object
*
* @param mixed $returnType The type to convert the object(s) to
*
* @return mixed object or array of objects of type $returnType
*/
public function getResponseAsObject($returnType)
{
$class = $returnType;
$result = $this->getBody();
//If more than one object is returned
if (array_key_exists('value', $result)) {
$values = $result['value'];
//Check that this is an object array instead of a value called "value"
if (is_array($values)) {
$objArray = array();
foreach ($values as $obj) {
$objArray[] = new $class($obj);
}
return $objArray;
}
}
return new $class($result);
}
/**
* Gets the next link of a response object from OData
* If the nextLink is null, there are no more pages
*
* @return string|null nextLink, if provided
*/
public function getNextLink()
{
if (array_key_exists("@odata.nextLink", $this->getBody())) {
$nextLink = $this->getBody()['@odata.nextLink'];
return $nextLink;
}
return null;
}
/**
* Gets the delta link of a response object from OData
* If the deltaLink is null, there are more pages in the collection;
* use nextLink to obtain more
*
* @return string|null deltaLink
*/
public function getDeltaLink()
{
if (array_key_exists("@odata.deltaLink", $this->getBody())) {
$deltaLink = $this->getBody()['@odata.deltaLink'];
return $deltaLink;
}
return null;
}
}