This repository has been archived by the owner on Sep 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOzanResponse.php
96 lines (86 loc) · 2.22 KB
/
OzanResponse.php
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
<?php
namespace Ozan;
use Ozan\Model\Mapper\PaymentMapper;
use Ozan\Model\Payment;
class OzanResponse
{
/**
* @var int The HTTP status code response from Ozan API.
*/
protected $httpStatusCode;
/**
* @var array The headers returned from Ozan API.
*/
protected $headers;
/**
* @var string The raw body of the response from Ozan API.
*/
protected $body;
/**
* @var array The decoded body of the Ozan response.
*/
protected $decodedBody = [];
/**
* @var array The response data body of the Ozan response.
*/
protected $responseData = [];
/**
* Creates a new OzanResponse entity.
*
* @param string|null $body
* @param int|null $httpStatusCode
* @param array|null $headers
*/
public function __construct($body = null, $httpStatusCode = null, array $headers = [])
{
$this->body = $body;
$this->httpStatusCode = $httpStatusCode;
$this->headers = $headers;
$this->decodeBody();
$this->responseData = $this->decodedBody['data'];
}
/**
* Returns a value from the response data.
*
* @param string $field The property to retrieve.
* @param mixed $default The default to return if the property doesn't exist.
*
* @return mixed
*/
public function getField($field, $default = null)
{
if (isset($this->responseData[$field])) {
return $this->responseData[$field];
}
return $default;
}
/**
* Returns payment object.
*
* @return Payment
*/
public function getPayment()
{
return PaymentMapper::create($this->body)->jsonDecode()->mapPayment(new Payment());
}
/**
* Return the decoded body response.
*
* @return array
*/
public function getDecodedBody()
{
return $this->decodedBody;
}
private function decodeBody()
{
$this->decodedBody = json_decode($this->body, true);
if ($this->decodedBody === null) {
$this->decodedBody = [];
parse_str($this->body, $this->decodedBody);
}
if (!is_array($this->decodedBody)) {
$this->decodedBody = [];
}
}
}