← All changes
|
includes/sdk/google/ramsey/collection/src/Map/AbstractMap.php
+75
-36
1.2.2
→
1.4.1
View file →
| @@ -9,12 +9,13 @@ | ||
| 9 | 9 | * @copyright Copyright (c) Ben Ramsey <ben@benramsey.com> |
| 10 | 10 | * @license http://opensource.org/licenses/MIT MIT |
| 11 | 11 | */ |
| 12 | 12 | declare (strict_types=1); |
| 13 | -namespace Dudlewebs\WPMCS\Ramsey\Collection\Map; | |
| 13 | +namespace Dudlewebs\WPMCS\GCP\Ramsey\Collection\Map; | |
| 14 | 14 | |
| 15 | -use Dudlewebs\WPMCS\Ramsey\Collection\AbstractArray; | |
| 16 | -use Dudlewebs\WPMCS\Ramsey\Collection\Exception\InvalidArgumentException; | |
| 15 | +use Dudlewebs\WPMCS\GCP\Ramsey\Collection\AbstractArray; | |
| 16 | +use Dudlewebs\WPMCS\GCP\Ramsey\Collection\Exception\InvalidArgumentException; | |
| 17 | +use Traversable; | |
| 17 | 18 | use function array_key_exists; |
| 18 | 19 | use function array_keys; |
| 19 | 20 | use function in_array; |
| 20 | 21 | use function var_export; |
| @@ -21,18 +22,36 @@ | ||
| 21 | 22 | /** |
| 22 | 23 | * This class provides a basic implementation of `MapInterface`, to minimize the |
| 23 | 24 | * effort required to implement this interface. |
| 24 | 25 | * |
| 26 | + * @template K of array-key | |
| 25 | 27 | * @template T |
| 26 | 28 | * @extends AbstractArray<T> |
| 27 | - * @implements MapInterface<T> | |
| 29 | + * @implements MapInterface<K, T> | |
| 28 | 30 | */ |
| 29 | 31 | abstract class AbstractMap extends AbstractArray implements MapInterface |
| 30 | 32 | { |
| 31 | 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 | + * | |
| 32 | 51 | * @inheritDoc |
| 33 | 52 | */ |
| 34 | - public function offsetSet($offset, $value): void | |
| 53 | + public function offsetSet(mixed $offset, mixed $value) : void | |
| 35 | 54 | { |
| 36 | 55 | if ($offset === null) { |
| 37 | 56 | throw new InvalidArgumentException('Map elements are key/value pairs; a key must be provided for ' . 'value ' . var_export($value, \true)); |
| 38 | 57 | } |
| @@ -37,19 +56,13 @@ | ||
| 37 | 56 | throw new InvalidArgumentException('Map elements are key/value pairs; a key must be provided for ' . 'value ' . var_export($value, \true)); |
| 38 | 57 | } |
| 39 | 58 | $this->data[$offset] = $value; |
| 40 | 59 | } |
| 41 | - /** | |
| 42 | - * @inheritDoc | |
| 43 | - */ | |
| 44 | - public function containsKey($key): bool | |
| 60 | + public function containsKey(int|string $key) : bool | |
| 45 | 61 | { |
| 46 | 62 | return array_key_exists($key, $this->data); |
| 47 | 63 | } |
| 48 | - /** | |
| 49 | - * @inheritDoc | |
| 50 | - */ | |
| 51 | - public function containsValue($value): bool | |
| 64 | + public function containsValue(mixed $value) : bool | |
| 52 | 65 | { |
| 53 | 66 | return in_array($value, $this->data, \true); |
| 54 | 67 | } |
| 55 | 68 | /** |
| @@ -54,26 +67,31 @@ | ||
| 54 | 67 | } |
| 55 | 68 | /** |
| 56 | 69 | * @inheritDoc |
| 57 | 70 | */ |
| 58 | - public function keys(): array | |
| 71 | + public function keys() : array | |
| 59 | 72 | { |
| 73 | + /** @var list<K> */ | |
| 60 | 74 | return array_keys($this->data); |
| 61 | 75 | } |
| 62 | 76 | /** |
| 63 | - * @inheritDoc | |
| 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. | |
| 64 | 81 | */ |
| 65 | - public function get($key, $defaultValue = null) | |
| 82 | + public function get(int|string $key, mixed $defaultValue = null) : mixed | |
| 66 | 83 | { |
| 67 | - if (!$this->containsKey($key)) { | |
| 68 | - return $defaultValue; | |
| 69 | - } | |
| 70 | - return $this[$key]; | |
| 84 | + return $this[$key] ?? $defaultValue; | |
| 71 | 85 | } |
| 72 | 86 | /** |
| 73 | - * @inheritDoc | |
| 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`. | |
| 74 | 92 | */ |
| 75 | - public function put($key, $value) | |
| 93 | + public function put(int|string $key, mixed $value) : mixed | |
| 76 | 94 | { |
| 77 | 95 | $previousValue = $this->get($key); |
| 78 | 96 | $this[$key] = $value; |
| 79 | 97 | return $previousValue; |
| @@ -78,11 +96,15 @@ | ||
| 78 | 96 | $this[$key] = $value; |
| 79 | 97 | return $previousValue; |
| 80 | 98 | } |
| 81 | 99 | /** |
| 82 | - * @inheritDoc | |
| 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`. | |
| 83 | 105 | */ |
| 84 | - public function putIfAbsent($key, $value) | |
| 106 | + public function putIfAbsent(int|string $key, mixed $value) : mixed | |
| 85 | 107 | { |
| 86 | 108 | $currentValue = $this->get($key); |
| 87 | 109 | if ($currentValue === null) { |
| 88 | 110 | $this[$key] = $value; |
| @@ -89,20 +111,20 @@ | ||
| 89 | 111 | } |
| 90 | 112 | return $currentValue; |
| 91 | 113 | } |
| 92 | 114 | /** |
| 93 | - * @inheritDoc | |
| 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`. | |
| 94 | 119 | */ |
| 95 | - public function remove($key) | |
| 120 | + public function remove(int|string $key) : mixed | |
| 96 | 121 | { |
| 97 | 122 | $previousValue = $this->get($key); |
| 98 | 123 | unset($this[$key]); |
| 99 | 124 | return $previousValue; |
| 100 | 125 | } |
| 101 | - /** | |
| 102 | - * @inheritDoc | |
| 103 | - */ | |
| 104 | - public function removeIf($key, $value): bool | |
| 126 | + public function removeIf(int|string $key, mixed $value) : bool | |
| 105 | 127 | { |
| 106 | 128 | if ($this->get($key) === $value) { |
| 107 | 129 | unset($this[$key]); |
| 108 | 130 | return \true; |
| @@ -109,11 +131,15 @@ | ||
| 109 | 131 | } |
| 110 | 132 | return \false; |
| 111 | 133 | } |
| 112 | 134 | /** |
| 113 | - * @inheritDoc | |
| 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`. | |
| 114 | 140 | */ |
| 115 | - public function replace($key, $value) | |
| 141 | + public function replace(int|string $key, mixed $value) : mixed | |
| 116 | 142 | { |
| 117 | 143 | $currentValue = $this->get($key); |
| 118 | 144 | if ($this->containsKey($key)) { |
| 119 | 145 | $this[$key] = $value; |
| @@ -119,12 +145,9 @@ | ||
| 119 | 145 | $this[$key] = $value; |
| 120 | 146 | } |
| 121 | 147 | return $currentValue; |
| 122 | 148 | } |
| 123 | - /** | |
| 124 | - * @inheritDoc | |
| 125 | - */ | |
| 126 | - public function replaceIf($key, $oldValue, $newValue): bool | |
| 149 | + public function replaceIf(int|string $key, mixed $oldValue, mixed $newValue) : bool | |
| 127 | 150 | { |
| 128 | 151 | if ($this->get($key) === $oldValue) { |
| 129 | 152 | $this[$key] = $newValue; |
| 130 | 153 | return \true; |
| @@ -129,6 +152,22 @@ | ||
| 129 | 152 | $this[$key] = $newValue; |
| 130 | 153 | return \true; |
| 131 | 154 | } |
| 132 | 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(); | |
| 133 | 172 | } |
| 134 | 173 | } |