Expiration::__toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace ElePHPant\Cookie\ValueObjects;
4
5
use ElePHPant\Cookie\Exceptions\InvalidParamException;
6
7
/**
8
 * Class Expiration
9
 *
10
 * Please report bugs on https://github.com/wilderamorim/cookie/issues
11
 *
12
 * @author Wilder Amorim <https://github.com/wilderamorim>
13
 * @link https://www.linkedin.com/in/wilderamorim/
14
 */
15
final class Expiration
16
{
17
    /** @var string The expiration unit. */
18
    private string $expiration;
19
20
    /** The valid expiration units. */
21
    private const VALID_UNITS = [
22
        'second', 'seconds',
23
        'minute', 'minutes',
24
        'hour', 'hours',
25
        'day', 'days',
26
        'week', 'weeks',
27
        'month', 'months',
28
        'year', 'years',
29
    ];
30
31
    /**
32
     * Create a new Expiration instance.
33
     *
34
     * @param string $expiration The expiration unit.
35
     * @throws InvalidParamException If the provided expiration unit is invalid.
36
     */
37
    public function __construct(string $expiration)
38
    {
39
        if (!in_array($expiration, self::VALID_UNITS)) {
40
            throw new InvalidParamException("Invalid expiration unit: '$expiration'.");
41
        }
42
43
        $this->expiration = $expiration;
44
    }
45
46
    /**
47
     * Get the string representation of the expiration unit.
48
     *
49
     * @return string The expiration unit.
50
     */
51
    public function __toString(): string
52
    {
53
        return $this->expiration;
54
    }
55
}