|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the ramsey/collection library |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright Copyright (c) Ben Ramsey <[email protected]> |
|
9
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
|
10
|
|
|
* @link https://benramsey.com/projects/ramsey-collection/ Documentation |
|
11
|
|
|
* @link https://packagist.org/packages/ramsey/collection Packagist |
|
12
|
|
|
* @link https://github.com/ramsey/collection GitHub |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Ramsey\Collection\Map; |
|
16
|
|
|
|
|
17
|
|
|
use Ramsey\Collection\AbstractArray; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* This class provides an implementation of the MapInterface, to |
|
21
|
|
|
* minimize the effort required to implement this interface |
|
22
|
|
|
*/ |
|
23
|
|
|
abstract class AbstractMap extends AbstractArray implements MapInterface |
|
24
|
|
|
{ |
|
25
|
|
|
public function offsetSet($offset, $value) |
|
26
|
|
|
{ |
|
27
|
|
|
if ($offset === null) { |
|
28
|
|
|
throw new \InvalidArgumentException( |
|
29
|
|
|
'Map elements are key/value pairs; a key must be provided for ' |
|
30
|
|
|
. 'value ' . (string) $value |
|
31
|
|
|
); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$this->data[$offset] = $value; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function containsKey($key) |
|
38
|
|
|
{ |
|
39
|
|
|
return array_key_exists($key, $this->data); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function containsValue($value) |
|
43
|
|
|
{ |
|
44
|
|
|
return in_array($value, $this->data, true); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function keys() |
|
48
|
|
|
{ |
|
49
|
|
|
return array_keys($this->data); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function get($key, $defaultValue = null) |
|
53
|
|
|
{ |
|
54
|
|
|
if (!$this->containsKey($key)) { |
|
55
|
|
|
return $defaultValue; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $this[$key]; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function put($key, $value) |
|
62
|
|
|
{ |
|
63
|
|
|
$previousValue = $this->get($key); |
|
64
|
|
|
$this[$key] = $value; |
|
65
|
|
|
|
|
66
|
|
|
return $previousValue; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
View Code Duplication |
public function putIfAbsent($key, $value) |
|
70
|
|
|
{ |
|
71
|
|
|
$currentValue = $this->get($key); |
|
72
|
|
|
|
|
73
|
|
|
if ($currentValue === null) { |
|
74
|
|
|
$this[$key] = $value; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return $currentValue; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function remove($key) |
|
81
|
|
|
{ |
|
82
|
|
|
$previousValue = $this->get($key); |
|
83
|
|
|
unset($this[$key]); |
|
84
|
|
|
|
|
85
|
|
|
return $previousValue; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function removeIf($key, $value) |
|
89
|
|
|
{ |
|
90
|
|
|
if ($this->get($key) === $value) { |
|
91
|
|
|
unset($this[$key]); |
|
92
|
|
|
|
|
93
|
|
|
return true; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return false; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
View Code Duplication |
public function replace($key, $value) |
|
100
|
|
|
{ |
|
101
|
|
|
$currentValue = $this->get($key); |
|
102
|
|
|
|
|
103
|
|
|
if ($this->containsKey($key)) { |
|
104
|
|
|
$this[$key] = $value; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $currentValue; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function replaceIf($key, $oldValue, $newValue) |
|
111
|
|
|
{ |
|
112
|
|
|
if ($this->get($key) === $oldValue) { |
|
113
|
|
|
$this[$key] = $newValue; |
|
114
|
|
|
|
|
115
|
|
|
return true; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return false; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|