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
|
|
|
protected $key; |
35
|
|
|
|
36
|
|
|
/** @var mixed */ |
37
|
|
|
protected $value; |
38
|
|
|
|
39
|
|
|
/** @var bool */ |
40
|
|
|
protected $isHit = false; |
41
|
|
|
|
42
|
|
|
/** @var float|int|null */ |
43
|
|
|
protected $expiry; |
44
|
|
|
|
45
|
|
|
/** @var int */ |
46
|
|
|
protected $defaultLifetime; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
public function getKey(): string |
52
|
|
|
{ |
53
|
|
|
return $this->key; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
|
|
public function get() |
60
|
|
|
{ |
61
|
|
|
return $this->value; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
|
|
public function isHit(): bool |
68
|
|
|
{ |
69
|
|
|
return $this->isHit; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
* |
75
|
|
|
* @return $this |
76
|
|
|
*/ |
77
|
|
|
public function set($value): self |
78
|
|
|
{ |
79
|
|
|
$this->value = $value; |
80
|
|
|
|
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
* |
87
|
|
|
* @return $this |
88
|
|
|
*/ |
89
|
|
|
public function expiresAt($expiration): self |
90
|
|
|
{ |
91
|
|
|
if (null === $expiration) { |
92
|
|
|
$this->expiry = $this->defaultLifetime > 0 ? \microtime(true) + $this->defaultLifetime : null; |
93
|
|
|
} elseif ($expiration instanceof DateTimeInterface) { |
94
|
|
|
$this->expiry = (float) $expiration->format('U.u'); |
95
|
|
|
} else { |
96
|
|
|
throw new InvalidArgumentException('Expiration date must implement DateTimeInterface or be null.'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
* |
105
|
|
|
* @return $this |
106
|
|
|
*/ |
107
|
|
|
public function expiresAfter($time): self |
108
|
|
|
{ |
109
|
|
|
if (null === $time) { |
110
|
|
|
$this->expiry = $this->defaultLifetime > 0 ? \microtime(true) + $this->defaultLifetime : null; |
111
|
|
|
} elseif ($time instanceof DateInterval) { |
112
|
|
|
$this->expiry = \microtime(true) + (int) DateTime::createFromFormat('U', '0')->add($time)->format('U.u'); |
113
|
|
|
} elseif (\is_int($time)) { |
114
|
|
|
$this->expiry = $time + \microtime(true); |
115
|
|
|
} else { |
116
|
|
|
throw new InvalidArgumentException('Expiration date must be an integer, a DateInterval or null.'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Validates a cache key according to PSR-6 and PSR-16. |
124
|
|
|
* |
125
|
|
|
* @param string $key The key to validate |
126
|
|
|
* |
127
|
|
|
* @return string |
128
|
|
|
* @throws InvalidArgumentException When $key is not valid |
129
|
|
|
*/ |
130
|
|
|
public static function validateKey($key): string |
131
|
|
|
{ |
132
|
|
|
if (!\is_string($key)) { |
|
|
|
|
133
|
|
|
throw new InvalidArgumentException('Cache key must be string.'); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
if ('' === $key) { |
137
|
|
|
throw new InvalidArgumentException('Cache key length must be greater than zero.'); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
if (false !== \strpbrk($key, self::RESERVED_CHARACTERS)) { |
141
|
|
|
throw new InvalidArgumentException( |
142
|
|
|
\sprintf('Cache key "%s" contains reserved characters "%s".', $key, self::RESERVED_CHARACTERS) |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $key; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|