1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @file |
5
|
|
|
* Class CheckInDayConstraint |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Roomify\Bat\Constraint; |
9
|
|
|
|
10
|
|
|
use Roomify\Bat\Calendar\CalendarResponse; |
11
|
|
|
use Roomify\Bat\Constraint\Constraint; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* |
15
|
|
|
*/ |
16
|
|
|
class CheckInDayConstraint extends Constraint { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var |
20
|
|
|
*/ |
21
|
|
|
protected $checkin_day; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param $units |
25
|
|
|
* @param $checkin_day |
26
|
|
|
*/ |
27
|
|
|
public function __construct($units, $checkin_day, $start_date = NULL, $end_date = NULL) { |
28
|
|
|
parent::__construct($units); |
29
|
|
|
|
30
|
|
|
$this->checkin_day = $checkin_day; |
31
|
|
|
$this->start_date = $start_date; |
32
|
|
|
$this->end_date = $end_date; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
|
|
public function applyConstraint(CalendarResponse &$calendar_response) { |
39
|
|
|
parent::applyConstraint($calendar_response); |
40
|
|
|
|
41
|
|
|
if ($this->start_date === NULL) { |
42
|
|
|
$this->start_date = new \DateTime('1970-01-01'); |
43
|
|
|
} |
44
|
|
|
if ($this->end_date === NULL) { |
45
|
|
|
$this->end_date = new \DateTime('2999-12-31'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if ( (($calendar_response->getStartDate()->getTimestamp() >= $this->start_date->getTimestamp() && |
49
|
|
|
$calendar_response->getStartDate()->getTimestamp() <= $this->end_date->getTimestamp()) || |
50
|
|
|
($calendar_response->getStartDate()->getTimestamp() <= $this->start_date->getTimestamp() && |
51
|
|
|
$calendar_response->getEndDate()->getTimestamp() >= $this->end_date->getTimestamp())) && |
52
|
|
|
$this->checkin_day != $calendar_response->getStartDate()->format('N') ) { |
53
|
|
|
|
54
|
|
|
$units = $this->getUnits(); |
55
|
|
|
|
56
|
|
|
$included_set = $calendar_response->getIncluded(); |
57
|
|
|
|
58
|
|
View Code Duplication |
foreach ($included_set as $unit_id => $set) { |
|
|
|
|
59
|
|
|
if (isset($units[$unit_id]) || empty($units)) { |
60
|
|
|
$calendar_response->removeFromMatched($included_set[$unit_id]['unit'], CalendarResponse::INVALID_STATE); |
61
|
|
|
|
62
|
|
|
$this->affected_units[$unit_id] = $included_set[$unit_id]['unit']; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Generates a text describing an availability_constraint. |
70
|
|
|
* |
71
|
|
|
* @return string |
72
|
|
|
* The formatted message. |
73
|
|
|
*/ |
74
|
|
|
public function toString() { |
75
|
|
|
$text = ''; |
76
|
|
|
$args = array(); |
77
|
|
|
|
78
|
|
|
$start_date = FALSE; |
79
|
|
|
$end_date = FALSE; |
80
|
|
|
|
81
|
|
|
// Day of the week constraint variable. |
82
|
|
|
$day_of_the_week = $this->getWeekDay($this->checkin_day); |
83
|
|
|
|
84
|
|
|
// Date range constraint variables. |
85
|
|
|
if ($this->start_date !== NULL) { |
86
|
|
|
$start_date = $this->start_date->format('Y-m-d'); |
87
|
|
|
} |
88
|
|
|
if ($this->start_date !== NULL) { |
89
|
|
|
$end_date = $this->end_date->format('Y-m-d'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// Finally, build out the constraint text string adding components |
93
|
|
|
// as necessary. |
94
|
|
|
|
95
|
|
|
// Specify a date range constraint. |
96
|
|
View Code Duplication |
if ($start_date && $end_date) { |
|
|
|
|
97
|
|
|
$text = 'From @start_date to @end_date'; |
98
|
|
|
|
99
|
|
|
$args['@start_date'] = $start_date; |
100
|
|
|
$args['@end_date'] = $end_date; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
// Specify the day of the week constraint. |
104
|
|
View Code Duplication |
if ($day_of_the_week) { |
|
|
|
|
105
|
|
|
if ($start_date && $end_date) { |
106
|
|
|
$text = 'From @start_date to @end_date, if booking starts on @day_of_the_week'; |
107
|
|
|
} else { |
108
|
|
|
$text = 'If booking starts on @day_of_the_week'; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$args['@day_of_the_week'] = $day_of_the_week; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return array('text' => $text, 'args' => $args); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param $day |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
View Code Duplication |
private function getWeekDay($day) { |
|
|
|
|
122
|
|
|
$weekdays = array( |
123
|
|
|
1 => 'Monday', |
124
|
|
|
2 => 'Tuesday', |
125
|
|
|
3 => 'Wednesday', |
126
|
|
|
4 => 'Thursday', |
127
|
|
|
5 => 'Friday', |
128
|
|
|
6 => 'Saturday', |
129
|
|
|
7 => 'Sunday', |
130
|
|
|
); |
131
|
|
|
|
132
|
|
|
return isset($weekdays[$day]) ? $weekdays[$day] : ''; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return int |
137
|
|
|
*/ |
138
|
|
|
public function getCheckinDay() { |
139
|
|
|
return $checkin_day; |
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param $checkin_day |
144
|
|
|
*/ |
145
|
|
|
public function setCheckinDay($checkin_day) { |
146
|
|
|
$this->checkin_day = $checkin_day; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
} |
150
|
|
|
|
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.