bavix /
content
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Bavix\Context; |
||
| 4 | |||
| 5 | class Cookies extends Container |
||
| 6 | { |
||
| 7 | |||
| 8 | /** |
||
| 9 | * @var Configure |
||
| 10 | */ |
||
| 11 | protected $configure; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Cookie constructor. |
||
| 15 | * |
||
| 16 | * @param null|string $password |
||
| 17 | * @param Configure $configure |
||
| 18 | */ |
||
| 19 | 8 | public function __construct(?string $password = null, Configure $configure = null) |
|
| 20 | { |
||
| 21 | 8 | parent::__construct($password); |
|
| 22 | 8 | $this->store = filter_input_array(INPUT_COOKIE, FILTER_UNSAFE_RAW) ?: []; |
|
| 23 | |||
| 24 | 8 | $this->configure = $this->load($configure); |
|
| 25 | 8 | } |
|
| 26 | |||
| 27 | /** |
||
| 28 | * @return null|string |
||
| 29 | */ |
||
| 30 | 1 | public function sessionId(): ?string |
|
| 31 | { |
||
| 32 | 1 | return $this->store['PHPSESSID'] ?? null; |
|
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @return Configure |
||
| 37 | */ |
||
| 38 | 3 | public function configure(): Configure |
|
| 39 | { |
||
| 40 | 3 | return $this->configure; |
|
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @param Configure $configure |
||
| 45 | * |
||
| 46 | * @return Configure |
||
| 47 | */ |
||
| 48 | 8 | protected function load(?Configure $configure) : Configure |
|
| 49 | { |
||
| 50 | 8 | if (!$configure) |
|
| 51 | { |
||
| 52 | 8 | $configure = new Configure(); |
|
| 53 | } |
||
| 54 | |||
| 55 | 8 | return $configure; |
|
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @param string $name |
||
| 60 | * @param mixed $data |
||
| 61 | * @param Configure|null $configure |
||
| 62 | */ |
||
| 63 | 8 | public function set(string $name, $data, Configure $configure = null): void |
|
| 64 | { |
||
| 65 | 8 | parent::set($name, $data); |
|
| 66 | 8 | $configure = $configure ?: $this->configure; |
|
| 67 | 8 | \setcookie($name, $this->store[$name], ...$configure->asArray()); |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 68 | 8 | } |
|
| 69 | |||
| 70 | /** |
||
| 71 | * @param string $name |
||
| 72 | */ |
||
| 73 | 3 | public function remove(string $name): void |
|
| 74 | { |
||
| 75 | 3 | $this->set($name, '', $this->configure()->modify([ |
|
| 76 | 3 | 'expire' => 0 |
|
| 77 | ])); |
||
| 78 | |||
| 79 | 3 | parent::remove($name); |
|
| 80 | 3 | } |
|
| 81 | |||
| 82 | } |
||
| 83 |