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 CalendarioDeColombiaCom extends BaseProvider |
12
|
|
|
{ |
13
|
|
|
protected function baseUrl(): string |
14
|
|
|
{ |
15
|
|
|
return 'https://calendariodecolombia.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}/calendario-{$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
|
|
|
$rows = $page->filter( |
42
|
|
|
'#cuadro_festivos > div > .tabla_festivos1 > .formato_fechas, #cuadro_festivos > div > .tabla_festivos2 > .formato_fechas' |
43
|
|
|
); |
44
|
|
|
|
45
|
|
|
if ($rows->count() === 0) { |
46
|
|
|
throw HolidaysPhpException::unrecognizedStructure(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$holidays = []; |
50
|
|
|
|
51
|
|
|
$country = $this->country(); |
52
|
|
|
|
53
|
|
|
/** @var \DOMElement */ |
54
|
|
|
foreach ($rows as $row) { |
55
|
|
|
$nodeList = $row->childNodes; |
56
|
|
|
|
57
|
|
|
// It must contain this amount, else, it must be something different than a holiday |
58
|
|
|
if ($nodeList->count() !== 7) { |
59
|
|
|
continue; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** @var \DOMElement */ |
63
|
|
|
$timeElement = $nodeList->item(3); |
64
|
|
|
|
65
|
|
|
// The date is already in a field time |
66
|
|
|
$date = Date::parse($timeElement->getAttribute('datetime')); |
67
|
|
|
|
68
|
|
|
/** @var \DOMElement */ |
69
|
|
|
$aElement = $nodeList->item(2); |
70
|
|
|
|
71
|
|
|
$holidays[] = new Holiday( |
72
|
|
|
$country, |
73
|
|
|
$date->setTime(0, 0), |
74
|
|
|
trim($aElement->getAttribute('title')), // title |
75
|
|
|
$this->getLanguage() |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $holidays; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|