Total Complexity | 22 |
Total Lines | 95 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | <?php |
||
8 | class Period |
||
9 | { |
||
10 | /** |
||
11 | * @var DateTime |
||
12 | */ |
||
13 | private $start; |
||
14 | |||
15 | /** |
||
16 | * @var DateTime |
||
17 | */ |
||
18 | private $end; |
||
19 | |||
20 | 1 | private function __construct( |
|
26 | 1 | } |
|
27 | |||
28 | 2 | public static function create( |
|
29 | DateTime $start, |
||
30 | DateTime $end |
||
31 | ) { |
||
32 | 2 | if ($start > $end) { |
|
33 | 1 | throw new EndBeforeStartException(); |
|
34 | } |
||
35 | |||
36 | 1 | return new static( |
|
37 | 1 | $start, |
|
38 | $end |
||
39 | ); |
||
40 | } |
||
41 | |||
42 | 1 | public function getStart(): DateTime |
|
43 | { |
||
44 | 1 | return $this->start; |
|
45 | } |
||
46 | |||
47 | 1 | public function getEnd(): DateTime |
|
48 | { |
||
49 | 1 | return $this->end; |
|
50 | } |
||
51 | |||
52 | 3 | public function equals(Period $period): bool |
|
53 | { |
||
54 | 3 | return $this->getStart() == $period->getStart() |
|
55 | 3 | && $this->getEnd() == $period->getEnd(); |
|
56 | } |
||
57 | |||
58 | 2 | public function in(Period $period): bool |
|
59 | { |
||
60 | 2 | if ($this->getStart() >= $period->getStart() |
|
61 | 2 | && $this->getStart() <= $period->getEnd() |
|
62 | 2 | && $this->getEnd() >= $period->getStart() |
|
63 | 2 | && $this->getEnd() <= $period->getEnd() |
|
64 | ) { |
||
65 | 1 | return true; |
|
66 | } |
||
67 | |||
68 | 1 | return false; |
|
69 | } |
||
70 | |||
71 | 2 | public function encloses(Period $period): bool |
|
72 | { |
||
73 | 2 | if ($this->getStart() <= $period->getStart() |
|
74 | 2 | && $this->getEnd() >= $period->getEnd() |
|
75 | ) { |
||
76 | 1 | return true; |
|
77 | } |
||
78 | |||
79 | 1 | return false; |
|
80 | } |
||
81 | |||
82 | 5 | public function intersects(Period $period): bool |
|
103 | } |
||
104 | } |