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