|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* This file is part of phpFastCache. |
|
5
|
|
|
* |
|
6
|
|
|
* @license MIT License (MIT) |
|
7
|
|
|
* |
|
8
|
|
|
* For full copyright and license information, please see the docs/CREDITS.txt file. |
|
9
|
|
|
* |
|
10
|
|
|
* @author Khoa Bui (khoaofgod) <[email protected]> http://www.phpfastcache.com |
|
11
|
|
|
* @author Georges.L (Geolim4) <[email protected]> |
|
12
|
|
|
* |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace phpFastCache\Core\Item; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class ItemExtendedTrait |
|
19
|
|
|
* @package phpFastCache\Core\Item |
|
20
|
|
|
*/ |
|
21
|
|
|
trait ItemExtendedTrait |
|
22
|
|
|
{ |
|
23
|
|
|
/******************** |
|
24
|
|
|
* |
|
25
|
|
|
* PSR-6 Extended Methods |
|
26
|
|
|
* |
|
27
|
|
|
*******************/ |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @return mixed |
|
32
|
|
|
*/ |
|
33
|
|
|
public function getUncommittedData() |
|
34
|
|
|
{ |
|
35
|
|
|
return $this->data; |
|
|
|
|
|
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @return \DateTimeInterface |
|
40
|
|
|
*/ |
|
41
|
|
|
public function getExpirationDate() |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->expirationDate; |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Alias of expireAt() with forced $expiration param |
|
48
|
|
|
* |
|
49
|
|
|
* @param \DateTimeInterface $expiration |
|
50
|
|
|
* The point in time after which the item MUST be considered expired. |
|
51
|
|
|
* If null is passed explicitly, a default value MAY be used. If none is set, |
|
52
|
|
|
* the value should be stored permanently or for as long as the |
|
53
|
|
|
* implementation allows. |
|
54
|
|
|
* |
|
55
|
|
|
* @return static |
|
56
|
|
|
* The called object. |
|
57
|
|
|
*/ |
|
58
|
|
|
public function setExpirationDate(\DateTimeInterface $expiration) |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->expiresAt($expiration); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return \DateTimeInterface |
|
66
|
|
|
* @throws \LogicException |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getCreationDate() |
|
69
|
|
|
{ |
|
70
|
|
|
if($this->driver->getConfig()['itemDetailedDate']){ |
|
|
|
|
|
|
71
|
|
|
return $this->creationDate; |
|
|
|
|
|
|
72
|
|
|
}else{ |
|
73
|
|
|
throw new \LogicException('Cannot access to the creation date when the "itemDetailedDate" configuration is disabled.'); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param \DateTimeInterface $date |
|
79
|
|
|
* @return $this |
|
80
|
|
|
* @throws \LogicException |
|
81
|
|
|
*/ |
|
82
|
|
View Code Duplication |
public function setCreationDate(\DateTimeInterface $date) |
|
|
|
|
|
|
83
|
|
|
{ |
|
84
|
|
|
if($this->driver->getConfig()['itemDetailedDate']){ |
|
85
|
|
|
$this->creationDate = $date; |
|
86
|
|
|
return $this; |
|
87
|
|
|
}else{ |
|
88
|
|
|
throw new \LogicException('Cannot access to the creation date when the "itemDetailedDate" configuration is disabled.'); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return \DateTimeInterface |
|
94
|
|
|
* @throws \LogicException |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getModificationDate() |
|
97
|
|
|
{ |
|
98
|
|
|
if($this->driver->getConfig()['itemDetailedDate']){ |
|
99
|
|
|
return $this->modificationDate; |
|
|
|
|
|
|
100
|
|
|
}else{ |
|
101
|
|
|
throw new \LogicException('Cannot access to the modification date when the "itemDetailedDate" configuration is disabled.'); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param \DateTimeInterface $date |
|
107
|
|
|
* @return $this |
|
108
|
|
|
* @throws \LogicException |
|
109
|
|
|
*/ |
|
110
|
|
View Code Duplication |
public function setModificationDate(\DateTimeInterface $date) |
|
|
|
|
|
|
111
|
|
|
{ |
|
112
|
|
|
if($this->driver->getConfig()['itemDetailedDate']){ |
|
113
|
|
|
$this->modificationDate = $date; |
|
114
|
|
|
return $this; |
|
115
|
|
|
}else{ |
|
116
|
|
|
throw new \LogicException('Cannot access to the creation date when the "itemDetailedDate" configuration is disabled.'); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @return int |
|
122
|
|
|
*/ |
|
123
|
|
|
public function getTtl() |
|
124
|
|
|
{ |
|
125
|
|
|
$ttl = $this->expirationDate->getTimestamp() - time(); |
|
126
|
|
|
if ($ttl > 2592000) { |
|
127
|
|
|
$ttl = time() + $ttl; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return $ttl; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @return bool |
|
135
|
|
|
*/ |
|
136
|
|
|
public function isExpired() |
|
137
|
|
|
{ |
|
138
|
|
|
return $this->expirationDate->getTimestamp() < (new \DateTime())->getTimestamp(); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @param int $step |
|
143
|
|
|
* @return $this |
|
144
|
|
|
* @throws \InvalidArgumentException |
|
145
|
|
|
*/ |
|
146
|
|
View Code Duplication |
public function increment($step = 1) |
|
|
|
|
|
|
147
|
|
|
{ |
|
148
|
|
|
if (is_int($step)) { |
|
149
|
|
|
$this->fetched = true; |
|
|
|
|
|
|
150
|
|
|
$this->data += $step; |
|
151
|
|
|
} else { |
|
152
|
|
|
throw new \InvalidArgumentException('$step must be numeric.'); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
return $this; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @param int $step |
|
160
|
|
|
* @return $this |
|
161
|
|
|
* @throws \InvalidArgumentException |
|
162
|
|
|
*/ |
|
163
|
|
View Code Duplication |
public function decrement($step = 1) |
|
|
|
|
|
|
164
|
|
|
{ |
|
165
|
|
|
if (is_int($step)) { |
|
166
|
|
|
$this->fetched = true; |
|
167
|
|
|
$this->data -= $step; |
|
168
|
|
|
} else { |
|
169
|
|
|
throw new \InvalidArgumentException('$step must be numeric.'); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
return $this; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* @param array|string $data |
|
177
|
|
|
* @return $this |
|
178
|
|
|
* @throws \InvalidArgumentException |
|
179
|
|
|
*/ |
|
180
|
|
View Code Duplication |
public function append($data) |
|
|
|
|
|
|
181
|
|
|
{ |
|
182
|
|
|
if (is_array($this->data)) { |
|
183
|
|
|
array_push($this->data, $data); |
|
184
|
|
|
} else if (is_string($data)) { |
|
185
|
|
|
$this->data .= (string) $data; |
|
186
|
|
|
} else { |
|
187
|
|
|
throw new \InvalidArgumentException('$data must be either array nor string.'); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
return $this; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @param array|string $data |
|
196
|
|
|
* @return $this |
|
197
|
|
|
* @throws \InvalidArgumentException |
|
198
|
|
|
*/ |
|
199
|
|
View Code Duplication |
public function prepend($data) |
|
|
|
|
|
|
200
|
|
|
{ |
|
201
|
|
|
if (is_array($this->data)) { |
|
202
|
|
|
array_unshift($this->data, $data); |
|
203
|
|
|
} else if (is_string($data)) { |
|
204
|
|
|
$this->data = (string) $data . $this->data; |
|
205
|
|
|
} else { |
|
206
|
|
|
throw new \InvalidArgumentException('$data must be either array nor string.'); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
return $this; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* @param $tagName |
|
214
|
|
|
* @return $this |
|
215
|
|
|
* @throws \InvalidArgumentException |
|
216
|
|
|
*/ |
|
217
|
|
|
public function addTag($tagName) |
|
218
|
|
|
{ |
|
219
|
|
|
if (is_string($tagName)) { |
|
220
|
|
|
$this->tags = array_unique(array_merge($this->tags, [$tagName])); |
|
|
|
|
|
|
221
|
|
|
|
|
222
|
|
|
return $this; |
|
223
|
|
|
} else { |
|
224
|
|
|
throw new \InvalidArgumentException('$tagName must be a string'); |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* @param array $tagNames |
|
230
|
|
|
* @return $this |
|
231
|
|
|
*/ |
|
232
|
|
|
public function addTags(array $tagNames) |
|
233
|
|
|
{ |
|
234
|
|
|
foreach ($tagNames as $tagName) { |
|
235
|
|
|
$this->addTag($tagName); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
return $this; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* @param array $tags |
|
243
|
|
|
* @return $this |
|
244
|
|
|
* @throws \InvalidArgumentException |
|
245
|
|
|
*/ |
|
246
|
|
|
public function setTags(array $tags) |
|
247
|
|
|
{ |
|
248
|
|
|
if (count($tags)) { |
|
249
|
|
|
if (array_filter($tags, 'is_string')) { |
|
250
|
|
|
$this->tags = $tags; |
|
251
|
|
|
} else { |
|
252
|
|
|
throw new \InvalidArgumentException('$tagName must be an array of string'); |
|
253
|
|
|
} |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
return $this; |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* @return array |
|
261
|
|
|
*/ |
|
262
|
|
|
public function getTags() |
|
263
|
|
|
{ |
|
264
|
|
|
return $this->tags; |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* @return string |
|
269
|
|
|
*/ |
|
270
|
|
|
public function getTagsAsString($separator = ', ') |
|
271
|
|
|
{ |
|
272
|
|
|
return implode($separator, $this->tags); |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* @param $tagName |
|
277
|
|
|
* @return $this |
|
278
|
|
|
*/ |
|
279
|
|
|
public function removeTag($tagName) |
|
280
|
|
|
{ |
|
281
|
|
|
if (($key = array_search($tagName, $this->tags)) !== false) { |
|
282
|
|
|
unset($this->tags[ $key ]); |
|
283
|
|
|
$this->removedTags[] = $tagName; |
|
|
|
|
|
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
return $this; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
/** |
|
290
|
|
|
* @param array $tagNames |
|
291
|
|
|
* @return $this |
|
292
|
|
|
*/ |
|
293
|
|
|
public function removeTags(array $tagNames) |
|
294
|
|
|
{ |
|
295
|
|
|
foreach ($tagNames as $tagName) { |
|
296
|
|
|
$this->removeTag($tagName); |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
return $this; |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
/** |
|
303
|
|
|
* @return array |
|
304
|
|
|
*/ |
|
305
|
|
|
public function getRemovedTags() |
|
306
|
|
|
{ |
|
307
|
|
|
return array_diff($this->removedTags, $this->tags); |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* Return the data as a well-formatted string. |
|
312
|
|
|
* Any scalar value will be casted to an array |
|
313
|
|
|
* @param int $option json_encode() options |
|
314
|
|
|
* @param int $depth json_encode() depth |
|
315
|
|
|
* @return string |
|
316
|
|
|
*/ |
|
317
|
|
|
public function getDataAsJsonString($option = 0, $depth = 512) |
|
318
|
|
|
{ |
|
319
|
|
|
$data = $this->get(); |
|
320
|
|
|
|
|
321
|
|
|
if (is_object($data) || is_array($data)) { |
|
322
|
|
|
$data = json_encode($data, $option, $depth); |
|
323
|
|
|
} else { |
|
324
|
|
|
$data = json_encode([$data], $option, $depth); |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
return json_encode($data, $option, $depth); |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
|
|
/** |
|
331
|
|
|
* Implements \JsonSerializable interface |
|
332
|
|
|
* @return mixed |
|
333
|
|
|
*/ |
|
334
|
|
|
public function jsonSerialize() |
|
335
|
|
|
{ |
|
336
|
|
|
return $this->get(); |
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
|
|
/** |
|
340
|
|
|
* Prevent recursions for Debug (php 5.6+) |
|
341
|
|
|
* @return array |
|
342
|
|
|
*/ |
|
343
|
|
|
final public function __debugInfo() |
|
344
|
|
|
{ |
|
345
|
|
|
$info = get_object_vars($this); |
|
346
|
|
|
$info[ 'driver' ] = 'object(' . get_class($info[ 'driver' ]) . ')'; |
|
347
|
|
|
|
|
348
|
|
|
return (array) $info; |
|
349
|
|
|
} |
|
350
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: