Completed
Push — master ( bb9431...733af7 )
by Beau
18s queued 11s
created

SameSite::none()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Dflydev\FigCookies\Modifier;
6
7
use function sprintf;
8
use function strtolower;
9
10
final class SameSite
11
{
12
    /**
13
     * The possible string values of the SameSite setting
14
     */
15
    private const STRICT = 'Strict';
16
    private const LAX    = 'Lax';
17
    private const NONE   = 'None';
18
19
    /** @var string */
20
    private $value;
21
22
    private function __construct(string $value)
23
    {
24
        $this->value = $value;
25
    }
26
27
    public static function strict() : self
28
    {
29
        return new self(self::STRICT);
30
    }
31
32
    public static function lax() : self
33
    {
34
        return new self(self::LAX);
35
    }
36
37
    public static function none() : self
38
    {
39
        return new self(self::NONE);
40
    }
41
42
    /**
43
     * @throws \InvalidArgumentException If the given SameSite string is neither strict nor lax.
44
     */
45
    public static function fromString(string $sameSite) : self
46
    {
47
        $lowerCaseSite = strtolower($sameSite);
48
49
        if ($lowerCaseSite === 'strict') {
50
            return self::strict();
51
        }
52
53
        if ($lowerCaseSite === 'lax') {
54
            return self::lax();
55
        }
56
57
        if ($lowerCaseSite === 'none') {
58
            return self::none();
59
        }
60
61
        throw new \InvalidArgumentException(sprintf(
62
            'Expected modifier value to be either "strict", "lax", or "none", "%s" given',
63
            $sameSite
64
        ));
65
    }
66
67
    public function asString() : string
68
    {
69
        return 'SameSite=' . $this->value;
70
    }
71
}
72