| 1: | <?php |
| 2: | |
| 3: | /* |
| 4: | * Yate core products API wrapper library |
| 5: | * (c) Alexey Pavlyuts <alexey@pavlyuts.ru> |
| 6: | */ |
| 7: | |
| 8: | namespace Yate\Api; |
| 9: | |
| 10: | /** |
| 11: | * Represent API call result |
| 12: | * |
| 13: | * This class allows to access API answer fields either as associative array |
| 14: | * elements and as class properties. |
| 15: | * |
| 16: | */ |
| 17: | class ApiResponse implements \ArrayAccess |
| 18: | { |
| 19: | |
| 20: | protected array $result; |
| 21: | |
| 22: | /** |
| 23: | * Populates instance with API answer data |
| 24: | * |
| 25: | * @param array $result |
| 26: | */ |
| 27: | public function __construct(array $result) |
| 28: | { |
| 29: | $this->result = $result; |
| 30: | } |
| 31: | |
| 32: | /** |
| 33: | * Returns API answer as associative array |
| 34: | * |
| 35: | * @return array |
| 36: | */ |
| 37: | public function asArray(): array |
| 38: | { |
| 39: | return $this->result; |
| 40: | } |
| 41: | |
| 42: | // Methods to allow array-like and property-like access |
| 43: | public function offsetExists($offset): bool |
| 44: | { |
| 45: | return isset($this->result[$offset]); |
| 46: | } |
| 47: | |
| 48: | public function offsetGet($offset) |
| 49: | { |
| 50: | return $this->result[$offset] ?? null; |
| 51: | } |
| 52: | |
| 53: | public function offsetSet($offset, $value): void |
| 54: | { |
| 55: | // Do nothing, data read-only |
| 56: | } |
| 57: | |
| 58: | public function offsetUnset($offset): void |
| 59: | { |
| 60: | // Do nothing, data read-only |
| 61: | } |
| 62: | |
| 63: | public function __get($name) |
| 64: | { |
| 65: | return $this->result[$name] ?? null; |
| 66: | } |
| 67: | |
| 68: | public function __isset($name) |
| 69: | { |
| 70: | return isset($this->result[$name]); |
| 71: | } |
| 72: | |
| 73: | public function __set($name, $value) |
| 74: | { |
| 75: | // Do nothing, data read-only |
| 76: | } |
| 77: | |
| 78: | public function __unset($name) |
| 79: | { |
| 80: | // Do nothing, data read-only |
| 81: | } |
| 82: | } |
| 83: |