|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Timegridio\Concierge\Vacancy; |
|
4
|
|
|
|
|
5
|
|
|
class VacancyParser |
|
6
|
|
|
{ |
|
7
|
|
|
const REGEX_PATTERN_VACANCY = '/(?P<services>.*)\n\ (?P<days>.*)\n\ \ (?<hours>.*)/im'; |
|
8
|
|
|
|
|
9
|
|
|
///////////////////// |
|
10
|
|
|
// VACANCY PARSING // |
|
11
|
|
|
///////////////////// |
|
12
|
|
|
|
|
13
|
6 |
|
public function readVacancies($vacancyString) |
|
14
|
|
|
{ |
|
15
|
6 |
|
preg_match_all(self::REGEX_PATTERN_VACANCY, $vacancyString, $matches, PREG_SET_ORDER); |
|
16
|
|
|
|
|
17
|
6 |
|
return $matches; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
6 |
|
public function buildVacancies(array $vacancyParameters) |
|
21
|
|
|
{ |
|
22
|
6 |
|
$services = $this->services($vacancyParameters['services']); |
|
23
|
6 |
|
$days = $this->dates($vacancyParameters['days']); |
|
24
|
6 |
|
$hourRanges = $this->hours($vacancyParameters['hours']); |
|
25
|
|
|
|
|
26
|
6 |
|
$builtVacancies = []; |
|
27
|
6 |
|
foreach ($services as $service) { |
|
28
|
6 |
|
foreach ($days as $day) { |
|
29
|
6 |
|
foreach ($hourRanges as $hourRange) { |
|
30
|
|
|
$data = [ |
|
31
|
6 |
|
'service' => $service['slug'], |
|
32
|
6 |
|
'date' => $day, |
|
33
|
6 |
|
'startAt' => $hourRange['startAt'], |
|
34
|
6 |
|
'finishAt' => $hourRange['finishAt'], |
|
35
|
6 |
|
'capacity' => $service['capacity'], |
|
36
|
|
|
]; |
|
37
|
|
|
// Avoid repeated sets |
|
38
|
6 |
|
$key = md5(implode('.', $data)); |
|
39
|
6 |
|
$builtVacancies[$key] = $data; |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
6 |
|
return $builtVacancies; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
6 |
|
public function parseStatements($stringStatements) |
|
48
|
|
|
{ |
|
49
|
6 |
|
$vacancyStatements = $this->readVacancies($stringStatements); |
|
50
|
|
|
|
|
51
|
6 |
|
$builtVacancies = []; |
|
52
|
6 |
|
foreach ($vacancyStatements as $vacancyStatement) { |
|
|
|
|
|
|
53
|
6 |
|
$builtVacancies = array_merge($builtVacancies, $this->buildVacancies($vacancyStatement)); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
6 |
|
return $builtVacancies; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
///////////////////// |
|
60
|
|
|
// SERVICE PARSING // |
|
61
|
|
|
///////////////////// |
|
62
|
|
|
|
|
63
|
8 |
|
public function services($services) |
|
64
|
|
|
{ |
|
65
|
8 |
|
$services = $this->splitServices($services); |
|
66
|
|
|
|
|
67
|
8 |
|
return $this->getServicesCapacity($services); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
9 |
|
public function splitServices($services) |
|
71
|
|
|
{ |
|
72
|
9 |
|
return preg_split('/\ *\,\ */', $services); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
8 |
|
public function getServicesCapacity(array $services) |
|
76
|
|
|
{ |
|
77
|
8 |
|
$converted = []; |
|
78
|
8 |
|
foreach ($services as $service) { |
|
79
|
8 |
|
$converted[] = $this->getServiceCapacity($service); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
8 |
|
return $converted; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
8 |
|
public function getServiceCapacity($service) |
|
86
|
|
|
{ |
|
87
|
8 |
|
$capacity = 1; |
|
88
|
8 |
|
if (strpos($service, ':') !== false) { |
|
89
|
8 |
|
list($service, $capacity) = explode(':', $service); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
8 |
|
return ['slug' => $service, 'capacity' => trim($capacity)]; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
////////////////// |
|
96
|
|
|
// DATE PARSING // |
|
97
|
|
|
////////////////// |
|
98
|
|
|
|
|
99
|
8 |
|
public function dates($days) |
|
100
|
|
|
{ |
|
101
|
8 |
|
$days = $this->splitDates($days); |
|
102
|
|
|
|
|
103
|
8 |
|
return $this->convertDaysToDate($days); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
9 |
|
public function splitDates($days) |
|
107
|
|
|
{ |
|
108
|
9 |
|
return preg_split('/\ *\,\ */', $days); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
8 |
|
public function convertDaysToDate(array $days) |
|
112
|
|
|
{ |
|
113
|
8 |
|
$converted = []; |
|
114
|
8 |
|
foreach ($days as $day) { |
|
115
|
8 |
|
$converted[] = $this->dayToDate($day); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
8 |
|
return $converted; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
8 |
|
public function dayToDate($day) |
|
122
|
|
|
{ |
|
123
|
8 |
|
return date('Y-m-d', strtotime($day)); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
////////////////// |
|
127
|
|
|
// TIME PARSING // |
|
128
|
|
|
////////////////// |
|
129
|
|
|
|
|
130
|
8 |
|
public function hours($string) |
|
131
|
|
|
{ |
|
132
|
8 |
|
$ranges = $this->splitRanges($string); |
|
133
|
|
|
|
|
134
|
8 |
|
return $this->normalizeRanges($ranges); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
8 |
|
public function normalizeRanges($ranges) |
|
138
|
|
|
{ |
|
139
|
8 |
|
$normalizedRanges = []; |
|
140
|
|
|
|
|
141
|
8 |
|
foreach ($ranges as $range) { |
|
142
|
8 |
|
list($startAt, $finishAt) = preg_split('/\ *\-\ */', $range); |
|
143
|
8 |
|
$normalizedRanges[] = [ |
|
144
|
8 |
|
'startAt' => $this->milTimeToStandard($startAt), |
|
145
|
8 |
|
'finishAt' => $this->milTimeToStandard($finishAt), |
|
146
|
|
|
]; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
8 |
|
return $normalizedRanges; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
8 |
|
public function splitRanges($string) |
|
153
|
|
|
{ |
|
154
|
8 |
|
return preg_split('/,\ */', $string); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
11 |
|
public function milTimeToStandard($militaryTime) |
|
158
|
|
|
{ |
|
159
|
11 |
|
$militaryTime = trim($militaryTime); |
|
160
|
|
|
|
|
161
|
11 |
|
if ($militaryTime == '') { |
|
162
|
1 |
|
return ''; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
10 |
|
if (strpos($militaryTime, ':') !== false) { |
|
166
|
2 |
|
return $militaryTime; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
8 |
|
$parts = []; |
|
170
|
|
|
|
|
171
|
8 |
|
if (strlen($militaryTime) <= 2) { |
|
172
|
7 |
|
$parts[] = intval($militaryTime); |
|
173
|
7 |
|
$parts[] = '00'; |
|
174
|
|
|
} |
|
175
|
8 |
View Code Duplication |
if (strlen($militaryTime) == 3) { |
|
|
|
|
|
|
176
|
3 |
|
$parts[0] = substr($militaryTime, 0, 1); |
|
177
|
3 |
|
$parts[1] = substr($militaryTime, 1, 2); |
|
178
|
|
|
} |
|
179
|
8 |
View Code Duplication |
if (strlen($militaryTime) == 4) { |
|
|
|
|
|
|
180
|
2 |
|
$parts[0] = substr($militaryTime, 0, 2); |
|
181
|
2 |
|
$parts[1] = substr($militaryTime, 2, 2); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
8 |
|
return implode(':', $parts); |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.