Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
8 / 8
CRAP
100.00% covered (success)
100.00%
1 / 1
CacheItem
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
8 / 8
10
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getKey
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isHit
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 set
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 expiresAt
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 expiresAfter
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 getExpirationSeconds
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2declare(strict_types=1);
3/**
4 * Pop PHP Framework (https://www.popphp.org/)
5 *
6 * @link       https://github.com/popphp/popphp-framework
7 * @author     Nick Sagona, III <nick@popphp.org>
8 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
9 * @license    https://www.popphp.org/license     New BSD License
10 */
11
12/**
13 * @namespace
14 */
15namespace Pop\Cache\Psr6;
16
17/**
18 * PSR-6 cache item class
19 *
20 * @category   Pop
21 * @package    Pop\Cache
22 * @author     Nick Sagona, III <nick@popphp.org>
23 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
24 * @license    https://www.popphp.org/license     New BSD License
25 * @version    5.0.0
26 */
27class CacheItem implements \Psr\Cache\CacheItemInterface
28{
29
30    /**
31     * The cache item key
32     * @var string
33     */
34    protected string $key;
35
36    /**
37     * The cache item value
38     * @var mixed
39     */
40    protected mixed $value = null;
41
42    /**
43     * Whether the item was a cache hit
44     * @var bool
45     */
46    protected bool $isHit = false;
47
48    /**
49     * The resolved expiration, in seconds from now (null means "use the pool's default")
50     * @var ?int
51     */
52    protected ?int $expirationSeconds = null;
53
54    /**
55     * Constructor
56     *
57     * Instantiate the cache item object
58     *
59     * @param  string $key
60     * @param  mixed  $value
61     * @param  bool   $isHit
62     */
63    public function __construct(string $key, mixed $value = null, bool $isHit = false)
64    {
65        $this->key   = $key;
66        $this->value = $value;
67        $this->isHit = $isHit;
68    }
69
70    /**
71     * Get the cache item key
72     *
73     * @return string
74     */
75    public function getKey(): string
76    {
77        return $this->key;
78    }
79
80    /**
81     * Get the cache item value
82     *
83     * @return mixed
84     */
85    public function get(): mixed
86    {
87        return $this->value;
88    }
89
90    /**
91     * Determine if the cache item lookup was a hit
92     *
93     * @return bool
94     */
95    public function isHit(): bool
96    {
97        return $this->isHit;
98    }
99
100    /**
101     * Set the cache item value
102     *
103     * @param  mixed $value
104     * @return static
105     */
106    public function set(mixed $value): static
107    {
108        $this->value = $value;
109        return $this;
110    }
111
112    /**
113     * Set the expiration time for this cache item
114     *
115     * @param  ?\DateTimeInterface $expiration
116     * @return static
117     */
118    public function expiresAt(?\DateTimeInterface $expiration): static
119    {
120        $this->expirationSeconds = ($expiration !== null) ? max(0, $expiration->getTimestamp() - time()) : null;
121        return $this;
122    }
123
124    /**
125     * Set the expiration time for this cache item, relative to now
126     *
127     * @param  int|\DateInterval|null $time
128     * @return static
129     */
130    public function expiresAfter(int|\DateInterval|null $time): static
131    {
132        if ($time instanceof \DateInterval) {
133            $now = new \DateTimeImmutable();
134            $this->expirationSeconds = $now->add($time)->getTimestamp() - $now->getTimestamp();
135        } else {
136            $this->expirationSeconds = $time;
137        }
138
139        return $this;
140    }
141
142    /**
143     * Get the resolved expiration, in seconds from now (null means "use the pool's default")
144     *
145     * @return ?int
146     */
147    public function getExpirationSeconds(): ?int
148    {
149        return $this->expirationSeconds;
150    }
151
152}