1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace P2A\YourMembership\Core; |
4
|
|
|
|
5
|
|
|
use P2A\YourMembership\Exceptions\YourMembershipApiException; |
6
|
|
|
/** |
7
|
|
|
* Your Membership Response Object |
8
|
|
|
*/ |
9
|
|
|
class Response |
10
|
|
|
{ |
11
|
|
|
private $method; |
12
|
|
|
private $response; |
13
|
|
|
|
14
|
|
|
public function __construct(string $method, $response) |
15
|
|
|
{ |
16
|
|
|
$this->method = $method; |
17
|
|
|
|
18
|
|
|
$body = $response->getBody()->getContents(); |
19
|
|
|
$this->response = new \SimpleXMLElement($body); |
20
|
|
|
|
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Checks if the response contains an Error |
25
|
|
|
* @method hasError |
26
|
|
|
* @author PA |
27
|
|
|
* @date 2017-01-10 |
28
|
|
|
* @return bool hasError |
29
|
|
|
*/ |
30
|
|
|
public function hasError() : bool |
31
|
|
|
{ |
32
|
|
|
return ($this->getErrorCode() != 0); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Fetches the Error Code from the Response |
37
|
|
|
* @method getErrorCode |
38
|
|
|
* @author PA |
39
|
|
|
* @date 2017-01-10 |
40
|
|
|
* @return int Error Code |
41
|
|
|
*/ |
42
|
|
|
public function getErrorCode() : int |
43
|
|
|
{ |
44
|
|
|
return (int) $this->response->ErrCode; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Fetches the Error Message From Response |
49
|
|
|
* @method getError |
50
|
|
|
* @author PA |
51
|
|
|
* @date 2017-01-10 |
52
|
|
|
* @return string Error Message |
53
|
|
|
*/ |
54
|
|
|
public function getError() : string |
55
|
|
|
{ |
56
|
|
|
return (string) $this->response->ErrDesc; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Converts the response to an Array |
61
|
|
|
* @method toArray |
62
|
|
|
* @throws YourMembershipException |
63
|
|
|
* @author PA |
64
|
|
|
* @date 2017-01-10 |
65
|
|
|
* @return array Response |
66
|
|
|
*/ |
67
|
|
|
public function toArray() : array |
68
|
|
|
{ |
69
|
|
|
return $this->unwrapXMLObject(true); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Converts the response to an Object |
74
|
|
|
* @method toObject |
75
|
|
|
* @throws YourMembershipException |
76
|
|
|
* @author PA |
77
|
|
|
* @date 2017-01-11 |
78
|
|
|
* @return stdClass Response |
79
|
|
|
*/ |
80
|
|
|
public function toObject() : \stdClass |
81
|
|
|
{ |
82
|
|
|
return $this->unwrapXMLObject(false); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Unwraps XML Object into either StdClass or Array |
87
|
|
|
* Lossy conversion, attributes are lost from XML |
88
|
|
|
* |
89
|
|
|
* @method unwrapXMLObject |
90
|
|
|
* @throws YourMembershipException |
91
|
|
|
* @author PA |
92
|
|
|
* @date 2017-01-11 |
93
|
|
|
* @param bool $asArray unwrap the object into an array instead of object |
94
|
|
|
* @return mixed|null Unwrapped Response |
95
|
|
|
*/ |
96
|
|
|
private function unwrapXMLObject(bool $asArray) |
97
|
|
|
{ |
98
|
|
|
//We cannot unwrap objects that have errors, so throw an exception |
99
|
|
|
if ($this->hasError()) { |
100
|
|
|
throw new YourMembershipApiException($this->getError(), $this->getErrorCode(), $this->method); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return json_decode(json_encode($this->response->{$this->method}), $asArray); |
104
|
|
|
} |
105
|
|
|
/** |
106
|
|
|
* Returns the Result Count |
107
|
|
|
* @method getResultCount |
108
|
|
|
* @author PA |
109
|
|
|
* @date 2017-01-10 |
110
|
|
|
* @return int|false false if no ResultCount is present |
111
|
|
|
*/ |
112
|
|
|
public function getResultCount() : int |
113
|
|
|
{ |
114
|
|
|
$count = false; |
115
|
|
|
|
116
|
|
|
if (isset($this->response->{$this->method}->Results)) { |
117
|
|
|
$attributes = $this->response->{$this->method}->Results->attributes(); |
118
|
|
|
$count = (int) $attributes['ResultTotal'] ?? false; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $count; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
} |
125
|
|
|
|