Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
28 / 28 |
|
100.00% |
15 / 15 |
CRAP | |
100.00% |
1 / 1 |
MydbRegistry | |
100.00% |
28 / 28 |
|
100.00% |
15 / 15 |
21 | |
100.00% |
1 / 1 |
serialize | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
unserialize | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
current | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
key | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
next | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
rewind | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
valid | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
count | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
offsetExists | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
offsetGet | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
offsetSet | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
offsetUnset | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
__clone | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
__serialize | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
__unserialize | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | /** |
3 | * This file is part of the sshilko/php-sql-mydb package. |
4 | * |
5 | * (c) Sergei Shilko <contact@sshilko.com> |
6 | * |
7 | * MIT License |
8 | * |
9 | * For the full copyright and license information, please view the LICENSE |
10 | * file that was distributed with this source code. |
11 | * @license https://opensource.org/licenses/mit-license.php MIT |
12 | */ |
13 | |
14 | declare(strict_types = 1); |
15 | |
16 | namespace sql; |
17 | |
18 | use ArrayAccess; |
19 | use Countable; |
20 | use Iterator; |
21 | use Serializable; |
22 | use sql\MydbException\RegistryException; |
23 | use Traversable; |
24 | use function count; |
25 | use function current; |
26 | use function key; |
27 | use function next; |
28 | use function reset; |
29 | use function serialize; |
30 | |
31 | /** |
32 | * Singleton or registry helper to manage multiple Mydb instances |
33 | * |
34 | * @author Sergei Shilko <contact@sshilko.com> |
35 | * @license https://opensource.org/licenses/mit-license.php MIT |
36 | * @see https://github.com/sshilko/php-sql-mydb |
37 | * |
38 | * @psalm-suppress MissingTemplateParam |
39 | */ |
40 | class MydbRegistry implements ArrayAccess, Countable, Traversable, Iterator, Serializable |
41 | { |
42 | |
43 | /** |
44 | * @var array<string, \sql\MydbInterface> |
45 | */ |
46 | protected array $instance = []; |
47 | |
48 | /** |
49 | * @throws \sql\MydbException\RegistryException |
50 | */ |
51 | public function serialize(): ?string |
52 | { |
53 | throw new RegistryException(); |
54 | } |
55 | |
56 | /** |
57 | * @throws \sql\MydbException\RegistryException |
58 | * @phpcs:disable SlevomatCodingStandard.Functions.UnusedParameter.UnusedParameter |
59 | */ |
60 | public function unserialize($data): void |
61 | { |
62 | throw new RegistryException(serialize($data)); |
63 | } |
64 | |
65 | /** |
66 | * Return the current element |
67 | */ |
68 | public function current(): ?MydbInterface |
69 | { |
70 | $result = current($this->instance); |
71 | if (false === $result) { |
72 | return null; |
73 | } |
74 | |
75 | return $result; |
76 | } |
77 | |
78 | /** |
79 | * Return the key of the current element |
80 | */ |
81 | public function key(): ?string |
82 | { |
83 | $result = key($this->instance); |
84 | if (null === $result) { |
85 | return null; |
86 | } |
87 | |
88 | return $result; |
89 | } |
90 | |
91 | /** |
92 | * Move forward to next element |
93 | */ |
94 | public function next(): void |
95 | { |
96 | next($this->instance); |
97 | } |
98 | |
99 | /** |
100 | * Rewind the Iterator to the first element |
101 | */ |
102 | public function rewind(): void |
103 | { |
104 | reset($this->instance); |
105 | } |
106 | |
107 | /** |
108 | * Checks if current position is valid |
109 | * @return bool The return value will be boolean and then evaluated. |
110 | * Returns true on success or false on failure. |
111 | */ |
112 | public function valid(): bool |
113 | { |
114 | return false !== current($this->instance); |
115 | } |
116 | |
117 | public function count(): int |
118 | { |
119 | return count($this->instance); |
120 | } |
121 | |
122 | /** |
123 | * Whether an offset exists |
124 | * |
125 | * @phpcs:disable SlevomatCodingStandard.TypeHints.DisallowMixedTypeHint.DisallowedMixedTypeHint |
126 | * @param string $offset |
127 | * @return bool true on success or false on failure. |
128 | */ |
129 | public function offsetExists($offset): bool |
130 | { |
131 | return isset($this->instance[$offset]); |
132 | } |
133 | |
134 | /** |
135 | * Offset to retrieve |
136 | * |
137 | * @phpcs:disable SlevomatCodingStandard.TypeHints.DisallowMixedTypeHint.DisallowedMixedTypeHint |
138 | * @param string $offset |
139 | * @throws \sql\MydbException\RegistryException |
140 | */ |
141 | public function offsetGet($offset): MydbInterface |
142 | { |
143 | if ($this->offsetExists($offset)) { |
144 | return $this->instance[$offset]; |
145 | } |
146 | |
147 | throw new RegistryException(); |
148 | } |
149 | |
150 | /** |
151 | * Offset to set |
152 | * |
153 | * @phpcs:disable SlevomatCodingStandard.TypeHints.DisallowMixedTypeHint.DisallowedMixedTypeHint |
154 | * @param string $offset |
155 | * @param \sql\MydbInterface $value |
156 | * @throws \sql\MydbException\RegistryException |
157 | */ |
158 | public function offsetSet($offset, $value): void |
159 | { |
160 | if ($value instanceof MydbInterface && !$this->offsetExists($offset)) { |
161 | $this->instance[$offset] = $value; |
162 | |
163 | return; |
164 | } |
165 | |
166 | throw new RegistryException(); |
167 | } |
168 | |
169 | /** |
170 | * Offset to unset |
171 | * |
172 | * @phpcs:disable SlevomatCodingStandard.TypeHints.DisallowMixedTypeHint.DisallowedMixedTypeHint |
173 | * @param string $offset |
174 | */ |
175 | public function offsetUnset($offset): void |
176 | { |
177 | if (!$this->offsetExists($offset)) { |
178 | return; |
179 | } |
180 | |
181 | unset($this->instance[$offset]); |
182 | } |
183 | |
184 | /** |
185 | * @throws \sql\MydbException\RegistryException |
186 | */ |
187 | public function __clone() |
188 | { |
189 | throw new RegistryException(); |
190 | } |
191 | |
192 | /** |
193 | * @throws \sql\MydbException\RegistryException |
194 | */ |
195 | public function __serialize(): array |
196 | { |
197 | throw new RegistryException(); |
198 | } |
199 | |
200 | /** |
201 | * @throws \sql\MydbException\RegistryException |
202 | */ |
203 | public function __unserialize(array $data): void |
204 | { |
205 | throw new RegistryException(serialize($data)); |
206 | } |
207 | } |