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