-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pharmacy.class.php
executable file
·330 lines (289 loc) · 9.46 KB
/
Pharmacy.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
/**
* @author: Ufuk OZDEMIR
* @creatAt: 07.04.2024 19:46
* @web: https://www.ufukozdemir.website/
* @email: ufuk.ozdemir1990@gmail.com
* @phone: +90 (532) 131 73 07
*/
class Pharmacy
{
/**
* City
* @var string
*/
private $city;
/**
* District
* @var string
*/
private $district;
/**
* JSON Data
* @var array
*/
private $jsonData = [];
/**
* Return Data
* @var array
*/
private $returnData = [];
/**
* Pharmacy constructor.
* @param $city
* @param $district
*/
public function __construct($city, $district)
{
$this->city = $city;
$this->district = $district;
}
/**
* Explode Data
* @return void
*/
private function getExplodeData()
{
$curlData = $this->fetchDataWithCurl('https://www.eczaneler.gen.tr/nobetci-' . $this->getConvert($this->city) . '-' . $this->getConvert($this->district));
if ($curlData) {
preg_match_all('@<table class="table table-striped mt-2" style="font-size:14px;">(.*?)</table>@si', $curlData, $currentData);
preg_match_all('@<div class="d-flex alert alert-warning text-center rounded shadow-sm">(.*?)</div>@si', $currentData[1][1] ?? '', $date);
preg_match_all('@<span class="isim">(.*?)</span>@si', $currentData[1][1] ?? '', $name);
preg_match_all("@<div class='col-lg-6'>(.*?)<@si", $currentData[1][1] ?? '', $address);
preg_match_all("@<div class='col-lg-3 py-lg-2'>(.*?)</div>@si", $currentData[1][1] ?? '', $phone);
preg_match_all('@<div class="mt-4 text-center">(.*?)<br>@si', $curlData ?? '', $totalPharmacy);
$total = count($name[1]);
if ($total > 0) {
$this->jsonData = [
'createdAt' => time(),
'city' => $this->city,
'district' => $this->district,
'descriptionDate' => trim(strip_tags($date[1][0] ?? '')),
'totalPharmacy' => trim($totalPharmacy[1][0] ?? ''),
'pharmacyList' => []
];
for ($i = 0; $i < $total; $i++) {
$pharmacyName = $this->ucwordsTR(trim(strip_tags(html_entity_decode($name[1][$i]) ?? '')));
$this->jsonData['pharmacyList'][] = [
'name' => $pharmacyName,
'address' => trim(strip_tags(html_entity_decode($this->ucwordsTR($address[1][$i] ?? '')))),
'phone' => trim(strip_tags(html_entity_decode($this->formatPhoneNumber($phone[1][$i] ?? '')))),
'maps' => $pharmacyName ? 'https://maps.google.com/maps?q=' . urlencode($pharmacyName) : ''
];
}
$this->successDataResult($this->jsonData);
} else {
$this->errorDataResult($this->jsonData, 'Nöbetçi Eczane Bulunamadı!');
}
} else {
$this->errorDataResult($this->jsonData, 'Nöbetçi Eczane Sistemine Erişim Sağlanamadı!');
}
}
/**
* Get Pharmacies
* @param int $time
* @return array
*/
public function getPharmacies($time = 60)
{
$cacheName = $this->getCacheName();
$cacheAge = $this->getCacheAge($time);
if (!file_exists($cacheName) || (time() - filemtime($cacheName)) > $cacheAge) {
$this->getExplodeData();
file_put_contents($cacheName, json_encode($this->returnData));
} else {
$cachedData = json_decode(file_get_contents($cacheName), true);
$city = $cachedData['data']['city'] ?? null;
$district = $cachedData['data']['district'] ?? null;
if ($city !== $this->city || $district !== $this->district) {
$this->getExplodeData();
file_put_contents($cacheName, json_encode($this->returnData));
} else {
$this->returnData = $cachedData;
}
}
return $this->returnData;
}
/**
* Caching Name
* @return string
*/
private function getCacheName()
{
return 'pharmacy.json';
}
/**
* Caching Age
* @param $time
* @return mixed
*/
private function getCacheAge($time)
{
return $time * 60;
}
/**
* Success Data Result
* @param $data
* @param string $message
*/
private function successDataResult($data, $message = '')
{
$this->returnData = [
'status' => 'success',
'message' => $message,
'data' => $data
];
}
/**
* Error Data Result
* @param $data
* @param string $message
*/
private function errorDataResult($data, $message = '')
{
$this->returnData = [
'status' => 'error',
'message' => $message,
'data' => $data
];
}
/**
* String Convert
* @param $string
* @return string
*/
private function getConvert($string)
{
$tr = array('ş', 'Ş', 'ı', 'İ', 'ğ', 'Ğ', 'ü', 'Ü', 'ö', 'Ö', 'Ç', 'ç');
$en = array('s', 's', 'i', 'i', 'g', 'g', 'u', 'u', 'o', 'o', 'c', 'c');
$string = str_replace($tr, $en, $string);
$string = strtolower($string);
$string = preg_replace('/&.+?;/', '', $string);
$string = preg_replace('/[^%a-z0-9 _-]/', '', $string);
$string = preg_replace('/\s+/', '-', $string);
$string = preg_replace('|-+|', '-', $string);
return trim($string, '-');
}
/**
* Turkish ucwords
* @param string $string
* @return string
*/
private function ucwordsTR($string)
{
$words = explode(" ", $string);
$result = array_map(function ($word) {
$parts = explode('/', $word);
$transformedParts = array_map(function ($part) {
$first_character = mb_substr($part, 0, 1, 'UTF-8');
$rest_of_word = mb_substr($part, 1, null, 'UTF-8');
$first_character = $this->upperCaseTR($first_character);
$rest_of_word = $this->createLower($rest_of_word);
return $first_character . $rest_of_word;
}, $parts);
return implode('/', $transformedParts);
}, $words);
$resultString = implode(' ', $result);
if (strpos($resultString, ' / ') !== false) {
$resultString = str_replace(' / ', ' - ', $resultString);
}
$resultString = $this->abbreviateAddressTerms($resultString);
$resultString = preg_replace_callback('/(\d\/\d-\w)/u', function($matches) {
return mb_strtoupper($matches[0], 'UTF-8');
}, $resultString);
return $resultString;
}
/**
* Turkish Character Uppercase
* @param string $char
* @return string
*/
private function upperCaseTR($char)
{
$tr = ['ç', 'ğ', 'ı', 'i', 'ö', 'ş', 'ü'];
$tr_upper = ['Ç', 'Ğ', 'I', 'İ', 'Ö', 'Ş', 'Ü'];
$index = array_search(mb_strtolower($char, 'UTF-8'), $tr);
if ($index !== false) {
return $tr_upper[$index];
}
return mb_strtoupper($char, 'UTF-8');
}
/**
* Turkish Character Lowercase
* @param string $string
* @return string
*/
private function createLower($string)
{
return mb_strtolower($string, 'UTF-8');
}
/**
* Address Terms Abbreviation
* @param string $string
* @return string
*/
private function abbreviateAddressTerms($string)
{
$abbreviations = [
'Mahallesi' => 'Mh.',
'Bulvarı' => 'Blv.',
'Sokak' => 'Sk.',
'Sokağı' => 'Sk.',
'Caddesi' => 'Cd.',
'Meydan' => 'Meyd.',
'Apartman' => 'Apt.',
'Daire' => 'D.',
'Kat' => 'Kt.',
'Numara' => 'No',
'Sitesi' => 'Sit.',
'İş Merkezi' => 'İş Mrk.',
'Plaza' => 'Plz.',
'Lojman' => 'Loj.',
'Köy' => 'Köy',
'Bina' => 'Bina No',
'Yolu' => 'Yolu',
'Sanayi' => 'San.',
'Organize Sanayi Bölgesi' => 'OSB',
'Bölge' => 'Blg.',
'İstasyonu' => 'İst.'
];
foreach ($abbreviations as $fullTerm => $abbreviation) {
$string = str_replace($fullTerm, $abbreviation, $string);
}
return $string;
}
/**
* Format Phone Number
* @param $input
* @return string
*/
private function formatPhoneNumber($input) {
$pattern = '/0 \(\d{3}\) \d{3}-\d{2}-\d{2}/';
preg_match_all($pattern, $input, $matches);
$number = $matches[0][0] ?? '';
if (!empty($number)) {
$number = str_replace('-', ' ', $number);
}
return $number;
}
/**
* Data Extraction with Curl
* @param $link
* @return bool|string
*/
private function fetchDataWithCurl($link)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $link);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$source = curl_exec($ch);
if (curl_errno($ch)) {
error_log('CURL error (' . curl_errno($ch) . '): ' . curl_error($ch));
curl_close($ch);
return null;
}
curl_close($ch);
return $source;
}
}