1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace cse\helpers; |
||
6 | |||
7 | /** |
||
8 | * Class Cookie |
||
9 | * |
||
10 | * @package cse\helpers |
||
11 | */ |
||
12 | class Cookie |
||
13 | { |
||
14 | const COOKIE_PATH = '/'; |
||
15 | const COOKIE_TIMEOUT = 432000; |
||
16 | |||
17 | /** |
||
18 | * Set cookie by name |
||
19 | * |
||
20 | * @param string $name |
||
21 | * @param $value |
||
22 | * @param string $path |
||
23 | * @param int $timeout |
||
24 | * |
||
25 | * @return bool |
||
26 | */ |
||
27 | public static function set(string $name, $value, string $path = self::COOKIE_PATH, int $timeout = self::COOKIE_TIMEOUT): bool |
||
28 | { |
||
29 | $_COOKIE[$name] = $value; |
||
30 | |||
31 | return setcookie($name, (string) $value, time() + $timeout, $path); |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * Check cookie by name |
||
36 | * |
||
37 | * @param string $name |
||
38 | * |
||
39 | * @return bool |
||
40 | */ |
||
41 | public static function has(string $name): bool |
||
42 | { |
||
43 | return isset($_COOKIE[$name]); |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * Get cookie by name |
||
48 | * |
||
49 | * @param string $name |
||
50 | * @param null $default |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
51 | * |
||
52 | * @return null|mixed |
||
53 | */ |
||
54 | public static function get(string $name, $default = null) |
||
55 | { |
||
56 | return self::has($name) ? $_COOKIE[$name] : $default; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Remove cookie by name |
||
61 | * |
||
62 | * @param string $name |
||
63 | */ |
||
64 | public static function remove(string $name): void |
||
65 | { |
||
66 | if (self::has($name)) { |
||
67 | self::set($name, null, self::COOKIE_PATH, -time()); |
||
68 | unset($_COOKIE[$name]); |
||
69 | } |
||
70 | } |
||
71 | } |