-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathMonth.php
More file actions
127 lines (102 loc) · 2.99 KB
/
Month.php
File metadata and controls
127 lines (102 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
declare(strict_types=1);
namespace App\Domain\Calendar;
use App\Infrastructure\ValueObject\Time\SerializableDateTime;
final readonly class Month
{
public const string MONTH_ID_FORMAT = 'Y-m';
private SerializableDateTime $firstDay;
private SerializableDateTime $lastDay;
private function __construct(
private int $year,
private int $month,
) {
$this->firstDay = SerializableDateTime::createFromFormat(
format: 'd-n-Y H:i:s',
datetime: '01-'.$this->month.'-'.$this->year.' 00:00:00',
);
$this->lastDay = SerializableDateTime::fromString($this->year.'-'.$this->month.'-'.$this->firstDay->format('t'));
}
public function getYear(): int
{
return $this->year;
}
public function getMonth(): int
{
return $this->month;
}
public static function fromDate(SerializableDateTime $date): self
{
return new self(
year: $date->getYear(),
month: $date->getMonthWithoutLeadingZero(),
);
}
public function getLabel(): string
{
return $this->firstDay->translatedFormat('F Y');
}
public function getShortLabel(): string
{
return $this->firstDay->translatedFormat('M Y');
}
public function getShortLabelWithoutYear(): string
{
return $this->firstDay->translatedFormat('M');
}
public function getId(): string
{
return $this->firstDay->format(self::MONTH_ID_FORMAT);
}
public function getNumberOfDays(): int
{
return (int) $this->firstDay->translatedFormat('t');
}
public function getWeekDayOfFirstDay(): int
{
// Numeric representation of week day, 1 (for Monday) through 7 (for Sunday)
return (int) $this->firstDay->translatedFormat('N');
}
public function getFirstDay(): SerializableDateTime
{
return $this->firstDay;
}
public function getPreviousMonth(): self
{
$date = $this->firstDay->modify('first day of previous month');
return self::fromDate($date);
}
public function getNextMonth(): self
{
$date = $this->firstDay->modify('first day of next month');
return self::fromDate($date);
}
public function isBefore(Month $other): bool
{
if ($this->getYear() < $other->getYear()) {
return true;
}
if ($this->getYear() > $other->getYear()) {
return false;
}
return $this->getMonth() < $other->getMonth();
}
public function isAfter(Month $other): bool
{
if ($this->getYear() > $other->getYear()) {
return true;
}
if ($this->getYear() < $other->getYear()) {
return false;
}
return $this->getMonth() > $other->getMonth();
}
public function getFrom(): SerializableDateTime
{
return $this->firstDay;
}
public function getTo(): SerializableDateTime
{
return $this->lastDay;
}
}