googleanalytics
/
lib
/
analytics-admin
/
vendor
/
ramsey
/
collection
/
src
/
Map
/
AbstractMap.php
AbstractMap.php
3 years ago
AbstractTypedMap.php
3 years ago
AssociativeArrayMap.php
3 years ago
MapInterface.php
3 years ago
NamedParameterMap.php
3 years ago
TypedMap.php
3 years ago
TypedMapInterface.php
3 years ago
AbstractMap.php
163 lines
| 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 | |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace Ramsey\Collection\Map; |
| 16 | |
| 17 | use Ramsey\Collection\AbstractArray; |
| 18 | use Ramsey\Collection\Exception\InvalidArgumentException; |
| 19 | |
| 20 | use function array_key_exists; |
| 21 | use function array_keys; |
| 22 | use function in_array; |
| 23 | |
| 24 | /** |
| 25 | * This class provides a basic implementation of `MapInterface`, to minimize the |
| 26 | * effort required to implement this interface. |
| 27 | * |
| 28 | * @template T |
| 29 | * @extends AbstractArray<T> |
| 30 | * @implements MapInterface<T> |
| 31 | */ |
| 32 | abstract class AbstractMap extends AbstractArray implements MapInterface |
| 33 | { |
| 34 | /** |
| 35 | * @inheritDoc |
| 36 | */ |
| 37 | public function offsetSet($offset, $value): void |
| 38 | { |
| 39 | if ($offset === null) { |
| 40 | throw new InvalidArgumentException( |
| 41 | 'Map elements are key/value pairs; a key must be provided for ' |
| 42 | . 'value ' . var_export($value, true) |
| 43 | ); |
| 44 | } |
| 45 | |
| 46 | $this->data[$offset] = $value; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @inheritDoc |
| 51 | */ |
| 52 | public function containsKey($key): bool |
| 53 | { |
| 54 | return array_key_exists($key, $this->data); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * @inheritDoc |
| 59 | */ |
| 60 | public function containsValue($value): bool |
| 61 | { |
| 62 | return in_array($value, $this->data, true); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @inheritDoc |
| 67 | */ |
| 68 | public function keys(): array |
| 69 | { |
| 70 | return array_keys($this->data); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @inheritDoc |
| 75 | */ |
| 76 | public function get($key, $defaultValue = null) |
| 77 | { |
| 78 | if (!$this->containsKey($key)) { |
| 79 | return $defaultValue; |
| 80 | } |
| 81 | |
| 82 | return $this[$key]; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * @inheritDoc |
| 87 | */ |
| 88 | public function put($key, $value) |
| 89 | { |
| 90 | $previousValue = $this->get($key); |
| 91 | $this[$key] = $value; |
| 92 | |
| 93 | return $previousValue; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @inheritDoc |
| 98 | */ |
| 99 | public function putIfAbsent($key, $value) |
| 100 | { |
| 101 | $currentValue = $this->get($key); |
| 102 | |
| 103 | if ($currentValue === null) { |
| 104 | $this[$key] = $value; |
| 105 | } |
| 106 | |
| 107 | return $currentValue; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * @inheritDoc |
| 112 | */ |
| 113 | public function remove($key) |
| 114 | { |
| 115 | $previousValue = $this->get($key); |
| 116 | unset($this[$key]); |
| 117 | |
| 118 | return $previousValue; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * @inheritDoc |
| 123 | */ |
| 124 | public function removeIf($key, $value): bool |
| 125 | { |
| 126 | if ($this->get($key) === $value) { |
| 127 | unset($this[$key]); |
| 128 | |
| 129 | return true; |
| 130 | } |
| 131 | |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * @inheritDoc |
| 137 | */ |
| 138 | public function replace($key, $value) |
| 139 | { |
| 140 | $currentValue = $this->get($key); |
| 141 | |
| 142 | if ($this->containsKey($key)) { |
| 143 | $this[$key] = $value; |
| 144 | } |
| 145 | |
| 146 | return $currentValue; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * @inheritDoc |
| 151 | */ |
| 152 | public function replaceIf($key, $oldValue, $newValue): bool |
| 153 | { |
| 154 | if ($this->get($key) === $oldValue) { |
| 155 | $this[$key] = $newValue; |
| 156 | |
| 157 | return true; |
| 158 | } |
| 159 | |
| 160 | return false; |
| 161 | } |
| 162 | } |
| 163 |