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