1 | <?php |
||
9 | class DayAttendance |
||
10 | { |
||
11 | /** |
||
12 | * example: 2015-12-12|08:30 (10:00-10:30) (12:30-13:30) (16:00-16:30) 17:30 |
||
13 | */ |
||
14 | const DAY_ATTENDANCE_LINE_REGEX = |
||
15 | '/^\d{4}-\d{2}-\d{2}\|\d{2}:\d{2}( \(\d{2}:\d{2}-\d{2}:\d{2}\))* \d{2}:\d{2}(\|[^\|]*)?$/'; |
||
16 | |||
17 | /** |
||
18 | * @var \DateTime |
||
19 | */ |
||
20 | private $arrival; |
||
21 | |||
22 | /** |
||
23 | * @var \DateTime |
||
24 | */ |
||
25 | private $departure; |
||
26 | |||
27 | /** |
||
28 | * @var Pause[] |
||
29 | */ |
||
30 | private $pauseList = []; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | private $description = ""; |
||
36 | |||
37 | /** |
||
38 | * DayAttendance constructor. |
||
39 | * @param \DateTime $arrival |
||
40 | * @param \DateTime $departure |
||
41 | * @param Pause[] $pauseList |
||
42 | */ |
||
43 | 33 | public function __construct(\DateTime $arrival, \DateTime $departure, array $pauseList = []) |
|
44 | { |
||
45 | 33 | if ($arrival > $departure or $arrival->format('Y-m-d') !== $departure->format('Y-m-d')) { |
|
|
|||
46 | 6 | throw new \InvalidArgumentException; |
|
47 | } |
||
48 | |||
49 | 27 | $this->arrival = $arrival; |
|
50 | 27 | $this->departure = $departure; |
|
51 | |||
52 | 27 | foreach ($pauseList as $pause) { |
|
53 | 24 | $this->addPause($pause); |
|
54 | 24 | } |
|
55 | 18 | } |
|
56 | |||
57 | /** |
||
58 | * @return \DateTime |
||
59 | */ |
||
60 | 48 | public function getArrival() |
|
64 | |||
65 | /** |
||
66 | * @return \DateTime |
||
67 | */ |
||
68 | 42 | public function getDeparture() |
|
72 | |||
73 | /** |
||
74 | * @return Pause[] |
||
75 | */ |
||
76 | 42 | public function getPauseList() |
|
80 | |||
81 | /** |
||
82 | * @return string |
||
83 | */ |
||
84 | 18 | public function getDescription() |
|
88 | |||
89 | /** |
||
90 | * @param string $description |
||
91 | */ |
||
92 | 18 | public function setDescription($description) |
|
96 | |||
97 | /** |
||
98 | * @param Pause $pause |
||
99 | */ |
||
100 | 24 | private function addPause(Pause $pause) |
|
117 | |||
118 | /** |
||
119 | * @param Pause $pause |
||
120 | * @return bool |
||
121 | */ |
||
122 | 21 | private function isPauseOverlapping(Pause $pause) |
|
137 | |||
138 | /** |
||
139 | * @return \DateInterval |
||
140 | */ |
||
141 | 9 | public function getDuration() |
|
156 | |||
157 | /** |
||
158 | * @return int |
||
159 | */ |
||
160 | 6 | public function getTotalMinutes() |
|
165 | |||
166 | /** |
||
167 | * @param $dayAttendanceLine |
||
168 | * @return DayAttendance |
||
169 | */ |
||
170 | 18 | public static function parseDayAttendanceLine($dayAttendanceLine) |
|
189 | |||
190 | /** |
||
191 | * @param string $dayAttendanceLine |
||
192 | * @return array |
||
193 | */ |
||
194 | 15 | private static function explodeDayAttendanceLineParts($dayAttendanceLine) |
|
205 | |||
206 | /** |
||
207 | * @param string $date date in Y-m-d format, ex: 2016-03-29 |
||
208 | * @param array $pauseBlocks |
||
209 | * @return Pause[] |
||
210 | */ |
||
211 | 15 | private static function parsePauseBlocks($date, $pauseBlocks) |
|
224 | |||
225 | /** |
||
226 | * @param string $date date in Y-m-d format, ex: 2016-03-29 |
||
227 | * @param string $pauseBlock ex: (10:00-10:30) |
||
228 | * @return Pause |
||
229 | */ |
||
230 | 12 | private static function parsePauseBlock($date, $pauseBlock) |
|
236 | |||
237 | /** |
||
238 | * @param $dayAttendanceLine |
||
239 | * @return int |
||
240 | */ |
||
241 | 45 | public static function isValidDayAttendanceLine($dayAttendanceLine) |
|
245 | |||
246 | /** |
||
247 | * @return string |
||
248 | */ |
||
249 | 6 | public function exportLine() |
|
259 | |||
260 | /** |
||
261 | * @return string |
||
262 | */ |
||
263 | 9 | public function getDate() |
|
267 | |||
268 | /** |
||
269 | * @return string |
||
270 | */ |
||
271 | 9 | public function getTimeLine() |
|
283 | } |
||
284 |
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.