|
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
|
|
|
* Flag: Enable serialization. |
|
32
|
|
|
*/ |
|
33
|
|
|
const SERIALIZE = 1; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var array |
|
37
|
|
|
*/ |
|
38
|
|
|
private $array = array(); |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var callable |
|
42
|
|
|
*/ |
|
43
|
|
|
private $serialize; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var callable |
|
47
|
|
|
*/ |
|
48
|
|
|
private $unserialize; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Creates a new store. |
|
52
|
|
|
* |
|
53
|
|
|
* @param array $array The values to set initially in the store. |
|
54
|
|
|
*/ |
|
55
|
192 |
|
public function __construct(array $array = array(), $flags = 0) |
|
56
|
|
|
{ |
|
57
|
192 |
|
if ($flags & self::SERIALIZE) { |
|
58
|
93 |
|
$this->serialize = array( |
|
59
|
|
|
'Webmozart\KeyValueStore\Util\Serializer', |
|
60
|
|
|
'serialize' |
|
61
|
|
|
); |
|
62
|
93 |
|
$this->unserialize = array( |
|
63
|
93 |
|
'Webmozart\KeyValueStore\Util\Serializer', |
|
64
|
|
|
'unserialize' |
|
65
|
|
|
); |
|
66
|
|
|
} else { |
|
67
|
|
|
$this->serialize = function ($value) { return $value; }; |
|
|
|
|
|
|
68
|
|
|
$this->unserialize = function ($value) { return $value; }; |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
192 |
|
foreach ($array as $key => $value) { |
|
72
|
2 |
|
$this->set($key, $value); |
|
73
|
|
|
} |
|
74
|
192 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* {@inheritdoc} |
|
78
|
|
|
*/ |
|
79
|
132 |
|
public function set($key, $value) |
|
80
|
|
|
{ |
|
81
|
132 |
|
KeyUtil::validate($key); |
|
82
|
|
|
|
|
83
|
128 |
|
$this->array[$key] = call_user_func($this->serialize, $value); |
|
84
|
128 |
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* {@inheritdoc} |
|
88
|
|
|
*/ |
|
89
|
12 |
View Code Duplication |
public function get($key, $default = null) |
|
|
|
|
|
|
90
|
|
|
{ |
|
91
|
12 |
|
KeyUtil::validate($key); |
|
92
|
|
|
|
|
93
|
8 |
|
if (!array_key_exists($key, $this->array)) { |
|
94
|
2 |
|
return $default; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
6 |
|
return call_user_func($this->unserialize, $this->array[$key]); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* {@inheritdoc} |
|
102
|
|
|
*/ |
|
103
|
60 |
View Code Duplication |
public function getOrFail($key) |
|
|
|
|
|
|
104
|
|
|
{ |
|
105
|
60 |
|
KeyUtil::validate($key); |
|
106
|
|
|
|
|
107
|
56 |
|
if (!array_key_exists($key, $this->array)) { |
|
108
|
2 |
|
throw NoSuchKeyException::forKey($key); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
54 |
|
return call_user_func($this->unserialize, $this->array[$key]); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* {@inheritdoc} |
|
116
|
|
|
*/ |
|
117
|
80 |
|
public function getMultiple(array $keys, $default = null) |
|
118
|
|
|
{ |
|
119
|
80 |
|
KeyUtil::validateMultiple($keys); |
|
120
|
|
|
|
|
121
|
76 |
|
$values = array(); |
|
122
|
|
|
|
|
123
|
76 |
|
foreach ($keys as $key) { |
|
124
|
76 |
|
$values[$key] = array_key_exists($key, $this->array) |
|
125
|
76 |
|
? call_user_func($this->unserialize, $this->array[$key]) |
|
126
|
76 |
|
: $default; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
76 |
|
return $values; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* {@inheritdoc} |
|
134
|
|
|
*/ |
|
135
|
60 |
|
public function getMultipleOrFail(array $keys) |
|
136
|
|
|
{ |
|
137
|
60 |
|
KeyUtil::validateMultiple($keys); |
|
138
|
|
|
|
|
139
|
56 |
|
$notFoundKeys = array_diff($keys, array_keys($this->array)); |
|
140
|
|
|
|
|
141
|
56 |
|
if (count($notFoundKeys) > 0) { |
|
142
|
2 |
|
throw NoSuchKeyException::forKeys($notFoundKeys); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
54 |
|
return $this->getMultiple($keys); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* {@inheritdoc} |
|
150
|
|
|
*/ |
|
151
|
20 |
|
public function remove($key) |
|
152
|
|
|
{ |
|
153
|
20 |
|
KeyUtil::validate($key); |
|
154
|
|
|
|
|
155
|
16 |
|
$removed = array_key_exists($key, $this->array); |
|
156
|
|
|
|
|
157
|
16 |
|
unset($this->array[$key]); |
|
158
|
|
|
|
|
159
|
16 |
|
return $removed; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* {@inheritdoc} |
|
164
|
|
|
*/ |
|
165
|
60 |
|
public function exists($key) |
|
166
|
|
|
{ |
|
167
|
60 |
|
KeyUtil::validate($key); |
|
168
|
|
|
|
|
169
|
56 |
|
return array_key_exists($key, $this->array); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* {@inheritdoc} |
|
174
|
|
|
*/ |
|
175
|
186 |
|
public function clear() |
|
176
|
|
|
{ |
|
177
|
186 |
|
$this->array = array(); |
|
178
|
186 |
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* {@inheritdoc} |
|
182
|
|
|
*/ |
|
183
|
24 |
|
public function keys() |
|
184
|
|
|
{ |
|
185
|
24 |
|
return array_keys($this->array); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Returns the contents of the store as array. |
|
190
|
|
|
* |
|
191
|
|
|
* @return array The keys and values in the store. |
|
192
|
|
|
*/ |
|
193
|
|
|
public function toArray() |
|
194
|
|
|
{ |
|
195
|
|
|
return array_map($this->unserialize, $this->array); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* {@inheritdoc} |
|
200
|
|
|
*/ |
|
201
|
12 |
|
public function sort($flags = SORT_REGULAR) |
|
202
|
|
|
{ |
|
203
|
12 |
|
ksort($this->array, $flags); |
|
204
|
12 |
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* {@inheritdoc} |
|
208
|
|
|
*/ |
|
209
|
2 |
|
public function count() |
|
210
|
|
|
{ |
|
211
|
2 |
|
return count($this->array); |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|
Let’s take a look at an example: