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 CalendarioHispanohablanteCom extends BaseProvider |
13
|
|
|
{ |
14
|
2 |
|
protected function baseUrl(): string |
15
|
|
|
{ |
16
|
2 |
|
return 'https://calendariohispanohablante.com'; |
17
|
|
|
} |
18
|
|
|
|
19
|
3 |
|
public function countryTranslations(): array |
20
|
|
|
{ |
21
|
|
|
return [ |
22
|
3 |
|
'es' => 'Colombia', |
23
|
|
|
]; |
24
|
|
|
} |
25
|
|
|
|
26
|
2 |
|
public function holidays(int $year = null): array |
27
|
|
|
{ |
28
|
2 |
|
$year = $year ?? date('Y'); |
29
|
|
|
|
30
|
2 |
|
$baseUrl = $this->baseUrl(); |
31
|
|
|
|
32
|
2 |
|
$url = "{$baseUrl}/{$year}/calendario-colombia-{$year}.html"; |
33
|
|
|
|
34
|
|
|
try { |
35
|
2 |
|
$html = file_get_contents($url); |
36
|
1 |
|
} catch (\Throwable $th) { |
37
|
1 |
|
throw HolidaysPhpException::notFound(); |
38
|
|
|
} |
39
|
|
|
|
40
|
1 |
|
$page = new HtmlPage($html); |
41
|
|
|
|
42
|
1 |
|
$rows = $page->filter('.group-three > p'); |
43
|
|
|
|
44
|
1 |
|
if ($rows->count() === 0) { |
45
|
|
|
throw HolidaysPhpException::unrecognizedStructure(); |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
$holidays = []; |
49
|
|
|
|
50
|
1 |
|
$country = $this->country(); |
51
|
|
|
|
52
|
1 |
|
foreach ($rows as $row) { |
53
|
1 |
|
$crawler = new HtmlPageCrawler($row); |
54
|
|
|
|
55
|
1 |
|
$dateContainer = $crawler->filter('b')->first(); |
56
|
|
|
|
57
|
|
|
// This element must exist |
58
|
1 |
|
if ($dateContainer->count() === 0) { |
59
|
|
|
continue; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// The text is like "Viernes 1 de Enero", so it has to be separated |
63
|
1 |
|
[, $spanishDate] = explode(' ', $dateContainer->text(), 2); |
64
|
|
|
|
65
|
|
|
// The ending : has to be removed |
66
|
1 |
|
$spanishDate = rtrim($spanishDate, ':'); |
67
|
|
|
|
68
|
1 |
|
Date::setLocale($this->getLanguage()); |
69
|
|
|
|
70
|
|
|
// The date looks this way "1 de Enero 2021" |
71
|
1 |
|
$date = Date::createFromFormat('j \d\e F Y', "{$spanishDate} {$year}"); |
72
|
|
|
|
73
|
1 |
|
if (!$date) { |
74
|
|
|
throw HolidaysPhpException::unrecognizedDate(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// Remove it so the text inside the $crawler is only the title of the holiday |
78
|
1 |
|
$dateContainer->remove(); |
79
|
|
|
|
80
|
1 |
|
$titleContainer = $crawler->first(); |
81
|
|
|
|
82
|
1 |
|
$holidays[] = new Holiday( |
83
|
1 |
|
$country, |
84
|
1 |
|
$date, |
85
|
1 |
|
$titleContainer->text(), |
86
|
1 |
|
$this->getLanguage() |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
return $holidays; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|