1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright (c) Andreas Heigl<[email protected]> |
7
|
|
|
* |
8
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
9
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
10
|
|
|
* in the Software without restriction, including without limitation the rights |
11
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
12
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
13
|
|
|
* furnished to do so, subject to the following conditions: |
14
|
|
|
* |
15
|
|
|
* The above copyright notice and this permission notice shall be included in |
16
|
|
|
* all copies or substantial portions of the Software. |
17
|
|
|
* |
18
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
20
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
21
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
22
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
23
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
24
|
|
|
* THE SOFTWARE. |
25
|
|
|
* |
26
|
|
|
* @author Andreas Heigl<[email protected]> |
27
|
|
|
* @copyright Andreas Heigl |
28
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT-License |
29
|
|
|
* @since 08.03.2017 |
30
|
|
|
* @link http://github.com/heiglandreas/org.heigl.Holidaychecker |
31
|
|
|
*/ |
32
|
|
|
|
33
|
|
|
namespace Org_Heigl\Holidaychecker\IteratorItem; |
34
|
|
|
|
35
|
|
|
use DateTimeInterface; |
36
|
|
|
use IntlCalendar; |
37
|
|
|
use Org_Heigl\Holidaychecker\CalendarDay; |
38
|
|
|
use Org_Heigl\Holidaychecker\HolidayIteratorItemInterface; |
39
|
|
|
use function array_filter; |
40
|
|
|
use function array_map; |
41
|
|
|
use function in_array; |
42
|
|
|
|
43
|
|
|
class DateFollowUp implements HolidayIteratorItemInterface |
44
|
|
|
{ |
45
|
|
|
/** @var CalendarDay */ |
46
|
|
|
private $day; |
47
|
|
|
|
48
|
|
|
/** @var bool */ |
49
|
|
|
private $holiday; |
50
|
|
|
|
51
|
|
|
/** @var string */ |
52
|
|
|
private $name; |
53
|
|
|
|
54
|
|
|
/** @var string */ |
55
|
|
|
private $followup; |
56
|
|
|
|
57
|
|
|
/** @var IntlCalendar::DOW*[] */ |
58
|
|
|
private $replaced; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param array<"sunday"|"monday"|"tuesday"|"wednesday"|"thursday"|"friday"|"saturday"> $replaced |
|
|
|
|
62
|
|
|
*/ |
63
|
|
|
public function __construct(string $name, bool $holiday, CalendarDay $day, string $followup, array $replaced = []) |
64
|
|
|
{ |
65
|
|
|
$this->day = $day; |
66
|
|
|
$this->followup = $followup; |
67
|
|
|
$this->holiday = $holiday; |
68
|
|
|
$this->name = $name; |
69
|
|
|
$this->replaced = $this->replacedDays($replaced); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param array<"sunday"|"monday"|"tuesday"|"wednesday"|"thursday"|"friday"|"saturday"|"foo"> $replaced |
|
|
|
|
74
|
|
|
* @return IntlCalendar::DOW_*[] |
75
|
|
|
*/ |
76
|
|
|
private static function replacedDays(array $replaced): array |
77
|
|
|
{ |
78
|
|
|
$daymap = [ |
79
|
|
|
'sunday' => IntlCalendar::DOW_SUNDAY, |
80
|
|
|
'monday' => IntlCalendar::DOW_MONDAY, |
81
|
|
|
'tuesday' => IntlCalendar::DOW_TUESDAY, |
82
|
|
|
'wednesday' => IntlCalendar::DOW_WEDNESDAY, |
83
|
|
|
'thursday' => IntlCalendar::DOW_THURSDAY, |
84
|
|
|
'friday' => IntlCalendar::DOW_FRIDAY, |
85
|
|
|
'saturday' => IntlCalendar::DOW_SATURDAY, |
86
|
|
|
]; |
87
|
|
|
|
88
|
|
|
if ([] === $replaced) { |
89
|
|
|
return [ |
|
|
|
|
90
|
|
|
IntlCalendar::DOW_SATURDAY, |
91
|
|
|
IntlCalendar::DOW_SUNDAY, |
92
|
|
|
]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return array_filter(array_map(function (string $day) use ($daymap) { |
|
|
|
|
96
|
|
|
if (!isset($daymap[$day])) { |
97
|
|
|
return null; |
98
|
|
|
} |
99
|
|
|
return $daymap[$day]; |
100
|
|
|
}, $replaced)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function dateMatches(DateTimeInterface $date): bool |
104
|
|
|
{ |
105
|
|
|
$gregorianYear = (int) $date->format('Y'); |
106
|
|
|
$weekday = $this->day->getWeekdayForGregorianYear($gregorianYear); |
107
|
|
|
|
108
|
|
|
if (in_array($weekday, $this->replaced)) { |
|
|
|
|
109
|
|
|
return $this->day->isFollowUpDay($date, $this->followup); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $this->day->isSameDay($date); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function getName(): string |
116
|
|
|
{ |
117
|
|
|
return $this->name; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function isHoliday(): bool |
121
|
|
|
{ |
122
|
|
|
return $this->holiday; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|