|
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 ($this->start_date->getTimestamp() <= $calendar_response->getStartDate()->getTimestamp() && |
|
49
|
|
|
$this->end_date->getTimestamp() >= $calendar_response->getEndDate()->getTimestamp() && |
|
50
|
|
|
$this->checkin_day != $calendar_response->getStartDate()->format('N')) { |
|
51
|
|
|
|
|
52
|
|
|
$units = $this->getUnits(); |
|
53
|
|
|
|
|
54
|
|
|
$included_set = $calendar_response->getIncluded(); |
|
55
|
|
|
|
|
56
|
|
|
foreach ($included_set as $unit_id => $set) { |
|
57
|
|
|
if (isset($units[$unit_id]) || empty($units)) { |
|
58
|
|
|
$calendar_response->removeFromMatched($included_set[$unit_id]['unit'], CalendarResponse::INVALID_STATE); |
|
59
|
|
|
|
|
60
|
|
|
$this->affected_units[$unit_id] = $included_set[$unit_id]['unit']; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Generates a text describing an availability_constraint. |
|
68
|
|
|
* |
|
69
|
|
|
* @return string |
|
70
|
|
|
* The formatted message. |
|
71
|
|
|
*/ |
|
72
|
|
|
public function toString() { |
|
73
|
|
|
$text = ''; |
|
74
|
|
|
$args = array(); |
|
75
|
|
|
|
|
76
|
|
|
$start_date = FALSE; |
|
77
|
|
|
$end_date = FALSE; |
|
78
|
|
|
|
|
79
|
|
|
// Day of the week constraint variable. |
|
80
|
|
|
$day_of_the_week = $this->getWeekDay($this->checkin_day); |
|
81
|
|
|
|
|
82
|
|
|
// Date range constraint variables. |
|
83
|
|
|
if ($this->start_date !== NULL) { |
|
84
|
|
|
$start_date = $this->start_date->format('Y-m-d'); |
|
85
|
|
|
} |
|
86
|
|
|
if ($this->start_date !== NULL) { |
|
87
|
|
|
$end_date = $this->end_date->format('Y-m-d'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// Finally, build out the constraint text string adding components |
|
91
|
|
|
// as necessary. |
|
92
|
|
|
|
|
93
|
|
|
// Specify a date range constraint. |
|
94
|
|
View Code Duplication |
if ($start_date && $end_date) { |
|
|
|
|
|
|
95
|
|
|
$text = 'From @start_date to @end_date'; |
|
96
|
|
|
|
|
97
|
|
|
$args['@start_date'] = $start_date; |
|
98
|
|
|
$args['@end_date'] = $end_date; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
// Specify the day of the week constraint. |
|
102
|
|
View Code Duplication |
if ($day_of_the_week) { |
|
|
|
|
|
|
103
|
|
|
if ($start_date && $end_date) { |
|
104
|
|
|
$text = 'From @start_date to @end_date, if booking starts on @day_of_the_week'; |
|
105
|
|
|
} |
|
106
|
|
|
else { |
|
107
|
|
|
$text = 'If booking starts on @day_of_the_week'; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$args['@day_of_the_week'] = $day_of_the_week; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return array('text' => $text, 'args' => $args); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param $day |
|
118
|
|
|
* @return string |
|
119
|
|
|
*/ |
|
120
|
|
View Code Duplication |
private function getWeekDay($day) { |
|
|
|
|
|
|
121
|
|
|
$weekdays = array( |
|
122
|
|
|
1 => 'Monday', |
|
123
|
|
|
2 => 'Tuesday', |
|
124
|
|
|
3 => 'Wednesday', |
|
125
|
|
|
4 => 'Thursday', |
|
126
|
|
|
5 => 'Friday', |
|
127
|
|
|
6 => 'Saturday', |
|
128
|
|
|
7 => 'Sunday', |
|
129
|
|
|
); |
|
130
|
|
|
|
|
131
|
|
|
return isset($weekdays[$day]) ? $weekdays[$day] : ''; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
} |
|
135
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..