WeekDay::fromNumber()   B
last analyzed

Complexity

Conditions 8
Paths 8

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 8.064

Importance

Changes 0
Metric Value
cc 8
eloc 10
nc 8
nop 1
dl 0
loc 14
ccs 9
cts 10
cp 0.9
crap 8.064
rs 7.7777
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Adlogix\EventScheduler\ValueObject;
6
7
use DateTimeInterface;
8
use Webmozart\Assert\Assert;
9
10
/**
11
 * @author Toni Van de Voorde <[email protected]>
12
 */
13
final class WeekDay
14
{
15
    /**
16
     * @var int
17
     */
18
    private $number;
19
20
    /**
21
     * @param int $number
22
     */
23 9
    private function __construct(int $number)
24
    {
25 9
        Assert::range($number, 1, 7);
26 9
        $this->number = $number;
27 9
    }
28
29
    /**
30
     * @return int
31
     */
32 9
    public function number(): int
33
    {
34 9
        return $this->number;
35
    }
36
37
    /**
38
     * @param WeekDay $weekDay
39
     * @return bool
40
     */
41 9
    public function equals(WeekDay $weekDay): bool
42
    {
43 9
        return $this->number === $weekDay->number();
44
    }
45
46
    /**
47
     * @return WeekDay
48
     */
49
    public static function monday(): self
50
    {
51
        return new self(1);
52
    }
53
54
    /**
55
     * @return WeekDay
56
     */
57
    public static function tuesday(): self
58
    {
59
        return new self(2);
60
    }
61
62
    /**
63
     * @return WeekDay
64
     */
65
    public static function wednesday(): self
66
    {
67
        return new self(3);
68
    }
69
70
    /**
71
     * @return WeekDay
72
     */
73
    public static function thursday(): self
74
    {
75
        return new self(4);
76
    }
77
78
    /**
79
     * @return WeekDay
80
     */
81 1
    public static function friday(): self
82
    {
83 1
        return new self(5);
84
    }
85
86
    /**
87
     * @return WeekDay
88
     */
89
    public static function saturday(): self
90
    {
91
        return new self(6);
92
    }
93
94
    /**
95
     * @return WeekDay
96
     */
97 1
    public static function sunday(): self
98
    {
99 1
        return new self(7);
100
    }
101
102
    /**
103
     * @param DateTimeInterface $dateTime
104
     * @return WeekDay
105
     */
106 2
    final public static function fromDateTime(DateTimeInterface $dateTime): self
107
    {
108 2
        return WeekDay::fromNumber((int)$dateTime->format('N'));
109
    }
110
111
    /**
112
     * Returns a WeekDay instance from a numeric value. The representation is according to ISO-8601.
113
     *
114
     * @param int $number
115
     * @return WeekDay
116
     */
117 9
    final public static function fromNumber(int $number): self
118
    {
119
        switch ($number) {
120 9
            case 1:
121 8
            case 2:
122 7
            case 3:
123 6
            case 4:
124 5
            case 5:
125 4
            case 6:
126 3
            case 7:
127 9
                return new self($number);
128
        }
129
130
        throw new \InvalidArgumentException(sprintf('Given number %d is an invalid WeekDay number', $number));
131
    }
132
}
133