1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the webmozart/key-value-store package. |
5
|
|
|
* |
6
|
|
|
* (c) Bernhard Schussek <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Webmozart\KeyValueStore; |
13
|
|
|
|
14
|
|
|
use Webmozart\KeyValueStore\Api\CountableStore; |
15
|
|
|
use Webmozart\KeyValueStore\Api\NoSuchKeyException; |
16
|
|
|
use Webmozart\KeyValueStore\Api\SortableStore; |
17
|
|
|
use Webmozart\KeyValueStore\Util\KeyUtil; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* A key-value store backed by a PHP array. |
21
|
|
|
* |
22
|
|
|
* The contents of the store are lost when the store is released from memory. |
23
|
|
|
* |
24
|
|
|
* @since 1.0 |
25
|
|
|
* |
26
|
|
|
* @author Bernhard Schussek <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class ArrayStore implements SortableStore, CountableStore |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private $array = array(); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Creates a new store. |
37
|
|
|
* |
38
|
|
|
* @param array $array The values to set initially in the store. |
39
|
|
|
*/ |
40
|
192 |
|
public function __construct(array $array = array()) |
41
|
|
|
{ |
42
|
192 |
|
foreach ($array as $key => $value) { |
43
|
2 |
|
$this->set($key, $value); |
44
|
|
|
} |
45
|
192 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
132 |
|
public function set($key, $value) |
51
|
|
|
{ |
52
|
132 |
|
KeyUtil::validate($key); |
53
|
|
|
|
54
|
128 |
|
$this->array[$key] = $value; |
55
|
128 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
9 |
View Code Duplication |
public function get($key, $default = null) |
|
|
|
|
61
|
|
|
{ |
62
|
9 |
|
KeyUtil::validate($key); |
63
|
|
|
|
64
|
7 |
|
if (!array_key_exists($key, $this->array)) { |
65
|
1 |
|
return $default; |
66
|
|
|
} |
67
|
|
|
|
68
|
6 |
|
return $this->array[$key]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
64 |
|
public function getOrFail($key) |
75
|
|
|
{ |
76
|
64 |
|
KeyUtil::validate($key); |
77
|
|
|
|
78
|
60 |
|
if (!array_key_exists($key, $this->array)) { |
79
|
4 |
|
throw NoSuchKeyException::forKey($key); |
80
|
|
|
} |
81
|
|
|
|
82
|
58 |
|
return $this->array[$key]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
26 |
|
public function getMultiple(array $keys, $default = null) |
89
|
|
|
{ |
90
|
26 |
|
KeyUtil::validateMultiple($keys); |
91
|
|
|
|
92
|
22 |
|
$values = array(); |
93
|
|
|
|
94
|
22 |
|
foreach ($keys as $key) { |
95
|
22 |
|
$values[$key] = array_key_exists($key, $this->array) |
96
|
22 |
|
? $this->array[$key] |
97
|
22 |
|
: $default; |
98
|
|
|
} |
99
|
|
|
|
100
|
22 |
|
return $values; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
*/ |
106
|
60 |
|
public function getMultipleOrFail(array $keys) |
107
|
|
|
{ |
108
|
60 |
|
KeyUtil::validateMultiple($keys); |
109
|
|
|
|
110
|
56 |
|
$values = array(); |
111
|
|
|
|
112
|
56 |
|
foreach ($keys as $key) { |
113
|
56 |
|
$values[$key] = $this->getOrFail($key); |
114
|
|
|
} |
115
|
|
|
|
116
|
54 |
|
return $values; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritdoc} |
121
|
|
|
*/ |
122
|
20 |
View Code Duplication |
public function remove($key) |
|
|
|
|
123
|
|
|
{ |
124
|
20 |
|
KeyUtil::validate($key); |
125
|
|
|
|
126
|
16 |
|
$removed = array_key_exists($key, $this->array); |
127
|
|
|
|
128
|
16 |
|
unset($this->array[$key]); |
129
|
|
|
|
130
|
16 |
|
return $removed; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritdoc} |
135
|
|
|
*/ |
136
|
74 |
|
public function exists($key) |
137
|
|
|
{ |
138
|
74 |
|
KeyUtil::validate($key); |
139
|
|
|
|
140
|
68 |
|
return array_key_exists($key, $this->array); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* {@inheritdoc} |
145
|
|
|
*/ |
146
|
186 |
|
public function clear() |
147
|
|
|
{ |
148
|
186 |
|
$this->array = array(); |
149
|
186 |
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* {@inheritdoc} |
153
|
|
|
*/ |
154
|
24 |
|
public function keys() |
155
|
|
|
{ |
156
|
24 |
|
return array_keys($this->array); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Returns the contents of the store as array. |
161
|
|
|
* |
162
|
|
|
* @return array The keys and values in the store. |
163
|
|
|
*/ |
164
|
|
|
public function toArray() |
165
|
|
|
{ |
166
|
|
|
return $this->array; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* {@inheritdoc} |
171
|
|
|
*/ |
172
|
12 |
|
public function sort($flags = SORT_REGULAR) |
173
|
|
|
{ |
174
|
12 |
|
ksort($this->array, $flags); |
175
|
12 |
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* {@inheritdoc} |
179
|
|
|
*/ |
180
|
2 |
|
public function count() |
181
|
|
|
{ |
182
|
2 |
|
return count($this->array); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.