1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Andreshg112\HolidaysPhp\Providers\Colombia; |
4
|
|
|
|
5
|
|
|
use Andreshg112\HolidaysPhp\Holiday; |
6
|
|
|
use Andreshg112\HolidaysPhp\HolidaysPhpException; |
7
|
|
|
use Andreshg112\HolidaysPhp\Providers\BaseProvider; |
8
|
|
|
use Jenssegers\Date\Date; |
9
|
|
|
use Wa72\HtmlPageDom\HtmlPage; |
10
|
|
|
use Wa72\HtmlPageDom\HtmlPageCrawler; |
11
|
|
|
|
12
|
|
|
class PublicHolidaysCo extends BaseProvider |
13
|
|
|
{ |
14
|
|
|
protected function baseUrl(): string |
15
|
|
|
{ |
16
|
|
|
return 'https://publicholidays.co'; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function countryTranslations(): array |
20
|
|
|
{ |
21
|
|
|
return [ |
22
|
|
|
'en' => 'Colombia', |
23
|
|
|
'es' => 'Colombia', |
24
|
|
|
]; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function holidays(int $year = null): array |
28
|
|
|
{ |
29
|
|
|
$year = $year ?? date('Y'); |
30
|
|
|
|
31
|
|
|
$baseUrl = $this->baseUrl(); |
32
|
|
|
|
33
|
|
|
$url = $this->getLanguage() === 'en' ? "{$baseUrl}/{$year}-dates" : "{$baseUrl}/es/{$year}-dates"; |
34
|
|
|
|
35
|
|
|
try { |
36
|
|
|
$html = file_get_contents($url); |
37
|
|
|
} catch (\Throwable $th) { |
38
|
|
|
throw HolidaysPhpException::notFound(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$page = new HtmlPage($html); |
42
|
|
|
|
43
|
|
|
$rows = $page->filter('.publicholidays > tbody > tr'); |
44
|
|
|
|
45
|
|
|
if ($rows->count() === 0) { |
46
|
|
|
throw HolidaysPhpException::unrecognizedStructure(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$holidays = []; |
50
|
|
|
|
51
|
|
|
$country = $this->country(); |
52
|
|
|
|
53
|
|
|
foreach ($rows as $row) { |
54
|
|
|
$crawler = new HtmlPageCrawler($row); |
55
|
|
|
|
56
|
|
|
if ($crawler->children()->count() !== 3) { |
57
|
|
|
continue; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$spanishDate = trim($crawler->children()->getNode(0)->textContent); |
61
|
|
|
|
62
|
|
|
Date::setLocale($this->getLanguage()); |
63
|
|
|
|
64
|
|
|
$date = Date::createFromFormat('j F Y', "{$spanishDate} {$year}"); |
65
|
|
|
|
66
|
|
|
if (!$date) { |
67
|
|
|
throw HolidaysPhpException::unrecognizedDate(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$holiday = new Holiday( |
71
|
|
|
$country, |
72
|
|
|
$date, |
73
|
|
|
$crawler->children()->getNode(2)->textContent, // title, |
74
|
|
|
$this->getLanguage() |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
$holidays[] = $holiday; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $holidays; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|