PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 3.1.0
Defender Security – Malware Scanner, Login Security & Firewall v3.1.0
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 3.1.0, at vendor/ramsey/collection/src/AbstractArray.php

180 lines 4.7 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 serialize;
21 use function unserialize;
22
23 /**
24 * This class provides a basic implementation of `ArrayInterface`, to minimize
25 * the effort required to implement this interface.
26 *
27 * @template T
28 * @template-implements ArrayInterface<T>
29 */
30 abstract class AbstractArray implements ArrayInterface
31 {
32 /**
33 * The items of this array.
34 *
35 * @var array<array-key, T>
36 */
37 protected $data = [];
38
39 /**
40 * Constructs a new array object.
41 *
42 * @param array<array-key, T> $data The initial items to add to this array.
43 */
44 public function __construct(array $data = [])
45 {
46 // Invoke offsetSet() for each value added; in this way, sub-classes
47 // may provide additional logic about values added to the array object.
48 foreach ($data as $key => $value) {
49 $this[$key] = $value;
50 }
51 }
52
53 /**
54 * Returns an iterator for this array.
55 *
56 * @link http://php.net/manual/en/iteratoraggregate.getiterator.php IteratorAggregate::getIterator()
57 *
58 * @return Traversable<T>
59 */
60 public function getIterator(): Traversable
61 {
62 return new ArrayIterator($this->data);
63 }
64
65 /**
66 * Returns `true` if the given offset exists in this array.
67 *
68 * @link http://php.net/manual/en/arrayaccess.offsetexists.php ArrayAccess::offsetExists()
69 *
70 * @param array-key $offset The offset to check.
71 */
72 public function offsetExists($offset): bool
73 {
74 return isset($this->data[$offset]);
75 }
76
77 /**
78 * Returns the value at the specified offset.
79 *
80 * @link http://php.net/manual/en/arrayaccess.offsetget.php ArrayAccess::offsetGet()
81 *
82 * @param array-key $offset The offset for which a value should be returned.
83 *
84 * @return T|null the value stored at the offset, or null if the offset
85 * does not exist.
86 */
87 public function offsetGet($offset)
88 {
89 return $this->data[$offset] ?? null;
90 }
91
92 /**
93 * Sets the given value to the given offset in the array.
94 *
95 * @link http://php.net/manual/en/arrayaccess.offsetset.php ArrayAccess::offsetSet()
96 *
97 * @param array-key|null $offset The offset to set. If `null`, the value may be
98 * set at a numerically-indexed offset.
99 * @param T $value The value to set at the given offset.
100 */
101 // phpcs:ignore SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
102 public function offsetSet($offset, $value): void
103 {
104 if ($offset === null) {
105 $this->data[] = $value;
106 } else {
107 $this->data[$offset] = $value;
108 }
109 }
110
111 /**
112 * Removes the given offset and its value from the array.
113 *
114 * @link http://php.net/manual/en/arrayaccess.offsetunset.php ArrayAccess::offsetUnset()
115 *
116 * @param array-key $offset The offset to remove from the array.
117 */
118 public function offsetUnset($offset): void
119 {
120 unset($this->data[$offset]);
121 }
122
123 /**
124 * Returns a serialized string representation of this array object.
125 *
126 * @link http://php.net/manual/en/serializable.serialize.php Serializable::serialize()
127 *
128 * @return string a PHP serialized string.
129 */
130 public function serialize(): string
131 {
132 return serialize($this->data);
133 }
134
135 /**
136 * Converts a serialized string representation into an instance object.
137 *
138 * @link http://php.net/manual/en/serializable.unserialize.php Serializable::unserialize()
139 *
140 * @param string $serialized A PHP serialized string to unserialize.
141 *
142 * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
143 */
144 public function unserialize($serialized): void
145 {
146 /** @var array<array-key, T> $data */
147 $data = unserialize($serialized, ['allowed_classes' => false]);
148
149 $this->data = $data;
150 }
151
152 /**
153 * Returns the number of items in this array.
154 *
155 * @link http://php.net/manual/en/countable.count.php Countable::count()
156 */
157 public function count(): int
158 {
159 return count($this->data);
160 }
161
162 public function clear(): void
163 {
164 $this->data = [];
165 }
166
167 /**
168 * @inheritDoc
169 */
170 public function toArray(): array
171 {
172 return $this->data;
173 }
174
175 public function isEmpty(): bool
176 {
177 return count($this->data) === 0;
178 }
179 }
180