-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathReservationRepeatService.php
More file actions
353 lines (317 loc) · 8.68 KB
/
ReservationRepeatService.php
File metadata and controls
353 lines (317 loc) · 8.68 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<?php
/**
* ReservationRepeatService.php
*
* @author Ryuji AMANO <ryuji@ryus.co.jp>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
*/
/**
* Class ReservationRepeatService
*/
class ReservationRepeatService {
/**
* @var array rruleでつかってる曜日文字
*/
protected $_weekday = [
0 => 'SU',
1 => 'MO',
2 => 'TU',
3 => 'WE',
4 => 'TH',
5 => 'FR',
6 => 'SA'
];
/**
* rruleと開始日から繰り返しの日付リストを返す
*
* @param array $rrule rrule
* @param string $startDate Y-m-d日付
* @return array Y-m-d日付のリスト
*/
public function getRepeatDateSet($rrule, $startDate) {
switch ($rrule['FREQ']) {
case 'DAILY':
$dateSet = $this->_getRepeatDateSetByDaily($rrule, $startDate);
break;
case 'WEEKLY':
$dateSet = $this->_getRepeatDateSetByWeekly($rrule, $startDate);
break;
case 'MONTHLY':
$dateSet = $this->_getRepeatDateSetByMonthly($rrule, $startDate);
break;
case 'YEARLY':
$dateSet = $this->_getRepeatDateSetByYearly($rrule, $startDate);
break;
default:
// $rruleに該当する繰り返しがなければ初日だけ
$dateSet = [
$startDate
];
break;
}
//debug($dateSet);
return $dateSet;
}
/**
* 日繰り返しの日付を返す
*
* @param array $rrule rrule
* @param string $startDate Y-m-d日付
* @return array Y-m-d日付のリスト
*/
protected function _getRepeatDateSetByDaily($rrule, $startDate) {
// 間隔
$interval = $rrule['INTERVAL'];
// 回数 COUNT か期日 UNTILまでか
$currentDate = $startDate;
$repeatDateSet = [];
// 回数指定 最初を1回目として数える
$count = 1;
$continue = true;
while ($continue) {
$repeatDateSet[] = $currentDate;
$currentDate = date(
'Y-m-d',
strtotime('+' . $interval . ' day', strtotime($currentDate))
);
$count++;
$continue = $this->_isContinue($rrule, $count, $currentDate);
}
return $repeatDateSet;
}
/**
* 週繰り返しの日付を返す
*
* @param array $rrule rrule
* @param string $startDate Y-m-d日付
* @return array Y-m-d日付のリスト
*/
protected function _getRepeatDateSetByWeekly($rrule, $startDate) {
// 間隔
$interval = $rrule['INTERVAL'];
// 回数 COUNT か期日 UNTILまでか
$currentDate = $startDate;
$repeatDateSet = [];
$bydayNumbers = [];
foreach ($rrule['BYDAY'] as $weekday) {
$bydayNumbers[] = array_search($weekday, $this->_weekday);
}
// 回数指定 最初を1回目として数える
$currentWeekEndDate = date('Y-m-d', strtotime('Saturday', strtotime($currentDate)));//
// 土曜日の日付取得
$continue = true;
$count = 1;
while ($continue) {
$repeatDateSet[] = $currentDate;
$next = false;
while ($next == false) {
$currentDate = date(
'Y-m-d',
strtotime('+1 day', strtotime($currentDate))
);
$currentWeekDayNumber = date('w', strtotime($currentDate));
//$next = true;
if (in_array($currentWeekDayNumber, $bydayNumbers)) {
// 繰り返し曜日に該当したら繰り返し日に保存
$next = true;
}
if ($currentDate >= $currentWeekEndDate) {
// 土曜になったら翌週へ。interval 1なら1週
$currentWeekEndDate = date(
'Y-m-d',
strtotime(
'+' . $interval . ' week
Saturday',
strtotime($currentWeekEndDate)
)
);
$currentDate = date(
'Y-m-d',
strtotime(
'+' . $interval - 1 . ' week',
strtotime($currentDate)
)
);
}
}
$count++;
$continue = $this->_isContinue($rrule, $count, $currentDate);
}
return $repeatDateSet;
}
/**
* 月繰り返しの日付を返す
*
* @param array $rrule rrule
* @param string $startDate Y-m-d日付
* @return array Y-m-d日付のリスト
*/
protected function _getRepeatDateSetByMonthly($rrule, $startDate) {
// 間隔
$interval = $rrule['INTERVAL'];
// 回数 COUNT か期日 UNTILまでか
$currentDate = $startDate;
$repeatDateSet = [];
//$bydayNumbers = [];
//foreach($rrule['BYDAY'] as $weekday){
// $bydayNumbers[] = array_search($weekday, $this->_weekday);
//}
// 回数指定 最初を1回目として数える
//$currentWeekEndDate = date('Y-m-d', strtotime('Saturday', strtotime($currentDate)));//
// 土曜日の日付取得
$currentMonth = date('Y-m', strtotime($currentDate));
$continue = true;
$count = 1;
while ($continue) {
$repeatDateSet[] = $currentDate;
$nthString = [
'-1' => 'last',
'1' => 'First',
'2' => 'Second',
'3' => 'Third',
'4' => 'Fourth',
];
$weekDayString = [
'SU' => 'Sunday',
'MO' => 'Monday',
'TU' => 'Tuesday',
'WE' => 'Wednesday',
'TH' => 'Thursday',
'FR' => 'Friday',
'SA' => 'Saturday',
];
$continue2 = true;
while ($continue2) {
if (isset($rrule['BYDAY'])) {
// 第n曜日繰り返し
$weekDay = substr($rrule['BYDAY'], -2);// SU, MO, ...SA
$nth = str_replace($weekDay, '', $rrule['BYDAY']); // -1 〜4
$nextString = sprintf(
'%s %s of %s',
$nthString[$nth],
$weekDayString[$weekDay],
$currentMonth
);
$nextDate = date('Y-m-d', strtotime($nextString));
} else {
//指定日繰り返し
$nextDate = $currentMonth . '-' . $rrule['BYMONTHDAY'];
}
if ($nextDate > $currentDate) {
// 1回目は第n $weekDay曜日が$cuurentDateより小さくなる可能性あるのでチェックする。
$currentDate = $nextDate;
//$repeatDateSet[] = $currentDate;
$continue2 = false;
}
// 次の繰り返しの年月
$currentMonth = date(
'Y-m',
strtotime('+' . $interval . ' month', strtotime($currentMonth))
);
}
$count++;
$continue = $this->_isContinue($rrule, $count, $currentDate);
}
return $repeatDateSet;
}
/**
* 年繰り返しの日付を返す
*
* @param array $rrule rrule
* @param string $startDate Y-m-d日付
* @return array Y-m-d日付のリスト
*/
protected function _getRepeatDateSetByYearly($rrule, $startDate) {
// 間隔
$interval = $rrule['INTERVAL'];
// 回数 COUNT か期日 UNTILまでか
$currentDate = $startDate;
$repeatDateSet = [];
// 回数指定 最初を1回目として数える
$count = 1; //次のカウント
// スタート年の繰り返し月日を得る
// 日付指定くりかえし
$currentYear = date('Y', strtotime($startDate));
$repeatDay = date('d', strtotime($startDate));
$continue = true;
$byMonth = $rrule['BYMONTH'];
$byMonthIndex = 0;
$byMonthLength = count($byMonth);
//$monthCycle = new ReservationCycle($rrule['BYMONTH']);
while ($continue) {
$repeatDateSet[] = $currentDate;
$date = '1900-01-01';
while ($date < $currentDate) {
$month = $byMonth[$byMonthIndex];
if (isset($rrule['BYDAY'])) {
//第nX曜日指定
$nthString = [
'-1' => 'last',
'1' => 'First',
'2' => 'Second',
'3' => 'Third',
'4' => 'Fourth',
];
$weekDayString = [
'SU' => 'Sunday',
'MO' => 'Monday',
'TU' => 'Tuesday',
'WE' => 'Wednesday',
'TH' => 'Thursday',
'FR' => 'Friday',
'SA' => 'Saturday',
];
$weekDay = substr($rrule['BYDAY'], -2);// SU, MO, ...SA
$nth = str_replace($weekDay, '', $rrule['BYDAY']); // -1 〜4
$nextString = sprintf(
'%s %s of %04d-%02d',
$nthString[$nth],
$weekDayString[$weekDay],
$currentYear,
$month
);
$date = date('Y-m-d', strtotime($nextString));
} else {
// 開始日で繰り返し
$date = sprintf('%04d-%02d-%02d', $currentYear, $month, $repeatDay);
}
if (date('m', strtotime($date)) != $month) {
// 2017-02-30とかって日時をstrtotimeしてdate('m')すると3月あつかいになる
// 翌月になってしまうなら月の最終日とする
$date('Y-m-d', strtotime('Last day of ' . $currentYear . ' ' . $month));
}
$byMonthIndex++;
if ($byMonthIndex >= $byMonthLength) {
$byMonthIndex = 0;
$currentYear = $currentYear + $interval;
}
}
$currentDate = $date;
$count++;
$continue = $this->_isContinue($rrule, $count, $currentDate);
}
return $repeatDateSet;
}
/**
* 繰り返し終了判定
*
* @param array $rrule rrule
* @param int $count 現在の繰返し回数
* @param string $currentDate Y-m-d 繰り返しで生成中の日付
* @return bool false:繰り返し終了 true : 繰り返し
*/
protected function _isContinue($rrule, $count, $currentDate) {
$continue = true;
if (isset($rrule['COUNT'])) {
if ($count > $rrule['COUNT']) {
$continue = false;
}
} else {
if ($currentDate > $rrule['UNTIL']) {
$continue = false;
}
}
return $continue;
}
}