1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Cubiche package. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) Cubiche |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace Cubiche\Core\Storage; |
11
|
|
|
|
12
|
|
|
use Cubiche\Core\Collections\ArrayCollection\ArrayHashMap; |
13
|
|
|
use Cubiche\Core\Serializer\SerializerInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* InMemoryStorage class. |
17
|
|
|
* |
18
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class InMemoryStorage extends AbstractStorage implements StorageInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var HashMap |
24
|
|
|
*/ |
25
|
|
|
protected $store; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Creates a new store. |
29
|
|
|
* |
30
|
|
|
* @param SerializerInterface $serializer |
31
|
|
|
* @param array $elements |
32
|
|
|
*/ |
33
|
|
|
public function __construct(SerializerInterface $serializer, array $elements = array()) |
34
|
|
|
{ |
35
|
|
|
parent::__construct($serializer); |
36
|
|
|
|
37
|
|
|
$this->store = new ArrayHashMap(); |
|
|
|
|
38
|
|
|
foreach ($elements as $key => $value) { |
39
|
|
|
$this->set($key, $value); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
|
public function set($key, $value) |
47
|
|
|
{ |
48
|
|
|
$this->validateKey($key); |
49
|
|
|
|
50
|
|
|
$this->store->set($key, $this->serializer->serialize($value)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
public function get($key, $default = null) |
57
|
|
|
{ |
58
|
|
|
$this->validateKey($key); |
59
|
|
|
if (!$this->store->containsKey($key)) { |
60
|
|
|
return $default; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $this->serializer->deserialize($this->store->get($key)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
public function has($key) |
70
|
|
|
{ |
71
|
|
|
$this->validateKey($key); |
72
|
|
|
|
73
|
|
|
return $this->store->containsKey($key); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
|
|
public function remove($key) |
80
|
|
|
{ |
81
|
|
|
$this->validateKey($key); |
82
|
|
|
|
83
|
|
|
$removed = $this->store->removeAt($key); |
84
|
|
|
if ($removed !== null) { |
85
|
|
|
return true; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
|
|
public function clear() |
95
|
|
|
{ |
96
|
|
|
$this->store->clear(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
|
|
*/ |
102
|
|
|
public function keys() |
103
|
|
|
{ |
104
|
|
|
return array_keys($this->store->toArray()); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..