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