|
5 | 5 | namespace Compwright\PhpSession; |
6 | 6 |
|
7 | 7 | use Countable; |
| 8 | +use ArrayAccess; |
8 | 9 | use RuntimeException; |
9 | 10 |
|
10 | | -class Session implements Countable |
| 11 | +class Session implements ArrayAccess, Countable |
11 | 12 | { |
12 | 13 | protected string $name; |
13 | 14 |
|
@@ -50,7 +51,12 @@ public function __get(string $name) |
50 | 51 | throw new RuntimeException('Session not initialized'); |
51 | 52 | } |
52 | 53 |
|
53 | | - return $this->contents[$name] ?? null; |
| 54 | + if (!isset($this->contents[$name])) { |
| 55 | + \trigger_error('Undefined property: ' . self::class . '::$' . $name, \E_USER_WARNING); |
| 56 | + return null; |
| 57 | + } |
| 58 | + |
| 59 | + return $this->contents[$name]; |
54 | 60 | } |
55 | 61 |
|
56 | 62 | public function __isset(string $name): bool |
@@ -89,10 +95,67 @@ public function __unset(string $name): void |
89 | 95 | throw new RuntimeException('Cannot alter session after it is closed'); |
90 | 96 | } |
91 | 97 |
|
| 98 | + if (!isset($this->contents[$name])) { |
| 99 | + \trigger_error('Undefined property: ' . self::class . '::$' . $name, \E_USER_WARNING); |
| 100 | + return; |
| 101 | + } |
| 102 | + |
92 | 103 | $this->modified = true; |
93 | 104 | unset($this->contents[$name]); |
94 | 105 | } |
95 | 106 |
|
| 107 | + public function offsetSet($name, $value): void |
| 108 | + { |
| 109 | + if (!$this->isInitialized()) { |
| 110 | + throw new RuntimeException('Session not initialized'); |
| 111 | + } |
| 112 | + |
| 113 | + if (!$this->writeable) { |
| 114 | + throw new RuntimeException('Cannot alter session after it is closed'); |
| 115 | + } |
| 116 | + |
| 117 | + $this->modified = true; |
| 118 | + $this->contents[$name] = $value; |
| 119 | + } |
| 120 | + |
| 121 | + public function offsetExists($name): bool |
| 122 | + { |
| 123 | + if (!$this->isInitialized()) { |
| 124 | + throw new RuntimeException('Session not initialized'); |
| 125 | + } |
| 126 | + |
| 127 | + return isset($this->contents[$name]); |
| 128 | + } |
| 129 | + |
| 130 | + public function offsetUnset($name): void |
| 131 | + { |
| 132 | + if (!$this->isInitialized()) { |
| 133 | + throw new RuntimeException('Session not initialized'); |
| 134 | + } |
| 135 | + |
| 136 | + if (!$this->writeable) { |
| 137 | + throw new RuntimeException('Cannot alter session after it is closed'); |
| 138 | + } |
| 139 | + |
| 140 | + if (!isset($this->contents[$name])) { |
| 141 | + \trigger_error('Undefined array key "' . $name . '"', \E_USER_WARNING); |
| 142 | + return; |
| 143 | + } |
| 144 | + |
| 145 | + $this->modified = true; |
| 146 | + unset($this->contents[$name]); |
| 147 | + } |
| 148 | + |
| 149 | + public function offsetGet($name): mixed |
| 150 | + { |
| 151 | + if (!isset($this->contents[$name])) { |
| 152 | + \trigger_error('Undefined array key "' . $name . '"', \E_USER_WARNING); |
| 153 | + return null; |
| 154 | + } |
| 155 | + |
| 156 | + return $this->contents[$name]; |
| 157 | + } |
| 158 | + |
96 | 159 | /** |
97 | 160 | * @param ?array<string, mixed> $contents |
98 | 161 | */ |
|
0 commit comments