Skip to content

Commit

Permalink
Context cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Sep 9, 2020
1 parent c6f0049 commit e6e9dc5
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/Cookie/ContextCookie.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
namespace Packaged\Context\Cookie;

use Packaged\Context\ContextAware;
use Packaged\Context\ContextAwareTrait;
use Packaged\Context\WithContext;
use Packaged\Context\WithContextTrait;

abstract class ContextCookie implements ContextAware, WithContext
{
use ContextAwareTrait;
use WithContextTrait;

/**
* @var string|null
*/
private $_rawValue;

abstract public function name(): string;

abstract public function ttl(): int;

public function exists(bool $checkQueued = true)
{
return $this->getContext()->cookies()->has($this->name(), $checkQueued);
}

public function read(bool $checkQueued = true)
{
return $this->_setRawValue($this->getContext()->cookies()->read($this->name(), $checkQueued));
}

protected function _getRawValue(): ?string
{
return $this->_rawValue;
}

protected function _setRawValue(string $value)
{
$this->_rawValue = $value;
return $this;
}

public function store()
{
$this->getContext()->cookies()->store($this->name(), $this->_getRawValue(), $this->ttl());
return $this;
}
}

0 comments on commit e6e9dc5

Please sign in to comment.