|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* |
|
5
|
|
|
* The MIT License (MIT) |
|
6
|
|
|
* |
|
7
|
|
|
* Copyright (c) 2015 Daniel Popiniuc |
|
8
|
|
|
* |
|
9
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
10
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
11
|
|
|
* in the Software without restriction, including without limitation the rights |
|
12
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
13
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
14
|
|
|
* furnished to do so, subject to the following conditions: |
|
15
|
|
|
* |
|
16
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
17
|
|
|
* copies or substantial portions of the Software. |
|
18
|
|
|
* |
|
19
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
20
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
21
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
22
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
23
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
24
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
25
|
|
|
* SOFTWARE. |
|
26
|
|
|
* |
|
27
|
|
|
*/ |
|
28
|
|
|
|
|
29
|
|
|
namespace danielgp\bank_holidays; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Return a list of all Romanian Holidays between 2001 and 2020 |
|
33
|
|
|
* |
|
34
|
|
|
* @author Daniel Popiniuc |
|
35
|
|
|
*/ |
|
36
|
|
|
trait Romanian |
|
37
|
|
|
{ |
|
38
|
|
|
|
|
39
|
|
|
private function getEasterDatetime($year) { |
|
40
|
|
|
$base = new \DateTime("$year-03-21"); |
|
41
|
|
|
$days = easter_days($year); |
|
42
|
|
|
return $base->add(new \DateInterval("P{$days}D")); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
private function readTypeFromJsonFile($fileBaseName) { |
|
46
|
|
|
$fName = __DIR__ . DIRECTORY_SEPARATOR . 'json' . DIRECTORY_SEPARATOR . $fileBaseName . '.min.json'; |
|
47
|
|
|
$fJson = fopen($fName, 'r'); |
|
48
|
|
|
$jSonContent = fread($fJson, filesize($fName)); |
|
49
|
|
|
fclose($fJson); |
|
50
|
|
|
return json_decode($jSonContent, true); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* List of legal holidays |
|
55
|
|
|
* |
|
56
|
|
|
* @param date $lngDate |
|
57
|
|
|
* @param boolean $inclCatholicEaster |
|
58
|
|
|
* @return array |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function setHolidays($lngDate, $inclCatholicEaster = false, $inclWorkingHolidays = false) { |
|
61
|
|
|
$givenYear = date('Y', $lngDate); |
|
62
|
|
|
$daying = array_merge($this->setHolidaysOrthodoxEaster($lngDate), $this->setHolidaysFixed($lngDate)); |
|
63
|
|
|
if ($inclWorkingHolidays) { |
|
64
|
|
|
$daying = array_merge($daying, $this->setHolidaysFixedButWorking($lngDate)); |
|
65
|
|
|
} |
|
66
|
|
|
if ($inclCatholicEaster) { // Catholic easter is already known by PHP |
|
67
|
|
|
$firstEasterDate = strtotime($this->getEasterDatetime($givenYear)->format('Y-m-d')); |
|
68
|
|
|
$secondEasterDate = strtotime('+1 day', $firstEasterDate); |
|
69
|
|
|
$daying = array_merge($daying, [ |
|
70
|
|
|
$firstEasterDate, |
|
71
|
|
|
$secondEasterDate, |
|
72
|
|
|
]); |
|
73
|
|
|
} |
|
74
|
|
|
sort($daying); |
|
75
|
|
|
return array_unique($daying); // remove duplicate for when catholic and orthodox easter match |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* List of all Romanian fixed holidays |
|
80
|
|
|
* (where fixed means every single year occur on same day of the month) |
|
81
|
|
|
* |
|
82
|
|
|
* @param date $lngDate |
|
83
|
|
|
* @return array |
|
84
|
|
|
*/ |
|
85
|
|
|
private function setHolidaysFixed($lngDate) { |
|
86
|
|
|
$givenYear = date('Y', $lngDate); |
|
87
|
|
|
$daying[] = mktime(0, 0, 0, 1, 1, $givenYear); // Happy New Year |
|
|
|
|
|
|
88
|
|
|
$daying[] = mktime(0, 0, 0, 1, 2, $givenYear); // recovering from New Year party |
|
89
|
|
|
$daying[] = mktime(0, 0, 0, 5, 1, $givenYear); // May 1st |
|
90
|
|
|
if ($givenYear >= 2009) { |
|
91
|
|
|
$daying[] = mktime(0, 0, 0, 8, 15, $givenYear); // St. Marry |
|
92
|
|
|
} |
|
93
|
|
|
if ($givenYear >= 2012) { |
|
94
|
|
|
$daying[] = mktime(0, 0, 0, 11, 30, $givenYear); // St. Andrew |
|
95
|
|
|
} |
|
96
|
|
|
$daying[] = mktime(0, 0, 0, 12, 1, $givenYear); // Romanian National Day |
|
97
|
|
|
$daying[] = mktime(0, 0, 0, 12, 25, $givenYear); // December 25th |
|
98
|
|
|
$daying[] = mktime(0, 0, 0, 12, 26, $givenYear); // December 26th |
|
99
|
|
|
return $daying; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* List of Romanian fixed holidays that are still working (weird ones) |
|
104
|
|
|
* (where fixed means every single year occur on same day of the month) |
|
105
|
|
|
* |
|
106
|
|
|
* @param date $lngDate |
|
107
|
|
|
* @return array |
|
108
|
|
|
*/ |
|
109
|
|
|
private function setHolidaysFixedButWorking($lngDate) { |
|
110
|
|
|
$daying = []; |
|
111
|
|
|
$givenYear = date('Y', $lngDate); |
|
112
|
|
|
if ($givenYear >= 2015) { |
|
113
|
|
|
$daying[] = mktime(0, 0, 0, 1, 24, $givenYear); // Unirea Principatelor Romane |
|
114
|
|
|
} |
|
115
|
|
|
if ($givenYear >= 2016) { |
|
116
|
|
|
$daying[] = mktime(0, 0, 0, 2, 19, $givenYear); // Constantin Brancusi birthday |
|
117
|
|
|
} |
|
118
|
|
|
return $daying; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* List of all Orthodox holidays between 2011 and 2020 |
|
123
|
|
|
* |
|
124
|
|
|
* @param date $lngDate |
|
125
|
|
|
* @return array |
|
126
|
|
|
*/ |
|
127
|
|
|
private function setHolidaysOrthodoxEaster($lngDate) { |
|
128
|
|
|
$givenYear = date('Y', $lngDate); |
|
129
|
|
|
$daying = []; |
|
130
|
|
|
$statmentsArray = $this->readTypeFromJsonFile('RomanianBankHolidays'); |
|
131
|
|
|
if (array_key_exists($givenYear, $statmentsArray)) { |
|
132
|
|
|
foreach ($statmentsArray[$givenYear] as $value) { |
|
133
|
|
|
$daying[] = strtotime($value); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
return $daying; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* returns bank holidays in a given month |
|
141
|
|
|
* |
|
142
|
|
|
* @param date $lngDate |
|
143
|
|
|
* @param boolean $inclCatholicEaster |
|
144
|
|
|
* @return int |
|
145
|
|
|
*/ |
|
146
|
|
|
protected function setHolidaysInMonth($lngDate, $inclCatholicEaster = false) { |
|
147
|
|
|
$holidaysInGivenYear = $this->setHolidays($lngDate, $inclCatholicEaster); |
|
148
|
|
|
$thisMonthDayArray = $this->setMonthAllDaysIntoArray($lngDate); |
|
149
|
|
|
$holidays = 0; |
|
150
|
|
|
foreach ($thisMonthDayArray as $value) { |
|
151
|
|
|
if (in_array($value, $holidaysInGivenYear)) { |
|
152
|
|
|
$holidays += 1; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
return $holidays; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
protected function setMonthAllDaysIntoArray($lngDate) { |
|
159
|
|
|
$firstDayGivenMonth = strtotime('first day of', $lngDate); |
|
160
|
|
|
$lastDayInGivenMonth = strtotime('last day of', $lngDate); |
|
161
|
|
|
$secondsInOneDay = 24 * 60 * 60; |
|
162
|
|
|
return range($firstDayGivenMonth, $lastDayInGivenMonth, $secondsInOneDay); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* returns working days in a given month |
|
167
|
|
|
* |
|
168
|
|
|
* @param date $lngDate |
|
169
|
|
|
* @param boolean $inclCatholicEaster |
|
170
|
|
|
* @return int |
|
171
|
|
|
*/ |
|
172
|
|
|
protected function setWorkingDaysInMonth($lngDate, $inclCatholicEaster = false) { |
|
173
|
|
|
$holidaysInGivenYear = $this->setHolidays($lngDate, $inclCatholicEaster); |
|
174
|
|
|
$thisMonthDayArray = $this->setMonthAllDaysIntoArray($lngDate); |
|
175
|
|
|
$workingDays = 0; |
|
176
|
|
|
foreach ($thisMonthDayArray as $value) { |
|
177
|
|
|
if (!in_array(strftime('%w', $value), [0, 6]) && !in_array($value, $holidaysInGivenYear)) { |
|
178
|
|
|
$workingDays += 1; |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
return $workingDays; |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.