composer package updates
[openemr.git] / vendor / zendframework / zend-cache / src / Psr / CacheItemPool / CacheItem.php
blobe06eaf7640c2b5b73fe6382eed1f12484c3b3a1a
1 <?php
2 /**
3 * @see https://github.com/zendframework/zend-cache for the canonical source repository
4 * @copyright Copyright (c) 2018 Zend Technologies USA Inc. (http://www.zend.com)
5 * @license https://github.com/zendframework/zend-cache/blob/master/LICENSE.md New BSD License
6 */
8 namespace Zend\Cache\Psr\CacheItemPool;
10 use DateInterval;
11 use DateTimeImmutable;
12 use DateTimeInterface;
13 use DateTimeZone;
14 use Psr\Cache\CacheItemInterface;
16 final class CacheItem implements CacheItemInterface
18 /**
19 * Cache key
20 * @var string
22 private $key;
24 /**
25 * Cache value
26 * @var mixed|null
28 private $value;
30 /**
31 * True if the cache item lookup resulted in a cache hit or if they item is deferred or successfully saved
32 * @var bool
34 private $isHit = false;
36 /**
37 * Timestamp item will expire at if expiresAt() called, null otherwise
38 * @var int|null
40 private $expiration = null;
42 /**
43 * Seconds after item is stored it will expire at if expiresAfter() called, null otherwise
44 * @var int|null
46 private $ttl = null;
48 /**
49 * @var DateTimeZone
51 private $tz;
53 /**
54 * Constructor.
56 * @param string $key
57 * @param mixed $value
58 * @param bool $isHit
60 public function __construct($key, $value, $isHit)
62 $this->key = $key;
63 $this->value = $isHit ? $value : null;
64 $this->isHit = $isHit;
65 $this->utc = new DateTimeZone('UTC');
68 /**
69 * {@inheritdoc}
71 public function getKey()
73 return $this->key;
76 /**
77 * {@inheritdoc}
79 public function get()
81 return $this->value;
84 /**
85 * {@inheritdoc}
87 public function isHit()
89 if (! $this->isHit) {
90 return false;
92 $ttl = $this->getTtl();
93 return $ttl === null || $ttl > 0;
96 /**
97 * Sets isHit value
99 * This function is called by CacheItemPoolDecorator::saveDeferred() and is not intended for use by other calling
100 * code.
102 * @param boolean $isHit
103 * @return $this
105 public function setIsHit($isHit)
107 $this->isHit = $isHit;
109 return $this;
113 * {@inheritdoc}
115 public function set($value)
117 $this->value = $value;
119 return $this;
123 * {@inheritdoc}
125 public function expiresAt($expiration)
127 if (! ($expiration === null || $expiration instanceof DateTimeInterface)) {
128 throw new InvalidArgumentException('$expiration must be null or an instance of DateTimeInterface');
131 $this->expiration = $expiration instanceof DateTimeInterface ? $expiration->getTimestamp() : null;
132 $this->ttl = null;
134 return $this;
138 * {@inheritdoc}
140 public function expiresAfter($time)
142 if ($time instanceof DateInterval) {
143 $now = new DateTimeImmutable('now', $this->utc);
144 $end = $now->add($time);
145 $this->ttl = $end->getTimestamp() - $now->getTimestamp();
146 } elseif (is_int($time) || $time === null) {
147 $this->ttl = $time;
148 } else {
149 throw new InvalidArgumentException(sprintf('Invalid $time "%s"', gettype($time)));
152 $this->expiration = null;
154 return $this;
158 * Returns number of seconds until item expires
160 * If NULL, the pool should use the default TTL for the storage adapter. If <= 0, the item has expired.
162 * @return int|null
164 public function getTtl()
166 $ttl = $this->ttl;
167 if ($this->expiration !== null) {
168 $ttl = $this->expiration - time();
170 return $ttl;