PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.6.1
Defender Security – Malware Scanner, Login Security & Firewall v5.6.1
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / vendor / ramsey / collection / src / AbstractArray.php

AbstractArray.php in Defender Security – Malware Scanner, Login Security & Firewall 5.6.1, at vendor/ramsey/collection/src/AbstractArray.php

209 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * This file is part of the ramsey/collection library
5 *
6 * For the full copyright and license information, please view the LICENSE
7 * file that was distributed with this source code.
8 *
9 * @copyright Copyright (c) Ben Ramsey <[email protected]>
10 * @license http://opensource.org/licenses/MIT MIT
11 */
12
13 declare(strict_types=1);
14
15 namespace Ramsey\Collection;
16
17 use ArrayIterator;
18 use Traversable;
19
20 use function count;
21 use function serialize;
22 use function unserialize;
23
24 /**
25 * This class provides a basic implementation of `ArrayInterface`, to minimize
26 * the effort required to implement this interface.
27 *
28 * @template T
29 * @implements ArrayInterface<T>
30 */
31 abstract class AbstractArray implements ArrayInterface
32 {
33 /**
34 * The items of this array.
35 *
36 * @var array<array-key, T>
37 */
38 protected array $data = [];
39
40 /**
41 * Constructs a new array object.
42 *
43 * @param array<array-key, T> $data The initial items to add to this array.
44 */
45 public function __construct(array $data = [])
46 {
47 // Invoke offsetSet() for each value added; in this way, sub-classes
48 // may provide additional logic about values added to the array object.
49 foreach ($data as $key => $value) {
50 $this[$key] = $value;
51 }
52 }
53
54 /**
55 * Returns an iterator for this array.
56 *
57 * @link http://php.net/manual/en/iteratoraggregate.getiterator.php IteratorAggregate::getIterator()
58 *
59 * @return Traversable<array-key, T>
60 */
61 public function getIterator(): Traversable
62 {
63 return new ArrayIterator($this->data);
64 }
65
66 /**
67 * Returns `true` if the given offset exists in this array.
68 *
69 * @link http://php.net/manual/en/arrayaccess.offsetexists.php ArrayAccess::offsetExists()
70 *
71 * @param array-key $offset The offset to check.
72 */
73 public function offsetExists($offset): bool
74 {
75 return isset($this->data[$offset]);
76 }
77
78 /**
79 * Returns the value at the specified offset.
80 *
81 * @link http://php.net/manual/en/arrayaccess.offsetget.php ArrayAccess::offsetGet()
82 *
83 * @param array-key $offset The offset for which a value should be returned.
84 *
85 * @return T|null the value stored at the offset, or null if the offset
86 * does not exist.
87 */
88 #[\ReturnTypeWillChange] // phpcs:ignore
89 public function offsetGet($offset)
90 {
91 return $this->data[$offset] ?? null;
92 }
93
94 /**
95 * Sets the given value to the given offset in the array.
96 *
97 * @link http://php.net/manual/en/arrayaccess.offsetset.php ArrayAccess::offsetSet()
98 *
99 * @param array-key|null $offset The offset to set. If `null`, the value may be
100 * set at a numerically-indexed offset.
101 * @param T $value The value to set at the given offset.
102 */
103 // phpcs:ignore SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
104 public function offsetSet($offset, $value): void
105 {
106 if ($offset === null) {
107 $this->data[] = $value;
108 } else {
109 $this->data[$offset] = $value;
110 }
111 }
112
113 /**
114 * Removes the given offset and its value from the array.
115 *
116 * @link http://php.net/manual/en/arrayaccess.offsetunset.php ArrayAccess::offsetUnset()
117 *
118 * @param array-key $offset The offset to remove from the array.
119 */
120 public function offsetUnset($offset): void
121 {
122 unset($this->data[$offset]);
123 }
124
125 /**
126 * Returns a serialized string representation of this array object.
127 *
128 * @deprecated The Serializable interface will go away in PHP 9.
129 *
130 * @link http://php.net/manual/en/serializable.serialize.php Serializable::serialize()
131 *
132 * @return string a PHP serialized string.
133 */
134 public function serialize(): string
135 {
136 return serialize($this->data);
137 }
138
139 /**
140 * Returns data suitable for PHP serialization.
141 *
142 * @link https://www.php.net/manual/en/language.oop5.magic.php#language.oop5.magic.serialize
143 * @link https://www.php.net/serialize
144 *
145 * @return array<array-key, T>
146 */
147 public function __serialize(): array
148 {
149 return $this->data;
150 }
151
152 /**
153 * Converts a serialized string representation into an instance object.
154 *
155 * @deprecated The Serializable interface will go away in PHP 9.
156 *
157 * @link http://php.net/manual/en/serializable.unserialize.php Serializable::unserialize()
158 *
159 * @param string $serialized A PHP serialized string to unserialize.
160 *
161 * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
162 */
163 public function unserialize($serialized): void
164 {
165 /** @var array<array-key, T> $data */
166 $data = unserialize($serialized, ['allowed_classes' => false]);
167
168 $this->data = $data;
169 }
170
171 /**
172 * Adds unserialized data to the object.
173 *
174 * @param array<array-key, T> $data
175 */
176 public function __unserialize(array $data): void
177 {
178 $this->data = $data;
179 }
180
181 /**
182 * Returns the number of items in this array.
183 *
184 * @link http://php.net/manual/en/countable.count.php Countable::count()
185 */
186 public function count(): int
187 {
188 return count($this->data);
189 }
190
191 public function clear(): void
192 {
193 $this->data = [];
194 }
195
196 /**
197 * @inheritDoc
198 */
199 public function toArray(): array
200 {
201 return $this->data;
202 }
203
204 public function isEmpty(): bool
205 {
206 return count($this->data) === 0;
207 }
208 }
209