1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Andegna\Operations; |
4
|
|
|
|
5
|
|
|
use Andegna\Converter\Converter; |
6
|
|
|
use Andegna\Converter\FromJdnConverter; |
7
|
|
|
use Andegna\Validator\LeapYearValidator; |
8
|
|
|
use DateTime as GregorianDateTime; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Initiator trait. |
12
|
|
|
*/ |
13
|
|
|
trait Initiator |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* This fields are just for catching. |
17
|
|
|
* |
18
|
|
|
* @return void |
19
|
|
|
*/ |
20
|
49 |
|
protected function updateComputedFields() |
21
|
|
|
{ |
22
|
|
|
// Julian Date Number of the available datetime |
23
|
49 |
|
$jdn = $this->getJdnFromBase($this->dateTime); |
|
|
|
|
24
|
|
|
|
25
|
49 |
|
$converter = new FromJdnConverter($jdn); |
26
|
|
|
|
27
|
49 |
|
$this->setDateFromConverter($converter); |
28
|
49 |
|
$this->cacheTimestamp(); |
29
|
49 |
|
$this->computeFields(); |
30
|
49 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Return the JDN of the given gregorian date time. |
34
|
|
|
* |
35
|
|
|
* @param GregorianDateTime $dateTime |
36
|
|
|
* |
37
|
|
|
* @return int |
38
|
|
|
*/ |
39
|
49 |
|
protected function getJdnFromBase(GregorianDateTime $dateTime): int |
40
|
|
|
{ |
41
|
49 |
|
$year = $dateTime->format('Y'); |
42
|
49 |
|
$month = $dateTime->format('m'); |
43
|
49 |
|
$day = $dateTime->format('d'); |
44
|
|
|
|
45
|
49 |
|
return gregoriantojd($month, $day, $year); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Set the converted year, month and day from the given converter. |
50
|
|
|
* |
51
|
|
|
* @param Converter $converter |
52
|
|
|
* |
53
|
|
|
* @return void |
54
|
|
|
*/ |
55
|
49 |
|
protected function setDateFromConverter(Converter $converter) |
56
|
|
|
{ |
57
|
49 |
|
$this->year = $converter->getYear(); |
|
|
|
|
58
|
49 |
|
$this->month = $converter->getMonth(); |
|
|
|
|
59
|
49 |
|
$this->day = $converter->getDay(); |
|
|
|
|
60
|
49 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Set the timestamp field. |
64
|
|
|
*/ |
65
|
49 |
|
protected function cacheTimestamp() |
66
|
|
|
{ |
67
|
49 |
|
$this->timestamp = $this->dateTime->getTimestamp(); |
|
|
|
|
68
|
49 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Computer the available properties. |
72
|
|
|
* |
73
|
|
|
* @return void |
74
|
|
|
*/ |
75
|
49 |
|
protected function computeFields() |
76
|
|
|
{ |
77
|
49 |
|
$this->computeLeapYear(); |
78
|
49 |
|
$this->computeDayOfYear(); |
79
|
49 |
|
$this->computeDaysInMonth(); |
80
|
49 |
|
$this->cacheDayOfWeek(); |
81
|
49 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Compute the leapYear property. |
85
|
|
|
* |
86
|
|
|
* @return void |
87
|
|
|
*/ |
88
|
49 |
|
protected function computeLeapYear() |
89
|
|
|
{ |
90
|
49 |
|
$leapYear = new LeapYearValidator($this->year); |
91
|
|
|
|
92
|
49 |
|
$this->leapYear = $leapYear->isValid(); |
|
|
|
|
93
|
49 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Compute the dayOfYear property. |
97
|
|
|
* |
98
|
|
|
* @return void |
99
|
|
|
*/ |
100
|
49 |
|
protected function computeDayOfYear() |
101
|
|
|
{ |
102
|
49 |
|
$this->dayOfYear = ($this->month - 1) * 30 + $this->day; |
|
|
|
|
103
|
49 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Compute the daysInMonth property. |
107
|
|
|
* |
108
|
|
|
* @return void |
109
|
|
|
*/ |
110
|
49 |
|
protected function computeDaysInMonth() |
111
|
|
|
{ |
112
|
49 |
|
$this->daysInMonth = $this->month === 13 ? ($this->leapYear ? 6 : 5) : 30; |
|
|
|
|
113
|
49 |
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* cache the dayOfWeek property. |
117
|
|
|
* |
118
|
|
|
* @return void |
119
|
|
|
*/ |
120
|
49 |
|
protected function cacheDayOfWeek() |
121
|
|
|
{ |
122
|
49 |
|
$this->dayOfWeek = (int) $this->dateTime->format('N'); |
|
|
|
|
123
|
49 |
|
} |
124
|
|
|
} |
125
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: