1: | <?php |
2: | namespace Hyperwallet\Response; |
3: | |
4: | use Hyperwallet\Model\Error; |
5: | |
6: | /** |
7: | * Represents a API error response |
8: | * |
9: | * @package Hyperwallet\Response |
10: | */ |
11: | class ErrorResponse implements \Countable, \ArrayAccess { |
12: | |
13: | /** |
14: | * The http status code |
15: | * |
16: | * @var int |
17: | */ |
18: | private $statusCode; |
19: | |
20: | /** |
21: | * The list of errors |
22: | * |
23: | * @var Error[] |
24: | */ |
25: | private $errors; |
26: | |
27: | /** |
28: | * The list of related resources |
29: | * |
30: | * @var array |
31: | */ |
32: | private $relatedResources; |
33: | |
34: | /** |
35: | * Creates a ErrorResponse instance |
36: | * |
37: | * @param int $statusCode The http status code |
38: | * @param array $errors the errors response body |
39: | */ |
40: | public function __construct($statusCode, array $errors) { |
41: | $this->statusCode = $statusCode; |
42: | $this->errors = array_map(function ($error) { |
43: | if (!isset($this->relatedResources) && isset($error['relatedResources'])) { |
44: | $this->relatedResources = $error['relatedResources']; |
45: | } |
46: | return new Error($error); |
47: | }, $errors['errors']); |
48: | } |
49: | |
50: | /** |
51: | * Get the http status code |
52: | * |
53: | * @return int |
54: | */ |
55: | public function getStatusCode() { |
56: | return $this->statusCode; |
57: | } |
58: | |
59: | /** |
60: | * Get the list of errors |
61: | * |
62: | * @return Error[] |
63: | */ |
64: | public function getErrors() { |
65: | return $this->errors; |
66: | } |
67: | |
68: | /** |
69: | * Get list of related resources |
70: | * |
71: | * @return array |
72: | */ |
73: | public function getRelatedResources() { |
74: | return $this->relatedResources; |
75: | } |
76: | |
77: | /** |
78: | * @internal |
79: | * |
80: | * Count elements of an object |
81: | * @link http://php.net/manual/en/countable.count.php |
82: | * @return int The custom count as an integer. |
83: | * </p> |
84: | * <p> |
85: | * The return value is cast to an integer. |
86: | * @since 5.1.0 |
87: | * |
88: | * Note: Temporarily suppress the required return type, to keep compatibility with PHP 5.6 |
89: | * the #[\ReturnTypeWillChange] must be removed once 5.6 |
90: | */ |
91: | #[\ReturnTypeWillChange] |
92: | public function count() { |
93: | return count($this->errors); |
94: | } |
95: | |
96: | /** |
97: | * @internal |
98: | * |
99: | * Whether a offset exists |
100: | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
101: | * @param mixed $offset <p> |
102: | * An offset to check for. |
103: | * </p> |
104: | * @return boolean true on success or false on failure. |
105: | * </p> |
106: | * <p> |
107: | * The return value will be casted to boolean if non-boolean was returned. |
108: | * @since 5.0.0 |
109: | * |
110: | * Note: Temporarily suppress the required return type, to keep compatibility with PHP 5.6 |
111: | * the #[\ReturnTypeWillChange] must be removed once 5.6 |
112: | */ |
113: | #[\ReturnTypeWillChange] |
114: | public function offsetExists($offset) { |
115: | return isset($this->errors[$offset]); |
116: | } |
117: | |
118: | /** |
119: | * @internal |
120: | * |
121: | * Offset to retrieve |
122: | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
123: | * @param mixed $offset <p> |
124: | * The offset to retrieve. |
125: | * </p> |
126: | * @return mixed Can return all value types. |
127: | * @since 5.0.0 |
128: | * |
129: | * Note: Temporarily suppress the required return type, to keep compatibility with PHP 5.6 |
130: | * the #[\ReturnTypeWillChange] must be removed once 5.6 |
131: | */ |
132: | #[\ReturnTypeWillChange] |
133: | public function offsetGet($offset) { |
134: | return $this->errors[$offset]; |
135: | } |
136: | |
137: | /** |
138: | * @internal |
139: | * |
140: | * Offset to set |
141: | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
142: | * @param mixed $offset <p> |
143: | * The offset to assign the value to. |
144: | * </p> |
145: | * @param mixed $value <p> |
146: | * The value to set. |
147: | * </p> |
148: | * @return void |
149: | * @since 5.0.0 |
150: | * |
151: | * Note: Temporarily suppress the required return type, to keep compatibility with PHP 5.6 |
152: | * the #[\ReturnTypeWillChange] must be removed once 5.6 |
153: | */ |
154: | #[\ReturnTypeWillChange] |
155: | public function offsetSet($offset, $value) { |
156: | $this->errors[$offset] = $value; |
157: | } |
158: | |
159: | /** |
160: | * @internal |
161: | * |
162: | * Offset to unset |
163: | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
164: | * @param mixed $offset <p> |
165: | * The offset to unset. |
166: | * </p> |
167: | * @return void |
168: | * @since 5.0.0 |
169: | * |
170: | * Note: Temporarily suppress the required return type, to keep compatibility with PHP 5.6 |
171: | * the #[\ReturnTypeWillChange] must be removed once 5.6 |
172: | */ |
173: | #[\ReturnTypeWillChange] |
174: | public function offsetUnset($offset) { |
175: | unset($this->errors[$offset]); |
176: | } |
177: | } |