|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @file |
|
5
|
|
|
* Class CalendarResponse |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Roomify\Bat\Calendar; |
|
9
|
|
|
|
|
10
|
|
|
use Roomify\Bat\Unit\UnitInterface; |
|
11
|
|
|
use Roomify\Bat\Constraint\Constraint; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* A CalendarResponse contains the units that are matched or missed following |
|
15
|
|
|
* a search, together with the reason they are matched or missed. |
|
16
|
|
|
*/ |
|
17
|
|
|
class CalendarResponse { |
|
18
|
|
|
|
|
19
|
|
|
const VALID_STATE = 'valid_state'; |
|
20
|
|
|
const INVALID_STATE = 'invalid_state'; |
|
21
|
|
|
const CONSTRAINT = 'constraint'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $included_set; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $excluded_set; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var \DateTime |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $start_date; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var \DateTime |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $end_date; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var array |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $valid_states; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param $start_date |
|
50
|
|
|
* @param $end_date |
|
51
|
|
|
* @param $valid_states |
|
52
|
|
|
* @param $included |
|
53
|
|
|
* @param $excluded |
|
54
|
|
|
*/ |
|
55
|
|
|
public function __construct(\DateTime $start_date, \DateTime $end_date, $valid_states, $included = array(), $excluded = array()) { |
|
56
|
|
|
$this->start_date = $start_date; |
|
57
|
|
|
$this->end_date = $end_date; |
|
58
|
|
|
$this->valid_states = $valid_states; |
|
59
|
|
|
$this->included_set = $included; |
|
60
|
|
|
$this->excluded_set = $excluded; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param $unit |
|
65
|
|
|
* @param $reason |
|
66
|
|
|
*/ |
|
67
|
|
|
public function addMatch(UnitInterface $unit, $reason = '') { |
|
68
|
|
|
$this->included_set[$unit->getUnitId()] = array( |
|
69
|
|
|
'unit' => $unit, |
|
70
|
|
|
'reason' => $reason, |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param $unit |
|
76
|
|
|
* @param $reason |
|
77
|
|
|
*/ |
|
78
|
|
|
public function addMiss(UnitInterface $unit, $reason = '', Constraint $constraint = NULL) { |
|
79
|
|
|
$this->excluded_set[$unit->getUnitId()] = array( |
|
80
|
|
|
'unit' => $unit, |
|
81
|
|
|
'reason' => $reason, |
|
82
|
|
|
); |
|
83
|
|
|
|
|
84
|
|
|
if ($constraint !== NULL) { |
|
85
|
|
|
$this->excluded_set[$unit->getUnitId()]['constraint'] = $constraint; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return array |
|
91
|
|
|
*/ |
|
92
|
|
|
public function getIncluded() { |
|
93
|
|
|
return $this->included_set; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return array |
|
98
|
|
|
*/ |
|
99
|
|
|
public function getExcluded() { |
|
100
|
|
|
return $this->excluded_set; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return DateTime |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getStartDate() { |
|
107
|
|
|
return $this->start_date; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return DateTime |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getEndDate() { |
|
114
|
|
|
return $this->end_date; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param $unit |
|
119
|
|
|
* @param $reason |
|
120
|
|
|
* |
|
121
|
|
|
* @return bool |
|
122
|
|
|
*/ |
|
123
|
|
|
public function removeFromMatched(UnitInterface $unit, $reason = '', Constraint $constraint = NULL) { |
|
124
|
|
|
if (isset($this->included_set[$unit->getUnitId()])) { |
|
125
|
|
|
// Remove a unit from matched and add to the missed set |
|
126
|
|
|
unset($this->included_set[$unit->getUnitId()]); |
|
127
|
|
|
$this->addMiss($unit, $reason, $constraint); |
|
128
|
|
|
return TRUE; |
|
129
|
|
|
} else { |
|
130
|
|
|
return FALSE; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param $constraints |
|
136
|
|
|
*/ |
|
137
|
|
|
public function applyConstraints($constraints) { |
|
138
|
|
|
foreach ($constraints as $constraint) { |
|
139
|
|
|
$constraint->applyConstraint($this); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
} |
|
144
|
|
|
|