1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of BiuradPHP opensource projects. |
7
|
|
|
* |
8
|
|
|
* PHP version 7.1 and above required |
9
|
|
|
* |
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
11
|
|
|
* @copyright 2019 Biurad Group (https://biurad.com/) |
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
13
|
|
|
* |
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
15
|
|
|
* file that was distributed with this source code. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace Biurad\Cache; |
19
|
|
|
|
20
|
|
|
use Biurad\Cache\Exceptions\InvalidArgumentException; |
21
|
|
|
use DateInterval; |
22
|
|
|
use DateTime; |
23
|
|
|
use DateTimeInterface; |
24
|
|
|
use Psr\Cache\CacheItemInterface; |
25
|
|
|
|
26
|
|
|
final class CacheItem implements CacheItemInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Reserved characters that cannot be used in a key or tag. |
30
|
|
|
*/ |
31
|
|
|
public const RESERVED_CHARACTERS = '{}()/\@:'; |
32
|
|
|
|
33
|
|
|
/** @var string */ |
34
|
|
|
private $key; |
35
|
|
|
|
36
|
|
|
/** @var mixed */ |
37
|
|
|
private $value; |
38
|
|
|
|
39
|
|
|
/** @var bool */ |
40
|
|
|
private $isHit = false; |
41
|
|
|
|
42
|
|
|
/** @var null|float|int */ |
43
|
|
|
private $expiry; |
44
|
|
|
|
45
|
|
|
/** @var int */ |
46
|
|
|
private $defaultLifetime; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
81 |
|
public function getKey(): string |
52
|
|
|
{ |
53
|
81 |
|
return $this->key; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
57 |
|
public function get() |
60
|
|
|
{ |
61
|
57 |
|
return $this->value; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
74 |
|
public function isHit(): bool |
68
|
|
|
{ |
69
|
74 |
|
return $this->isHit; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
93 |
|
public function set($value) |
76
|
|
|
{ |
77
|
93 |
|
$this->value = $value; |
78
|
|
|
|
79
|
93 |
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
7 |
|
public function expiresAt($expiration) |
86
|
|
|
{ |
87
|
7 |
|
if (null === $expiration) { |
88
|
2 |
|
return $this->setDefaultExpiration(); |
89
|
|
|
} |
90
|
|
|
|
91
|
6 |
|
if (!$expiration instanceof DateTimeInterface) { |
92
|
1 |
|
throw new InvalidArgumentException('Expiration date must implement DateTimeInterface or be null.'); |
93
|
|
|
} |
94
|
|
|
|
95
|
5 |
|
$this->expiry = (float) $expiration->format('U.u'); |
96
|
|
|
|
97
|
5 |
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
69 |
|
public function expiresAfter($time) |
104
|
|
|
{ |
105
|
69 |
|
if (null === $time) { |
106
|
43 |
|
return $this->setDefaultExpiration(); |
107
|
|
|
} |
108
|
|
|
|
109
|
28 |
|
if ($time instanceof DateInterval) { |
110
|
3 |
|
$interval = DateTime::createFromFormat('U', '0')->add($time); |
111
|
3 |
|
$this->expiry = \microtime(true) + (int) $interval->format('U.u'); |
112
|
28 |
|
} elseif (\is_int($time)) { |
113
|
8 |
|
$this->expiry = $time + \microtime(true); |
114
|
|
|
} else { |
115
|
20 |
|
throw new InvalidArgumentException('Expiration date must be an integer, a DateInterval or null.'); |
116
|
|
|
} |
117
|
|
|
|
118
|
8 |
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Validates a cache key according to PSR-6 and PSR-16. |
123
|
|
|
* |
124
|
|
|
* @param string $key The key to validate |
125
|
|
|
* |
126
|
|
|
* @throws InvalidArgumentException When $key is not valid |
127
|
|
|
* |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
263 |
|
public static function validateKey($key): string |
131
|
|
|
{ |
132
|
263 |
|
if (!\is_string($key)) { |
|
|
|
|
133
|
6 |
|
throw new InvalidArgumentException('Cache key must be string.'); |
134
|
|
|
} |
135
|
|
|
|
136
|
257 |
|
if ('' === $key) { |
137
|
8 |
|
throw new InvalidArgumentException('Cache key length must be greater than zero.'); |
138
|
|
|
} |
139
|
|
|
|
140
|
252 |
|
if (false !== \strpbrk($key, self::RESERVED_CHARACTERS)) { |
141
|
118 |
|
throw new InvalidArgumentException( |
142
|
118 |
|
\sprintf('Cache key "%s" contains reserved characters "%s".', $key, self::RESERVED_CHARACTERS) |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
|
146
|
174 |
|
return $key; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return static |
151
|
|
|
*/ |
152
|
45 |
|
private function setDefaultExpiration(): self |
153
|
|
|
{ |
154
|
45 |
|
$this->expiry = $this->defaultLifetime > 0 ? \microtime(true) + $this->defaultLifetime : null; |
155
|
|
|
|
156
|
45 |
|
return $this; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|