Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
28 / 28 |
|
100.00% |
18 / 18 |
CRAP | |
100.00% |
1 / 1 |
| AbstractManager | |
100.00% |
28 / 28 |
|
100.00% |
18 / 18 |
24 | |
100.00% |
1 / 1 |
| setItems | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| addItems | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| addItem | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| removeItem | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| getItem | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getItems | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasItem | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasItems | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __set | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __get | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __isset | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __unset | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| offsetSet | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| offsetGet | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| offsetExists | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| offsetUnset | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| count | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getIterator | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | /** |
| 4 | * Pop PHP Framework (https://www.popphp.org/) |
| 5 | * |
| 6 | * @link https://github.com/popphp/popphp-framework |
| 7 | * @author Nick Sagona, III <nick@popphp.org> |
| 8 | * @copyright Copyright (c) 2009-2026 Nick Sagona, III |
| 9 | * @license https://www.popphp.org/license New BSD License |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * @namespace |
| 14 | */ |
| 15 | namespace Pop; |
| 16 | |
| 17 | use ArrayAccess; |
| 18 | use ArrayIterator; |
| 19 | use Countable; |
| 20 | use IteratorAggregate; |
| 21 | |
| 22 | /** |
| 23 | * Abstract manager class |
| 24 | * |
| 25 | * @category Pop |
| 26 | * @package Pop |
| 27 | * @author Nick Sagona, III <nick@popphp.org> |
| 28 | * @copyright Copyright (c) 2009-2026 Nick Sagona, III |
| 29 | * @license https://www.popphp.org/license New BSD License |
| 30 | * @version 5.0.0 |
| 31 | */ |
| 32 | abstract class AbstractManager implements ManagerInterface, ArrayAccess, Countable, IteratorAggregate |
| 33 | { |
| 34 | |
| 35 | /** |
| 36 | * Manager items |
| 37 | * @var array |
| 38 | */ |
| 39 | protected array $items = []; |
| 40 | |
| 41 | /** |
| 42 | * Set items |
| 43 | * |
| 44 | * @param array $items |
| 45 | * @return static |
| 46 | */ |
| 47 | public function setItems(array $items): static |
| 48 | { |
| 49 | $this->items = $items; |
| 50 | return $this; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Add items |
| 55 | * |
| 56 | * @param array $items |
| 57 | * @return static |
| 58 | */ |
| 59 | public function addItems(array $items): static |
| 60 | { |
| 61 | foreach ($items as $name => $item) { |
| 62 | $this->addItem($item, $name); |
| 63 | } |
| 64 | |
| 65 | return $this; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Add an item |
| 70 | * |
| 71 | * @param mixed $item |
| 72 | * @param mixed $name |
| 73 | * @return static |
| 74 | */ |
| 75 | public function addItem(mixed $item, mixed $name = null): static |
| 76 | { |
| 77 | if (($name !== null) && !is_numeric($name)) { |
| 78 | $this->items[$name] = $item; |
| 79 | } else { |
| 80 | $this->items[] = $item; |
| 81 | } |
| 82 | |
| 83 | return $this; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Remove an item |
| 88 | * |
| 89 | * @param mixed $name |
| 90 | * @return static |
| 91 | */ |
| 92 | public function removeItem(mixed $name): static |
| 93 | { |
| 94 | if (isset($this->items[$name])) { |
| 95 | unset($this->items[$name]); |
| 96 | } |
| 97 | |
| 98 | return $this; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Get an item |
| 103 | * |
| 104 | * @param mixed $name |
| 105 | * @return mixed |
| 106 | */ |
| 107 | public function getItem(mixed $name): mixed |
| 108 | { |
| 109 | return $this->items[$name] ?? null; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Get items |
| 114 | * |
| 115 | * @return array |
| 116 | */ |
| 117 | public function getItems(): array |
| 118 | { |
| 119 | return $this->items; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Determine whether the manager has an item |
| 124 | * |
| 125 | * @param string $name |
| 126 | * @return bool |
| 127 | */ |
| 128 | public function hasItem(string $name): bool |
| 129 | { |
| 130 | return (isset($this->items[$name])); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Determine whether the manager has items |
| 135 | * |
| 136 | * @return bool |
| 137 | */ |
| 138 | public function hasItems(): bool |
| 139 | { |
| 140 | return (!empty($this->items)); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Set an item |
| 145 | * |
| 146 | * @param string $name |
| 147 | * @param mixed $value |
| 148 | * @return void |
| 149 | */ |
| 150 | public function __set(string $name, mixed $value): void |
| 151 | { |
| 152 | $this->addItem($value, $name); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Get an item |
| 157 | * |
| 158 | * @param string $name |
| 159 | * @return mixed |
| 160 | */ |
| 161 | public function __get(string $name): mixed |
| 162 | { |
| 163 | return $this->getItem($name); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Determine if an item exists |
| 168 | * |
| 169 | * @param string $name |
| 170 | * @return bool |
| 171 | */ |
| 172 | public function __isset(string $name): bool |
| 173 | { |
| 174 | return $this->hasItem($name); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Unset an item |
| 179 | * |
| 180 | * @param string $name |
| 181 | * @return void |
| 182 | */ |
| 183 | public function __unset(string $name): void |
| 184 | { |
| 185 | if (isset($this->items[$name])) { |
| 186 | unset($this->items[$name]); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Set an item |
| 192 | * |
| 193 | * @param mixed $offset |
| 194 | * @param mixed $value |
| 195 | * @return void |
| 196 | */ |
| 197 | public function offsetSet(mixed $offset, mixed $value): void |
| 198 | { |
| 199 | $this->addItem($value, $offset); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Get an item |
| 204 | * |
| 205 | * @param mixed $offset |
| 206 | * @return mixed |
| 207 | */ |
| 208 | public function offsetGet(mixed $offset): mixed |
| 209 | { |
| 210 | return $this->getItem($offset); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Determine if an item exists |
| 215 | * |
| 216 | * @param string $offset |
| 217 | * @return bool |
| 218 | */ |
| 219 | public function offsetExists($offset): bool |
| 220 | { |
| 221 | return $this->hasItem($offset); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Unset an item |
| 226 | * |
| 227 | * @param mixed $offset |
| 228 | * @return void |
| 229 | */ |
| 230 | public function offsetUnset(mixed $offset): void |
| 231 | { |
| 232 | if (isset($this->items[$offset])) { |
| 233 | unset($this->items[$offset]); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Return count |
| 239 | * |
| 240 | * @return int |
| 241 | */ |
| 242 | public function count(): int |
| 243 | { |
| 244 | return count($this->items); |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Get iterator |
| 249 | * |
| 250 | * @return ArrayIterator |
| 251 | */ |
| 252 | public function getIterator(): ArrayIterator |
| 253 | { |
| 254 | return new ArrayIterator($this->items); |
| 255 | } |
| 256 | |
| 257 | } |