for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
/*
* This file is part of the Egils\Component\Cache package.
*
* (c) Egidijus Lukauskas <[email protected]>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Egils\Component\Cache;
use Psr\Cache\CacheItemInterface;
use DateTime;
class CacheItem implements CacheItemInterface
{
/** @var mixed */
private $value;
/** @var string */
private $key;
/** @var DateTime */
private $expiration;
/** @var boolean */
private $hit;
/**
* @param string $key
* @param DateTime|integer|null $ttl
* @param boolean $hit
* @throws InvalidArgumentException
public function __construct($key, $ttl = null, $hit = false)
$this->key = $key;
$this->setExpiration($ttl);
$this->setHit($hit);
}
* @inheritdoc
public function getKey()
return $this->key;
public function get()
return $this->value;
public function set($value)
$this->value = $value;
return $this;
public function isHit()
return $this->hit;
public function exists()
return $this->isHit();
public function setExpiration($ttl = null)
if (true === is_numeric($ttl)) {
$this->expiration = new DateTime('now +' . $ttl . ' seconds');
} elseif ($ttl instanceof DateTime) {
$this->expiration = $ttl;
} else {
$this->expiration = new DateTime('now +10 years');
public function getExpiration()
return $this->expiration;
* @return CacheItem
* @throws InvalidArgumentException not a boolean value given
public function setHit($hit)
if (false === is_bool($hit)) {
throw InvalidArgumentException::typeMismatch($hit, 'Boolean');
$this->hit = $hit;