CookieStorage::get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
namespace ElePHPant\Cookie\Storage;
4
5
6
/**
7
 * Class CookieStorage
8
 *
9
 * Please report bugs on https://github.com/wilderamorim/cookie/issues
10
 *
11
 * @author Wilder Amorim <https://github.com/wilderamorim>
12
 * @link https://www.linkedin.com/in/wilderamorim/
13
 */
14
class CookieStorage implements CookieStorageInterface
15
{
16
    /** @var array  The cookie options array. */
17
    private array $cookieOptions;
18
19
    /** @var string The expiration time unit. */
20
    private string $expiration;
21
22
    /** Default options for the cookie. */
23
    private const DEFAULT_VALUES = [
24
        'path' => '/',
25
        'domain' => '',
26
        'secure' => false,
27
        'httponly' => false,
28
    ];
29
30
    /**
31
     * @param array $options The options array for the cookie storage.
32
     */
33
    public function __construct(array $options)
34
    {
35
        $this->expiration = $options['expiration'];
36
        $allowedOptions = [...array_keys(self::DEFAULT_VALUES), 'samesite'];
37
38
        $this->cookieOptions = array_intersect_key($options, array_flip($allowedOptions));
39
        $this->cookieOptions = array_replace(self::DEFAULT_VALUES, $this->cookieOptions);
40
    }
41
42
    /**
43
     * Set a cookie.
44
     *
45
     * @param string      $name   The name of the cookie.
46
     * @param string|null $value  The value of the cookie.
47
     * @param int         $expire The expiration time of the cookie as a Unix timestamp.
48
     * @return bool               True if the cookie is set successfully, false otherwise.
49
     */
50
    public function set(string $name, ?string $value, int $expire): bool
51
    {
52
        $this->cookieOptions['expires'] = strtotime("+$expire $this->expiration");
53
        return setcookie($name, $value, $this->cookieOptions);
54
    }
55
56
    /**
57
     * Get the value of a cookie or all cookies.
58
     *
59
     * @param string|null $name The name of the cookie. If null, returns an array of all cookies.
60
     * @return mixed            The value of the cookie or an array of all cookies.
61
     */
62
    public function get(?string $name = null)
63
    {
64
        if (!$name) {
65
            return filter_input_array(INPUT_COOKIE, FILTER_DEFAULT);
66
        }
67
68
        return filter_input(INPUT_COOKIE, $name, FILTER_DEFAULT);
69
    }
70
71
    /**
72
     * Delete a cookie or all cookies.
73
     *
74
     * @param string|null $name The name of the cookie. If null, deletes all cookies.
75
     * @return bool             True if the cookie(s) is deleted successfully, false otherwise.
76
     */
77
    public function delete(?string $name = null): bool
78
    {
79
        $options = array_merge($this->cookieOptions, ['expires' => -1]);
80
        if (!$name) {
81
            array_map(fn($name) => setcookie($name, null, $options), array_keys($_COOKIE));
82
            return true;
83
        }
84
85
        return setcookie($name, null, $options);
86
    }
87
}
88