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
|
|
|
/** |
15
|
|
|
* A key-value store backed by a PHP array with serialized entries. |
16
|
|
|
* |
17
|
|
|
* The contents of the store are lost when the store is released from memory. |
18
|
|
|
* |
19
|
|
|
* This store behaves more like persistent key-value stores than |
20
|
|
|
* {@link ArrayStore}. It is useful for testing. |
21
|
|
|
* |
22
|
|
|
* @since 1.0 |
23
|
|
|
* |
24
|
|
|
* @author Bernhard Schussek <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class SerializingArrayStore extends ArrayStore |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
63 |
|
public function set($key, $value) |
32
|
|
|
{ |
33
|
63 |
|
parent::set($key, serialize($value)); |
34
|
61 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
6 |
|
public function get($key, $default = null) |
40
|
|
|
{ |
41
|
6 |
|
if (!$this->exists($key)) { |
42
|
1 |
|
return $default; |
43
|
|
|
} |
44
|
|
|
|
45
|
3 |
|
return unserialize(parent::get($key)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
32 |
|
public function getOrFail($key) |
52
|
|
|
{ |
53
|
32 |
|
return unserialize(parent::getOrFail($key)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
10 |
|
public function getMultiple(array $keys, $default = null) |
60
|
|
|
{ |
61
|
10 |
|
$values = parent::getMultiple($keys, $default); |
62
|
|
|
|
63
|
8 |
|
foreach ($values as $key => $value) { |
64
|
8 |
|
if ($this->exists($key)) { |
65
|
8 |
|
$values[$key] = unserialize($value); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
8 |
|
return $values; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
public function toArray() |
76
|
|
|
{ |
77
|
|
|
$values = parent::toArray(); |
78
|
|
|
|
79
|
|
|
foreach ($values as $key => $value) { |
80
|
|
|
$values[$key] = unserialize($value); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $values; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|