1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace COG\ChronoShifter\Shifter; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Base class for day of week shifters. |
7
|
|
|
* |
8
|
|
|
* @author Kristjan Siimson <[email protected]> |
9
|
|
|
* @package Shifter\Domain |
10
|
|
|
*/ |
11
|
|
|
abstract class DayOfWeekShifter implements Shifter |
12
|
|
|
{ |
13
|
|
|
const MONDAY = 1; |
14
|
|
|
|
15
|
|
|
const TUESDAY = 2; |
16
|
|
|
|
17
|
|
|
const WEDNESDAY = 3; |
18
|
|
|
|
19
|
|
|
const THURSDAY = 4; |
20
|
|
|
|
21
|
|
|
const FRIDAY = 5; |
22
|
|
|
|
23
|
|
|
const SATURDAY = 6; |
24
|
|
|
|
25
|
|
|
const SUNDAY = 7; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var int 1-7 |
29
|
|
|
*/ |
30
|
|
|
protected $dayOfWeek; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param int $dayOfWeek |
34
|
|
|
*/ |
35
|
34 |
|
public function __construct($dayOfWeek) |
36
|
|
|
{ |
37
|
34 |
|
if (false === filter_var($dayOfWeek, FILTER_VALIDATE_INT)) { |
38
|
4 |
|
throw new \InvalidArgumentException('Integer required'); |
39
|
|
|
} |
40
|
|
|
|
41
|
30 |
|
if ($dayOfWeek <= 0) { |
42
|
4 |
|
throw new \OutOfBoundsException('Day of week less than 1'); |
43
|
|
|
} |
44
|
|
|
|
45
|
26 |
|
if ($dayOfWeek > 7) { |
46
|
4 |
|
throw new \OutOfBoundsException('Day of week greater than 7'); |
47
|
|
|
} |
48
|
|
|
|
49
|
22 |
|
$this->dayOfWeek = $dayOfWeek; |
50
|
22 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param \DateTime $date |
54
|
|
|
*/ |
55
|
|
|
abstract public function shift(\DateTime $date); |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param \DateTime $time |
59
|
|
|
* @param $dayOfWeek |
60
|
|
|
* @return int |
61
|
|
|
*/ |
62
|
9 |
|
protected function getFirstDayOfMonthHavingDayOfWeek( |
63
|
|
|
\DateTime $time, |
64
|
|
|
$dayOfWeek |
65
|
|
|
) { |
66
|
9 |
|
$dayOfWeekOfFirstDayOfMonth = $this->getDayOfWeekForFirstDayOfMonth($time); |
67
|
|
|
|
68
|
9 |
|
return (int)((7 - $dayOfWeekOfFirstDayOfMonth + $dayOfWeek) % 7) + 1; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param \DateTime $time |
73
|
|
|
* @param int $dayOfWeek |
74
|
|
|
* @return int |
75
|
|
|
*/ |
76
|
9 |
|
protected function getLastDayOfMonthHavingDayOfWeek( |
77
|
|
|
\DateTime $time, |
78
|
|
|
$dayOfWeek |
79
|
|
|
) { |
80
|
9 |
|
$daysInMonth = (int)$time->format('t'); |
81
|
9 |
|
$dayOfWeekOfLastDayOfMonth = $this->getDayOfWeekOfLastDayOfMonth($time); |
82
|
9 |
|
$offset = ($dayOfWeekOfLastDayOfMonth - $dayOfWeek + 7) % 7; |
83
|
|
|
|
84
|
9 |
|
return $daysInMonth - $offset; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param \DateTime $time |
89
|
|
|
* @return int |
90
|
|
|
*/ |
91
|
9 |
|
protected function getDayOfWeekForFirstDayOfMonth(\DateTime $time) |
92
|
|
|
{ |
93
|
9 |
|
return (int)date('N', $this->getTimestampForFirstDayOfMonth($time)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param \DateTime $time |
98
|
|
|
* @return int |
99
|
|
|
*/ |
100
|
9 |
|
protected function getDayOfWeekOfLastDayOfMonth(\DateTime $time) |
101
|
|
|
{ |
102
|
9 |
|
return (int)date('N', $this->getTimestampForLastDayOfMonth($time)); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param \DateTime $time |
107
|
|
|
* @return int |
108
|
|
|
*/ |
109
|
9 |
View Code Duplication |
protected function getTimestampForFirstDayOfMonth(\DateTime $time) |
|
|
|
|
110
|
|
|
{ |
111
|
9 |
|
return mktime( |
112
|
9 |
|
null, |
113
|
9 |
|
null, |
114
|
9 |
|
null, |
115
|
9 |
|
$time->format('m'), |
116
|
9 |
|
1, |
117
|
9 |
|
$time->format('Y') |
118
|
9 |
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param \DateTime $time |
123
|
|
|
* @return int |
124
|
|
|
*/ |
125
|
9 |
View Code Duplication |
protected function getTimestampForLastDayOfMonth(\DateTime $time) |
|
|
|
|
126
|
|
|
{ |
127
|
9 |
|
return mktime( |
128
|
9 |
|
null, |
129
|
9 |
|
null, |
130
|
9 |
|
null, |
131
|
9 |
|
$time->format('m'), |
132
|
9 |
|
$time->format('t'), |
133
|
9 |
|
$time->format('Y') |
134
|
9 |
|
); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param \DateTime $time |
139
|
|
|
* @param $day |
140
|
|
|
*/ |
141
|
18 |
|
protected function setCalendarDay(\DateTime $time, $day) |
142
|
|
|
{ |
143
|
18 |
|
$time->setDate($time->format('Y'), $time->format('n'), $day); |
144
|
18 |
|
} |
145
|
|
|
|
146
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.