1: | <?php |
2: | namespace Hyperwallet\Response; |
3: | |
4: | /** |
5: | * Represents a API list response |
6: | * |
7: | * @package Hyperwallet\Response |
8: | */ |
9: | class ListResponse implements \Countable , \ArrayAccess{ |
10: | |
11: | /** |
12: | * Total number of matching objects |
13: | * |
14: | * @var int |
15: | */ |
16: | private $limit; |
17: | |
18: | /** |
19: | * Has Next Page objects |
20: | * |
21: | * @var boolean |
22: | */ |
23: | private $hasNextPage; |
24: | |
25: | /** |
26: | * Has Previous Page objects |
27: | * |
28: | * @var boolean |
29: | */ |
30: | private $hasPreviousPage; |
31: | |
32: | /** |
33: | * Array of Model's |
34: | * |
35: | * @var array |
36: | */ |
37: | private $data; |
38: | |
39: | /** |
40: | * Array of Model's |
41: | * |
42: | * @var array |
43: | */ |
44: | private $links; |
45: | |
46: | /** |
47: | * Creates a api list response instance |
48: | * |
49: | * @param array $body The api response body |
50: | * @param callable $convertEntry |
51: | */ |
52: | public function __construct(array $body, $convertEntry) { |
53: | if (count($body) == 0) { |
54: | $this->hasNextPage = false; |
55: | $this->hasPreviousPage = false; |
56: | $this->limit = 0 ; |
57: | $this->data = array(); |
58: | } else { |
59: | $this->hasNextPage = $body['hasNextPage']; |
60: | $this->hasPreviousPage = $body['hasPreviousPage']; |
61: | $this->limit = $body['limit']; |
62: | $this->links = $body['links']; |
63: | $this->data = array_map(function ($item) use ($convertEntry) { |
64: | return $convertEntry($item); |
65: | }, $body['data']); |
66: | } |
67: | } |
68: | |
69: | /** |
70: | * Get the array of Model's |
71: | * |
72: | * @return array |
73: | */ |
74: | public function getLinks() { |
75: | return $this->links; |
76: | } |
77: | |
78: | /** |
79: | * Get the array of Model's |
80: | * |
81: | * @return array |
82: | */ |
83: | public function getData() { |
84: | return $this->data; |
85: | } |
86: | |
87: | /** |
88: | * Get the Total number of Model's |
89: | * |
90: | * @return int |
91: | */ |
92: | public function getLimit() { |
93: | return $this->limit; |
94: | } |
95: | |
96: | /** |
97: | * Get Has Next Page of Model's |
98: | * |
99: | * @return boolean |
100: | */ |
101: | public function getHasNextPage() { |
102: | return $this->hasNextPage; |
103: | } |
104: | |
105: | /** |
106: | * Get Has Previous Page of Model's |
107: | * |
108: | * @return boolean |
109: | */ |
110: | public function getHasPreviousPage() { |
111: | return $this->hasPreviousPage; |
112: | } |
113: | |
114: | |
115: | /** |
116: | * @internal |
117: | * |
118: | * Count elements of an object |
119: | * @link http://php.net/manual/en/countable.count.php |
120: | * @return int The custom count as an integer. |
121: | * </p> |
122: | * <p> |
123: | * The return value is cast to an integer. |
124: | * @since 5.1.0 |
125: | * |
126: | * Note: Temporarily suppress the required return type, to keep compatibility with PHP 5.6 |
127: | * the #[\ReturnTypeWillChange] must be removed once 5.6 |
128: | */ |
129: | #[\ReturnTypeWillChange] |
130: | public function count() { |
131: | return count($this->data); |
132: | } |
133: | /** |
134: | * @internal |
135: | * |
136: | * Whether a offset exists |
137: | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
138: | * @param mixed $offset <p> |
139: | * An offset to check for. |
140: | * </p> |
141: | * @return boolean true on success or false on failure. |
142: | * </p> |
143: | * <p> |
144: | * The return value will be casted to boolean if non-boolean was returned. |
145: | * @since 5.0.0 |
146: | * |
147: | * Note: Temporarily suppress the required return type, to keep compatibility with PHP 5.6 |
148: | * the #[\ReturnTypeWillChange] must be removed once 5.6 |
149: | */ |
150: | #[\ReturnTypeWillChange] |
151: | public function offsetExists($offset) { |
152: | return isset($this->data[$offset]); |
153: | } |
154: | |
155: | /** |
156: | * @internal |
157: | * |
158: | * Offset to retrieve |
159: | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
160: | * @param mixed $offset <p> |
161: | * The offset to retrieve. |
162: | * </p> |
163: | * @return mixed Can return all value types. |
164: | * @since 5.0.0 |
165: | * |
166: | * Note: Temporarily suppress the required return type, to keep compatibility with PHP 5.6 |
167: | * the #[\ReturnTypeWillChange] must be removed once 5.6 |
168: | */ |
169: | #[\ReturnTypeWillChange] |
170: | public function offsetGet($offset) { |
171: | return $this->data[$offset]; |
172: | } |
173: | |
174: | /** |
175: | * @internal |
176: | * |
177: | * Offset to set |
178: | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
179: | * @param mixed $offset <p> |
180: | * The offset to assign the value to. |
181: | * </p> |
182: | * @param mixed $value <p> |
183: | * The value to set. |
184: | * </p> |
185: | * @return void |
186: | * @since 5.0.0 |
187: | * |
188: | * Note: Temporarily suppress the required return type, to keep compatibility with PHP 5.6 |
189: | * the #[\ReturnTypeWillChange] must be removed once 5.6 |
190: | */ |
191: | #[\ReturnTypeWillChange] |
192: | public function offsetSet($offset, $value) { |
193: | $this->data[$offset] = $value; |
194: | } |
195: | |
196: | /** |
197: | * @internal |
198: | * |
199: | * Offset to unset |
200: | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
201: | * @param mixed $offset <p> |
202: | * The offset to unset. |
203: | * </p> |
204: | * @return void |
205: | * @since 5.0.0 |
206: | * |
207: | * Note: Temporarily suppress the required return type, to keep compatibility with PHP 5.6 |
208: | * the #[\ReturnTypeWillChange] must be removed once 5.6 |
209: | */ |
210: | #[\ReturnTypeWillChange] |
211: | public function offsetUnset($offset) { |
212: | unset($this->data[$offset]); |
213: | } |
214: | |
215: | /** |
216: | * @external |
217: | * |
218: | * The links to unset. |
219: | * @method unsetLinksAttribute() |
220: | * @return void |
221: | * @description unsetting links attribute. |
222: | */ |
223: | public function unsetLinksAttribute(){ |
224: | unset($this->links); |
225: | } |
226: | } |
227: |