CacheControl::__toString()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
namespace IQParts\Content\Object;
4
5
final class CacheControl
6
{
7
    public const MUST_REVALIDATE = 0;
8
    public const NO_TRANSFORM = 1;
9
    public const NO_CACHE = 2;
10
    public const NO_STORE = 3;
11
    public const PUBLIC = 4;
12
    public const PRIVATE = 5;
13
    public const PROXY = 6;
14
    public const MAX_AGE = 7;
15
    public const S_MAX_AGE = 8;
16
17
    /**
18
     * @var array
19
     */
20
    private $modes = [
21
        'must-revalidate',
22
        'no-transform',
23
        'no-cache',
24
        'no-store',
25
        'public',
26
        'private',
27
        'proxy-revalidate',
28
        'max-age',
29
        's-max-age'
30
    ];
31
32
    /**
33
     * @var int
34
     */
35
    private $mode;
36
37
    /**
38
     * @var int
39
     */
40
    private $seconds;
41
42
    /**
43
     * CacheControl constructor.
44
     * @param int $mode
45
     * @param int $seconds
46
     */
47
    public function __construct(int $mode, int $seconds = -1)
48
    {
49
        if ($mode < 0 || $mode > 7) {
50
            throw new \InvalidArgumentException('Invalid mode.');
51
        }
52
53
        if ($mode >= 7 && $seconds < 0) {
54
            throw new \InvalidArgumentException('Invalid number of seconds.');
55
        }
56
57
        $this->mode = $mode;
58
        $this->seconds = $seconds;
59
    }
60
61
    /**
62
     * @return int|string
63
     */
64
    public function __toString()
65
    {
66
        $mode = $this->modes[$this->mode];
67
68
        if ($this->mode >= 7) {
69
            return $mode . '=' . (string)$this->seconds;
70
        }
71
72
        return $mode;
73
    }
74
}