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