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
|
|
|
|
11
|
|
|
class CalendarioHispanohablanteCom extends BaseProvider |
12
|
|
|
{ |
13
|
|
|
protected function baseUrl(): string |
14
|
|
|
{ |
15
|
|
|
return 'https://calendariohispanohablante.com'; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function countryTranslations(): array |
19
|
|
|
{ |
20
|
|
|
return [ |
21
|
|
|
'es' => 'Colombia', |
22
|
|
|
]; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function holidays(int $year = null): ?array |
26
|
|
|
{ |
27
|
|
|
$year = $year ?? date('Y'); |
28
|
|
|
|
29
|
|
|
$baseUrl = $this->baseUrl(); |
30
|
|
|
|
31
|
|
|
$url = "{$baseUrl}/{$year}/calendario-colombia-{$year}.html"; |
32
|
|
|
|
33
|
|
|
try { |
34
|
|
|
$html = file_get_contents($url); |
35
|
|
|
} catch (\Throwable $th) { |
36
|
|
|
throw HolidaysPhpException::notFound(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$page = new HtmlPage($html); |
40
|
|
|
|
41
|
|
|
$pList = $page->filter('.group-three > p'); |
42
|
|
|
|
43
|
|
|
if ($pList->count() === 0) { |
44
|
|
|
throw HolidaysPhpException::unrecognizedStructure(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$holidays = []; |
48
|
|
|
|
49
|
|
|
$country = $this->country(); |
50
|
|
|
|
51
|
|
|
/** @var \DOMElement */ |
52
|
|
|
foreach ($pList as $pItem) { |
53
|
|
|
$pChildren = $pItem->childNodes; |
54
|
|
|
|
55
|
|
|
// It must contain 2 nodes, else, it must be something different than a date |
56
|
|
|
if ($pChildren->count() !== 2) { |
57
|
|
|
continue; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// The text is like "Viernes 1 de Enero", so it has to be separated |
61
|
|
|
[$day, $spanishDate] = explode(' ', trim($pChildren->item(0)->textContent), 2); |
62
|
|
|
|
63
|
|
|
// The ending : has to be removed |
64
|
|
|
$spanishDate = rtrim($spanishDate, ':'); |
65
|
|
|
|
66
|
|
|
Date::setLocale($this->getLanguage()); |
67
|
|
|
|
68
|
|
|
// The date looks this way "1 de Enero 2021" |
69
|
|
|
$date = Date::createFromFormat('j \d\e F Y', "{$spanishDate} {$year}"); |
70
|
|
|
|
71
|
|
|
$holidays[] = new Holiday( |
72
|
|
|
$country, |
73
|
|
|
$date->setTime(0, 0), |
|
|
|
|
74
|
|
|
trim($pChildren->item(1)->textContent), // title |
75
|
|
|
$this->getLanguage() |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $holidays; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|