| 1: | <?php declare(strict_types=1); |
| 2: | |
| 3: | namespace Salient\Core; |
| 4: | |
| 5: | use ArrayAccess; |
| 6: | use OutOfRangeException; |
| 7: | use ReturnTypeWillChange; |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | final class Graph implements ArrayAccess |
| 15: | { |
| 16: | protected object $Object; |
| 17: | |
| 18: | protected array $Array; |
| 19: | protected bool $IsObject; |
| 20: | protected bool $AddMissingKeys; |
| 21: | protected bool $AddMissingProperties; |
| 22: | |
| 23: | protected array $Path = []; |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | public function __construct( |
| 31: | &$value = [], |
| 32: | bool $addMissingKeys = false, |
| 33: | bool $addMissingProperties = false |
| 34: | ) { |
| 35: | if (is_object($value)) { |
| 36: | $this->Object = $value; |
| 37: | $this->IsObject = true; |
| 38: | } else { |
| 39: | $this->Array = &$value; |
| 40: | $this->IsObject = false; |
| 41: | } |
| 42: | $this->AddMissingKeys = $addMissingKeys; |
| 43: | $this->AddMissingProperties = $addMissingProperties; |
| 44: | } |
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | public function getValue() |
| 52: | { |
| 53: | return $this->IsObject ? $this->Object : $this->Array; |
| 54: | } |
| 55: | |
| 56: | |
| 57: | |
| 58: | |
| 59: | |
| 60: | |
| 61: | public function getPath(): array |
| 62: | { |
| 63: | return $this->Path; |
| 64: | } |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | public function offsetExists($offset): bool |
| 70: | { |
| 71: | return $this->IsObject |
| 72: | ? property_exists($this->Object, (string) $offset) |
| 73: | : array_key_exists($offset, $this->Array); |
| 74: | } |
| 75: | |
| 76: | |
| 77: | |
| 78: | |
| 79: | |
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | |
| 86: | #[ReturnTypeWillChange] |
| 87: | public function offsetGet($offset) |
| 88: | { |
| 89: | if ($this->IsObject) { |
| 90: | if (!property_exists($this->Object, (string) $offset)) { |
| 91: | if (!$this->AddMissingProperties) { |
| 92: | throw new OutOfRangeException(sprintf('Property not found: %s', $offset)); |
| 93: | } |
| 94: | $this->Object->$offset = []; |
| 95: | } |
| 96: | if (!(is_object($this->Object->$offset) || is_array($this->Object->$offset))) { |
| 97: | return $this->Object->$offset; |
| 98: | } |
| 99: | $value = new self($this->Object->$offset, $this->AddMissingKeys, $this->AddMissingProperties); |
| 100: | } else { |
| 101: | if (!array_key_exists($offset, $this->Array)) { |
| 102: | if (!$this->AddMissingKeys) { |
| 103: | throw new OutOfRangeException(sprintf('Key not found: %s', $offset)); |
| 104: | } |
| 105: | $this->Array[$offset] = []; |
| 106: | } |
| 107: | if (!(is_object($this->Array[$offset]) || is_array($this->Array[$offset]))) { |
| 108: | |
| 109: | return $this->Array[$offset]; |
| 110: | } |
| 111: | $value = new self($this->Array[$offset], $this->AddMissingKeys, $this->AddMissingProperties); |
| 112: | } |
| 113: | |
| 114: | $value->Path = $this->Path; |
| 115: | $value->Path[] = $offset; |
| 116: | return $value; |
| 117: | } |
| 118: | |
| 119: | |
| 120: | |
| 121: | |
| 122: | public function offsetSet($offset, $value): void |
| 123: | { |
| 124: | if ($this->IsObject) { |
| 125: | if ($offset === null) { |
| 126: | throw new OutOfRangeException('Invalid offset'); |
| 127: | } |
| 128: | $this->Object->$offset = $value; |
| 129: | } elseif ($offset === null) { |
| 130: | $this->Array[] = $value; |
| 131: | } else { |
| 132: | $this->Array[$offset] = $value; |
| 133: | } |
| 134: | } |
| 135: | |
| 136: | |
| 137: | |
| 138: | |
| 139: | public function offsetUnset($offset): void |
| 140: | { |
| 141: | if ($this->IsObject) { |
| 142: | unset($this->Object->$offset); |
| 143: | } else { |
| 144: | unset($this->Array[$offset]); |
| 145: | } |
| 146: | } |
| 147: | } |
| 148: | |