Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
CookieOptions
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
2 / 2
3
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 getExpires
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace PeServer\Core\Store;
6
7use DateInterval;
8use DateTimeImmutable;
9
10/**
11 * Cookie 設定。
12 */
13class CookieOptions
14{
15    #region variable
16
17    /**
18     * パス
19     *
20     * @var string
21     */
22    public readonly string $path;
23    /**
24     * 期限。
25     */
26    public ?DateInterval $span;
27    /**
28     * HTTPS に限定。
29     */
30    public readonly bool $secure;
31    /**
32     *  HTTP リクエストのみで使用。
33     */
34    public readonly bool $httpOnly;
35
36    /**
37     * 同じサイト。
38     *
39     * @var string
40     * @phpstan-var CookieSameSiteAlias
41     */
42    public readonly string $sameSite;
43
44    #endregion
45
46    /**
47     * 生成。
48     *
49     * @param string $path パス。
50     * @param DateInterval|null $span 期間。
51     * @param boolean $secure HTTPS に限定するか。
52     * @param boolean $httpOnly HTTP リクエストのみで使用するか。
53     * @param string $sameSite 同じサイト。
54     * @phpstan-param CookieSameSiteAlias $sameSite
55     */
56    public function __construct(string $path, ?DateInterval $span, bool $secure, bool $httpOnly, string $sameSite)
57    {
58        $this->path = $path;
59        $this->span = $span;
60        $this->secure = $secure;
61        $this->httpOnly = $httpOnly;
62        $this->sameSite = $sameSite;
63    }
64
65    #region function
66
67    /**
68     * cookie の寿命を数値に変換。
69     *
70     * @return int
71     */
72    public function getExpires(): int
73    {
74        if ($this->span === null) {
75            return 0;
76        }
77
78        $reference = new DateTimeImmutable();
79        $endTime = $reference->add($this->span);
80
81        $result = $endTime->getTimestamp() - $reference->getTimestamp();
82
83        return $result + time();
84    }
85
86    #endregion
87}