1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace P2A\YourMembership\Core; |
4
|
|
|
|
5
|
|
|
use P2A\YourMembership\Exceptions\YourMembershipResponseException; |
6
|
|
|
/** |
7
|
|
|
* Your Membership Response Object |
8
|
|
|
*/ |
9
|
|
|
class Response |
10
|
|
|
{ |
11
|
|
|
private $method; |
12
|
|
|
private $response; |
13
|
|
|
|
14
|
10 |
|
public function __construct(string $method, $response) |
15
|
|
|
{ |
16
|
10 |
|
$this->method = $method; |
17
|
|
|
|
18
|
10 |
|
$body = $response->getBody()->getContents(); |
19
|
10 |
|
$this->response = new \SimpleXMLElement($body); |
20
|
|
|
|
21
|
10 |
|
} |
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
|
6 |
|
public function hasError() : bool |
31
|
|
|
{ |
32
|
6 |
|
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
|
7 |
|
public function getErrorCode() : int |
43
|
|
|
{ |
44
|
7 |
|
return (int) $this->response->ErrCode; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Fetches the Error Message (including Extended Error Info) From Response |
49
|
|
|
* @method getError |
50
|
|
|
* @author PA |
51
|
|
|
* @date 2017-01-10 |
52
|
|
|
* @return string Error Message |
53
|
|
|
*/ |
54
|
3 |
|
public function getError() : string |
55
|
|
|
{ |
56
|
3 |
|
$error = (string) $this->response->ErrDesc; |
57
|
|
|
|
58
|
3 |
|
$extendedError = $this->getExtendedError(); |
59
|
|
|
|
60
|
3 |
|
if ($extendedError) { |
61
|
1 |
|
$error .= " ($extendedError)"; |
62
|
|
|
} |
63
|
|
|
|
64
|
3 |
|
return $error; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Fetches the Extended Error from the Response |
69
|
|
|
* @return string |
70
|
|
|
* @author TLS |
71
|
|
|
* @date 1-30-2017 |
72
|
|
|
*/ |
73
|
4 |
|
public function getExtendedError() : string |
74
|
|
|
{ |
75
|
4 |
|
return isset($this->response->ExtendedErrorInfo) ? (string) $this->response->ExtendedErrorInfo : ''; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Converts the response to an Array |
80
|
|
|
* @method toArray |
81
|
|
|
* @throws YourMembershipResponseException |
82
|
|
|
* @author PA |
83
|
|
|
* @date 2017-01-10 |
84
|
|
|
* @return array Response |
85
|
|
|
*/ |
86
|
2 |
|
public function toArray() : array |
87
|
|
|
{ |
88
|
2 |
|
return $this->unwrapXMLObject(true); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Converts the response to an Object |
93
|
|
|
* @method toObject |
94
|
|
|
* @throws YourMembershipResponseException |
95
|
|
|
* @author PA |
96
|
|
|
* @date 2017-01-11 |
97
|
|
|
* @return stdClass Response |
98
|
|
|
*/ |
99
|
3 |
|
public function toObject() : \stdClass |
100
|
|
|
{ |
101
|
3 |
|
return $this->unwrapXMLObject(false); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Unwraps XML Object into either StdClass or Array |
106
|
|
|
* Lossy conversion, attributes are lost from XML |
107
|
|
|
* |
108
|
|
|
* @method unwrapXMLObject |
109
|
|
|
* @throws YourMembershipResponseException |
110
|
|
|
* @author PA |
111
|
|
|
* @date 2017-01-11 |
112
|
|
|
* @param bool $asArray unwrap the object into an array instead of object |
113
|
|
|
* @return mixed|null Unwrapped Response |
114
|
|
|
*/ |
115
|
5 |
|
private function unwrapXMLObject(bool $asArray) |
116
|
|
|
{ |
117
|
|
|
//We cannot unwrap objects that have errors, so throw an exception |
118
|
5 |
|
if ($this->hasError()) { |
119
|
1 |
|
throw new YourMembershipResponseException($this->getError(), $this->getErrorCode(), $this->method); |
120
|
|
|
} |
121
|
|
|
|
122
|
4 |
|
return json_decode(json_encode($this->response->{$this->method}), $asArray); |
123
|
|
|
} |
124
|
|
|
/** |
125
|
|
|
* Returns the Result Count |
126
|
|
|
* @method getResultCount |
127
|
|
|
* @author PA |
128
|
|
|
* @date 2017-01-10 |
129
|
|
|
* @return int|false false if no ResultCount is present |
130
|
|
|
*/ |
131
|
1 |
|
public function getResultCount() : int |
132
|
|
|
{ |
133
|
1 |
|
$count = false; |
134
|
|
|
|
135
|
1 |
|
if (isset($this->response->{$this->method}->Results)) { |
136
|
1 |
|
$attributes = $this->response->{$this->method}->Results->attributes(); |
137
|
1 |
|
$count = (int) $attributes['ResultTotal'] ?? false; |
138
|
|
|
} |
139
|
|
|
|
140
|
1 |
|
return $count; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
} |
144
|
|
|
|