Completed
Push — master ( 59abc1...a59857 )
by Beau
35s
created

SameSite::asString()   A

Complexity

Conditions 2
Paths 2

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 2
nc 2
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
    /** @var bool */
13
    private $strict;
14
15
    private function __construct(bool $strict)
16
    {
17
        $this->strict = $strict;
18
    }
19
20
    public static function strict() : self
21
    {
22
        return new self(true);
23
    }
24
25
    public static function lax() : self
26
    {
27
        return new self(false);
28
    }
29
30
    /**
31
     * @throws \InvalidArgumentException If the given SameSite string is neither strict nor lax.
32
     */
33
    public static function fromString(string $sameSite) : self
34
    {
35
        $lowerCaseSite = strtolower($sameSite);
36
37
        if ($lowerCaseSite === 'strict') {
38
            return self::strict();
39
        }
40
41
        if ($lowerCaseSite === 'lax') {
42
            return self::lax();
43
        }
44
45
        throw new \InvalidArgumentException(sprintf(
46
            'Expected modifier value to be either "strict" or "lax", "%s" given',
47
            $sameSite
48
        ));
49
    }
50
51
    public function asString() : string
52
    {
53
        return 'SameSite=' . ($this->strict ? 'Strict' : 'Lax');
54
    }
55
}
56