|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Doctrine\Common\Cache\Psr6; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Cache\CacheItemInterface; |
|
6
|
|
|
|
|
7
|
|
|
final class CacheItem implements CacheItemInterface |
|
8
|
|
|
{ |
|
9
|
|
|
/** @var string */ |
|
10
|
|
|
private $key; |
|
11
|
|
|
/** @var mixed */ |
|
12
|
|
|
private $value; |
|
13
|
|
|
/** @var bool */ |
|
14
|
|
|
private $hit; |
|
15
|
|
|
/** @var \DateTimeInterface|null */ |
|
|
|
|
|
|
16
|
|
|
private $expiration; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct(string $key, $data) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->key = $key; |
|
|
|
|
|
|
21
|
|
|
$this->value = false === $data ? null : $data; |
|
|
|
|
|
|
22
|
|
|
$this->hit = false !== $data; |
|
|
|
|
|
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @inheritDoc |
|
27
|
|
|
*/ |
|
28
|
|
|
public function getKey(): string |
|
|
|
|
|
|
29
|
|
|
{ |
|
30
|
|
|
return $this->key; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @inheritDoc |
|
35
|
|
|
*/ |
|
36
|
|
|
public function get() |
|
37
|
|
|
{ |
|
38
|
|
|
return $this->value; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @inheritDoc |
|
43
|
|
|
*/ |
|
44
|
|
|
public function isHit(): bool |
|
|
|
|
|
|
45
|
|
|
{ |
|
46
|
|
|
return $this->hit; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @inheritDoc |
|
51
|
|
|
*/ |
|
52
|
|
|
public function set($value): self |
|
|
|
|
|
|
53
|
|
|
{ |
|
54
|
|
|
$this->value = $value; |
|
55
|
|
|
|
|
56
|
|
|
return $this; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @inheritDoc |
|
61
|
|
|
*/ |
|
62
|
|
|
public function expiresAt($expiration): self |
|
|
|
|
|
|
63
|
|
|
{ |
|
64
|
|
|
if (null !== $expiration && !$expiration instanceof \DateTimeInterface) { |
|
|
|
|
|
|
65
|
|
|
throw new \TypeError(sprintf( |
|
|
|
|
|
|
66
|
|
|
'Expected $expiration to be an instance of DateTimeInterface or null, got %s', |
|
67
|
|
|
\is_object($expiration) ? \get_class($expiration) : \gettype($expiration) |
|
|
|
|
|
|
68
|
|
|
)); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$this->expiration = $expiration; |
|
72
|
|
|
|
|
73
|
|
|
return $this; |
|
74
|
|
|
} |
|
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @inheritDoc |
|
77
|
|
|
*/ |
|
78
|
|
|
public function expiresAfter($time): self |
|
|
|
|
|
|
79
|
|
|
{ |
|
80
|
|
|
if (null === $time) { |
|
|
|
|
|
|
81
|
|
|
$this->expiration = null; |
|
82
|
|
|
} elseif (is_numeric($time)) { |
|
|
|
|
|
|
83
|
|
|
$this->expiration = new \DateTimeImmutable(sprintf('now +%d seconds', $time)); |
|
|
|
|
|
|
84
|
|
|
} elseif ($time instanceof \DateInterval) { |
|
|
|
|
|
|
85
|
|
|
$this->expiration = (new \DateTimeImmutable())->add($time); |
|
|
|
|
|
|
86
|
|
|
} else { |
|
87
|
|
|
throw new \TypeError(sprintf( |
|
|
|
|
|
|
88
|
|
|
'Expected $time to be either an integer, an instance of DateInterval or null, got %s', |
|
89
|
|
|
\is_object($time) ? \get_class($time) : \gettype($time) |
|
|
|
|
|
|
90
|
|
|
)); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function getExpiration(): ?\DateTimeInterface |
|
|
|
|
|
|
97
|
|
|
{ |
|
98
|
|
|
return $this->expiration; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|