| 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\GCP\Ramsey\Collection\Map; |
| 14 |
|
| 15 |
use Dudlewebs\WPMCS\GCP\Ramsey\Collection\AbstractArray; |
| 16 |
use Dudlewebs\WPMCS\GCP\Ramsey\Collection\Exception\InvalidArgumentException; |
| 17 |
use Traversable; |
| 18 |
use function array_key_exists; |
| 19 |
use function array_keys; |
| 20 |
use function in_array; |
| 21 |
use function var_export; |
| 22 |
/** |
| 23 |
* This class provides a basic implementation of `MapInterface`, to minimize the |
| 24 |
* effort required to implement this interface. |
| 25 |
* |
| 26 |
* @template K of array-key |
| 27 |
* @template T |
| 28 |
* @extends AbstractArray<T> |
| 29 |
* @implements MapInterface<K, T> |
| 30 |
*/ |
| 31 |
abstract class AbstractMap extends AbstractArray implements MapInterface |
| 32 |
{ |
| 33 |
/** |
| 34 |
* @param array<K, T> $data The initial items to add to this map. |
| 35 |
*/ |
| 36 |
public function __construct(array $data = []) |
| 37 |
{ |
| 38 |
parent::__construct($data); |
| 39 |
} |
| 40 |
/** |
| 41 |
* @return Traversable<K, T> |
| 42 |
*/ |
| 43 |
public function getIterator() : Traversable |
| 44 |
{ |
| 45 |
return parent::getIterator(); |
| 46 |
} |
| 47 |
/** |
| 48 |
* @param K $offset The offset to set |
| 49 |
* @param T $value The value to set at the given offset. |
| 50 |
* |
| 51 |
* @inheritDoc |
| 52 |
*/ |
| 53 |
public function offsetSet(mixed $offset, mixed $value) : void |
| 54 |
{ |
| 55 |
if ($offset === null) { |
| 56 |
throw new InvalidArgumentException('Map elements are key/value pairs; a key must be provided for ' . 'value ' . var_export($value, \true)); |
| 57 |
} |
| 58 |
$this->data[$offset] = $value; |
| 59 |
} |
| 60 |
public function containsKey(int|string $key) : bool |
| 61 |
{ |
| 62 |
return array_key_exists($key, $this->data); |
| 63 |
} |
| 64 |
public function containsValue(mixed $value) : bool |
| 65 |
{ |
| 66 |
return in_array($value, $this->data, \true); |
| 67 |
} |
| 68 |
/** |
| 69 |
* @inheritDoc |
| 70 |
*/ |
| 71 |
public function keys() : array |
| 72 |
{ |
| 73 |
/** @var list<K> */ |
| 74 |
return array_keys($this->data); |
| 75 |
} |
| 76 |
/** |
| 77 |
* @param K $key The key to return from the map. |
| 78 |
* @param T | null $defaultValue The default value to use if `$key` is not found. |
| 79 |
* |
| 80 |
* @return T | null the value or `null` if the key could not be found. |
| 81 |
*/ |
| 82 |
public function get(int|string $key, mixed $defaultValue = null) : mixed |
| 83 |
{ |
| 84 |
return $this[$key] ?? $defaultValue; |
| 85 |
} |
| 86 |
/** |
| 87 |
* @param K $key The key to put or replace in the map. |
| 88 |
* @param T $value The value to store at `$key`. |
| 89 |
* |
| 90 |
* @return T | null the previous value associated with key, or `null` if |
| 91 |
* there was no mapping for `$key`. |
| 92 |
*/ |
| 93 |
public function put(int|string $key, mixed $value) : mixed |
| 94 |
{ |
| 95 |
$previousValue = $this->get($key); |
| 96 |
$this[$key] = $value; |
| 97 |
return $previousValue; |
| 98 |
} |
| 99 |
/** |
| 100 |
* @param K $key The key to put in the map. |
| 101 |
* @param T $value The value to store at `$key`. |
| 102 |
* |
| 103 |
* @return T | null the previous value associated with key, or `null` if |
| 104 |
* there was no mapping for `$key`. |
| 105 |
*/ |
| 106 |
public function putIfAbsent(int|string $key, mixed $value) : mixed |
| 107 |
{ |
| 108 |
$currentValue = $this->get($key); |
| 109 |
if ($currentValue === null) { |
| 110 |
$this[$key] = $value; |
| 111 |
} |
| 112 |
return $currentValue; |
| 113 |
} |
| 114 |
/** |
| 115 |
* @param K $key The key to remove from the map. |
| 116 |
* |
| 117 |
* @return T | null the previous value associated with key, or `null` if |
| 118 |
* there was no mapping for `$key`. |
| 119 |
*/ |
| 120 |
public function remove(int|string $key) : mixed |
| 121 |
{ |
| 122 |
$previousValue = $this->get($key); |
| 123 |
unset($this[$key]); |
| 124 |
return $previousValue; |
| 125 |
} |
| 126 |
public function removeIf(int|string $key, mixed $value) : bool |
| 127 |
{ |
| 128 |
if ($this->get($key) === $value) { |
| 129 |
unset($this[$key]); |
| 130 |
return \true; |
| 131 |
} |
| 132 |
return \false; |
| 133 |
} |
| 134 |
/** |
| 135 |
* @param K $key The key to replace. |
| 136 |
* @param T $value The value to set at `$key`. |
| 137 |
* |
| 138 |
* @return T | null the previous value associated with key, or `null` if |
| 139 |
* there was no mapping for `$key`. |
| 140 |
*/ |
| 141 |
public function replace(int|string $key, mixed $value) : mixed |
| 142 |
{ |
| 143 |
$currentValue = $this->get($key); |
| 144 |
if ($this->containsKey($key)) { |
| 145 |
$this[$key] = $value; |
| 146 |
} |
| 147 |
return $currentValue; |
| 148 |
} |
| 149 |
public function replaceIf(int|string $key, mixed $oldValue, mixed $newValue) : bool |
| 150 |
{ |
| 151 |
if ($this->get($key) === $oldValue) { |
| 152 |
$this[$key] = $newValue; |
| 153 |
return \true; |
| 154 |
} |
| 155 |
return \false; |
| 156 |
} |
| 157 |
/** |
| 158 |
* @return array<K, T> |
| 159 |
*/ |
| 160 |
public function __serialize() : array |
| 161 |
{ |
| 162 |
/** @var array<K, T> */ |
| 163 |
return parent::__serialize(); |
| 164 |
} |
| 165 |
/** |
| 166 |
* @return array<K, T> |
| 167 |
*/ |
| 168 |
public function toArray() : array |
| 169 |
{ |
| 170 |
/** @var array<K, T> */ |
| 171 |
return parent::toArray(); |
| 172 |
} |
| 173 |
} |
| 174 |
|