1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Onoi\BlobStore; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @license GNU GPL v2+ |
9
|
|
|
* @since 1.0 |
10
|
|
|
* |
11
|
|
|
* @author mwjames |
12
|
|
|
*/ |
13
|
|
|
class Container { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $id; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
private $data = array(); |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var integer |
27
|
|
|
*/ |
28
|
|
|
private $expiry = 0; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @since 1.0 |
32
|
|
|
* |
33
|
|
|
* @param string $id |
34
|
|
|
* @param array $data |
35
|
|
|
*/ |
36
|
10 |
|
public function __construct( $id, array $data = array() ) { |
37
|
|
|
|
38
|
10 |
|
if ( !is_string( $id ) ) { |
39
|
1 |
|
throw new InvalidArgumentException( "Expected the id to be a string" ); |
40
|
|
|
} |
41
|
|
|
|
42
|
9 |
|
$this->id = $id; |
43
|
9 |
|
$this->data = $data; |
44
|
9 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @since 1.0 |
48
|
|
|
* |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
4 |
|
public function getId() { |
52
|
4 |
|
return $this->id; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @since 1.0 |
57
|
|
|
* |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
4 |
|
public function getData() { |
61
|
4 |
|
return $this->data; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @since 1.1 |
66
|
|
|
* |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
4 |
|
public function getExpiry() { |
70
|
4 |
|
return $this->expiry; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @since 1.1 |
75
|
|
|
* |
76
|
|
|
* @return integer $expiry |
77
|
|
|
*/ |
78
|
4 |
|
public function setExpiryInSeconds( $expiry ) { |
79
|
4 |
|
$this->expiry = (int)$expiry; |
80
|
4 |
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @since 1.1 |
84
|
|
|
* |
85
|
|
|
* @return boolean |
86
|
|
|
*/ |
87
|
1 |
|
public function isEmpty() { |
88
|
1 |
|
return $this->data === array(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @since 1.0 |
93
|
|
|
* |
94
|
|
|
* @return boolean |
95
|
|
|
*/ |
96
|
3 |
|
public function has( $key ) { |
97
|
3 |
|
return isset( $this->data[$key] ) || array_key_exists( $key, $this->data ); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @since 1.0 |
102
|
|
|
* |
103
|
|
|
* @param string $key |
104
|
|
|
* |
105
|
|
|
* @return mixed |
106
|
|
|
*/ |
107
|
2 |
|
public function get( $key ) { |
108
|
|
|
|
109
|
2 |
|
if ( $this->has( $key ) ) { |
110
|
2 |
|
return $this->data[$key]; |
111
|
|
|
} |
112
|
|
|
|
113
|
1 |
|
return false; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @since 1.0 |
118
|
|
|
* |
119
|
|
|
* @param string $key |
120
|
|
|
* @param mixed $value |
121
|
|
|
*/ |
122
|
4 |
|
public function set( $key, $value ) { |
123
|
4 |
|
$this->data[$key] = $value; |
124
|
4 |
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Extend/append/merge the data with an existing storage item for the same |
128
|
|
|
* key |
129
|
|
|
* |
130
|
|
|
* @since 1.0 |
131
|
|
|
* |
132
|
|
|
* @param string $key |
133
|
|
|
* @param mixed $value |
134
|
|
|
*/ |
135
|
2 |
|
public function append( $key, $value ) { |
136
|
|
|
|
137
|
2 |
|
if ( !$this->has( $key ) ) { |
138
|
1 |
|
$this->data[$key] = array(); |
139
|
1 |
|
} |
140
|
|
|
|
141
|
2 |
|
if ( !is_array( $value ) ) { |
142
|
2 |
|
$value = array( $value ); |
143
|
2 |
|
} |
144
|
|
|
|
145
|
2 |
|
$this->data[$key] = array_merge( |
146
|
2 |
|
(array)$this->data[$key], |
147
|
|
|
$value |
148
|
2 |
|
); |
149
|
2 |
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @since 1.2 |
153
|
|
|
* |
154
|
|
|
* @param integer|string $hash |
155
|
|
|
*/ |
156
|
1 |
|
public function addToLinkedList( $hash ) { |
157
|
|
|
|
158
|
1 |
|
if ( !isset( $this->data['@linkedList'] ) ) { |
159
|
1 |
|
$this->data['@linkedList'] = array(); |
160
|
1 |
|
} |
161
|
|
|
|
162
|
1 |
|
$this->data['@linkedList'][$hash] = true; |
163
|
1 |
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @since 1.2 |
167
|
|
|
* |
168
|
|
|
* @param array |
169
|
|
|
*/ |
170
|
2 |
|
public function getLinkedList() { |
171
|
|
|
|
172
|
2 |
|
if ( !isset( $this->data['@linkedList'] ) ) { |
173
|
1 |
|
return array(); |
174
|
|
|
} |
175
|
|
|
|
176
|
1 |
|
return array_keys( $this->data['@linkedList'] ); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @since 1.0 |
181
|
|
|
* |
182
|
|
|
* @param string $key |
183
|
|
|
*/ |
184
|
1 |
|
public function delete( $key ) { |
185
|
1 |
|
unset( $this->data[$key] ); |
186
|
1 |
|
} |
187
|
|
|
|
188
|
|
|
} |
189
|
|
|
|