key = $key; $this->isHit = $hit; $this->currentDateTime = $currentDateTime ?? new DateTimeImmutable(); } /** * @inheritDoc */ public function getKey(): string { return $this->key; } /** * @inheritDoc */ public function get() { return $this->value; } /** * @inheritDoc */ public function isHit(): bool { return $this->isHit; } /** * @inheritDoc */ public function set($value): self { $this->value = $value; return $this; } /** * @inheritDoc */ public function expiresAt($expiration): self { if (null === $expiration) { $this->expiry = null; return $this; } if ($expiration instanceof DateTimeInterface) { $this->expiry = $expiration->getTimestamp(); return $this; } throw new InvalidArgumentException('Expiration date must implement DateTimeInterface or be null'); } /** * @inheritDoc * * @param DateInterval|null $time */ public function expiresAfter($time): self { if (null === $time) { $this->expiry = null; return $this; } if (is_int($time)) { $time = new DateInterval(sprintf('PT%dS', $time)); } if ($time instanceof DateInterval) { $this->expiry = $this->currentDateTime->add($time)->getTimestamp(); return $this; } throw new InvalidArgumentException( sprintf( 'Expiration date must implement DateTimeInterface or be null, "%s" given.', gettype($time) ) ); } }