| 1 |
<?php |
| 2 |
|
| 3 |
namespace ipl\Stdlib; |
| 4 |
|
| 5 |
use Generator; |
| 6 |
use OutOfBoundsException; |
| 7 |
use Traversable; |
| 8 |
|
| 9 |
/** |
| 10 |
* Trait for property access, mutation, and array access |
| 11 |
*/ |
| 12 |
trait Properties |
| 13 |
{ |
| 14 |
/** @var array<string, mixed> */ |
| 15 |
private array $properties = []; |
| 16 |
|
| 17 |
/** |
| 18 |
* Get whether this class has any properties |
| 19 |
* |
| 20 |
* @return bool |
| 21 |
*/ |
| 22 |
public function hasProperties(): bool |
| 23 |
{ |
| 24 |
return ! empty($this->properties); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Get whether a property with the given key exists |
| 29 |
* |
| 30 |
* @param string $key |
| 31 |
* |
| 32 |
* @return bool |
| 33 |
*/ |
| 34 |
public function hasProperty(string $key): bool |
| 35 |
{ |
| 36 |
return array_key_exists($key, $this->properties); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Set the given properties |
| 41 |
* |
| 42 |
* @param array<string, mixed> $properties |
| 43 |
* |
| 44 |
* @return $this |
| 45 |
*/ |
| 46 |
public function setProperties(array $properties): static |
| 47 |
{ |
| 48 |
foreach ($properties as $key => $value) { |
| 49 |
$this->setProperty($key, $value); |
| 50 |
} |
| 51 |
|
| 52 |
return $this; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Get the property by the given key |
| 57 |
* |
| 58 |
* @param string $key |
| 59 |
* |
| 60 |
* @return mixed |
| 61 |
* |
| 62 |
* @throws OutOfBoundsException If the property by the given key does not exist |
| 63 |
*/ |
| 64 |
protected function getProperty(string $key): mixed |
| 65 |
{ |
| 66 |
if (array_key_exists($key, $this->properties)) { |
| 67 |
return $this->properties[$key]; |
| 68 |
} |
| 69 |
|
| 70 |
throw new OutOfBoundsException("Can't access property '$key'. Property does not exist"); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Set a property with the given key and value |
| 75 |
* |
| 76 |
* @param string $key |
| 77 |
* @param mixed $value |
| 78 |
* |
| 79 |
* @return $this |
| 80 |
*/ |
| 81 |
protected function setProperty(string $key, mixed $value): static |
| 82 |
{ |
| 83 |
$this->properties[$key] = $value; |
| 84 |
|
| 85 |
return $this; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Iterate over all existing properties |
| 90 |
* |
| 91 |
* @return Generator<string, mixed> |
| 92 |
*/ |
| 93 |
public function getIterator(): Traversable |
| 94 |
{ |
| 95 |
foreach ($this->properties as $key => $value) { |
| 96 |
yield $key => $value; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Check whether an offset exists |
| 102 |
* |
| 103 |
* @param mixed $offset |
| 104 |
* |
| 105 |
* @return bool |
| 106 |
*/ |
| 107 |
public function offsetExists(mixed $offset): bool |
| 108 |
{ |
| 109 |
return isset($this->properties[$offset]); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Get the value for an offset |
| 114 |
* |
| 115 |
* @param mixed $offset |
| 116 |
* |
| 117 |
* @return mixed |
| 118 |
*/ |
| 119 |
public function offsetGet(mixed $offset): mixed |
| 120 |
{ |
| 121 |
return $this->getProperty($offset); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Set the value for an offset |
| 126 |
* |
| 127 |
* @param mixed $offset |
| 128 |
* @param mixed $value |
| 129 |
* |
| 130 |
* @return void |
| 131 |
*/ |
| 132 |
public function offsetSet(mixed $offset, mixed $value): void |
| 133 |
{ |
| 134 |
$this->setProperty($offset, $value); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Unset the value for an offset |
| 139 |
* |
| 140 |
* @param mixed $offset |
| 141 |
* |
| 142 |
* @return void |
| 143 |
*/ |
| 144 |
public function offsetUnset(mixed $offset): void |
| 145 |
{ |
| 146 |
unset($this->properties[$offset]); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Get the value of a non-public property |
| 151 |
* |
| 152 |
* This is a PHP magic method which is implicitly called upon access to non-public properties, |
| 153 |
* e.g. `$value = $object->property;`. |
| 154 |
* Do not call this method directly. |
| 155 |
* |
| 156 |
* @param string $key |
| 157 |
* |
| 158 |
* @return mixed |
| 159 |
*/ |
| 160 |
public function __get(string $key): mixed |
| 161 |
{ |
| 162 |
return $this->getProperty($key); |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Set the value of a non-public property |
| 167 |
* |
| 168 |
* This is a PHP magic method which is implicitly called upon access to non-public properties, |
| 169 |
* e.g. `$object->property = $value;`. |
| 170 |
* Do not call this method directly. |
| 171 |
* |
| 172 |
* @param string $key |
| 173 |
* @param mixed $value |
| 174 |
* |
| 175 |
* @return void |
| 176 |
*/ |
| 177 |
public function __set(string $key, mixed $value): void |
| 178 |
{ |
| 179 |
$this->setProperty($key, $value); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Check whether a non-public property is defined and not null |
| 184 |
* |
| 185 |
* This is a PHP magic method which is implicitly called upon access to non-public properties, |
| 186 |
* e.g. `isset($object->property);`. |
| 187 |
* Do not call this method directly. |
| 188 |
* |
| 189 |
* @param string $key |
| 190 |
* |
| 191 |
* @return bool |
| 192 |
*/ |
| 193 |
public function __isset(string $key): bool |
| 194 |
{ |
| 195 |
return $this->offsetExists($key); |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Unset the value of a non-public property |
| 200 |
* |
| 201 |
* This is a PHP magic method which is implicitly called upon access to non-public properties, |
| 202 |
* e.g. `unset($object->property);`. This method does nothing if the property does not exist. |
| 203 |
* Do not call this method directly. |
| 204 |
* |
| 205 |
* @param string $key |
| 206 |
* |
| 207 |
* @return void |
| 208 |
*/ |
| 209 |
public function __unset(string $key): void |
| 210 |
{ |
| 211 |
$this->offsetUnset($key); |
| 212 |
} |
| 213 |
} |