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 CalendarioDeColombiaCom extends BaseProvider |
13
|
|
|
{ |
14
|
1 |
|
protected function baseUrl(): string |
15
|
|
|
{ |
16
|
1 |
|
return 'https://calendariodecolombia.com'; |
17
|
|
|
} |
18
|
|
|
|
19
|
1 |
|
public function countryTranslations(): array |
20
|
|
|
{ |
21
|
|
|
return [ |
22
|
1 |
|
'es' => 'Colombia', |
23
|
|
|
]; |
24
|
|
|
} |
25
|
|
|
|
26
|
1 |
|
public function holidays(int $year = null): array |
27
|
|
|
{ |
28
|
1 |
|
$year = $year ?? date('Y'); |
29
|
|
|
|
30
|
1 |
|
$baseUrl = $this->baseUrl(); |
31
|
|
|
|
32
|
1 |
|
$url = "{$baseUrl}/calendario-{$year}.html"; |
33
|
|
|
|
34
|
|
|
try { |
35
|
1 |
|
$html = file_get_contents($url); |
36
|
|
|
} catch (\Throwable $th) { |
37
|
|
|
throw HolidaysPhpException::notFound(); |
38
|
|
|
} |
39
|
|
|
|
40
|
1 |
|
$page = new HtmlPage($html); |
41
|
|
|
|
42
|
1 |
|
$rows = $page->filter( |
43
|
1 |
|
'#cuadro_festivos > div > .tabla_festivos1 > .formato_fechas, #cuadro_festivos > div > .tabla_festivos2 > .formato_fechas' |
44
|
|
|
); |
45
|
|
|
|
46
|
1 |
|
if ($rows->count() === 0) { |
47
|
|
|
throw HolidaysPhpException::unrecognizedStructure(); |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
$holidays = []; |
51
|
|
|
|
52
|
1 |
|
$country = $this->country(); |
53
|
|
|
|
54
|
1 |
|
foreach ($rows as $row) { |
55
|
1 |
|
$crawler = new HtmlPageCrawler($row); |
56
|
|
|
|
57
|
1 |
|
$dateContainer = $crawler->filter('time')->first(); |
58
|
|
|
|
59
|
|
|
// This element must exist |
60
|
1 |
|
if ($dateContainer->count() === 0) { |
61
|
|
|
continue; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// The date is already in a field time |
65
|
1 |
|
$date = Date::parse($dateContainer->getAttribute('datetime')); |
66
|
|
|
|
67
|
1 |
|
$titleContainer = $crawler->filter('a')->first(); |
68
|
|
|
|
69
|
1 |
|
$holidays[] = new Holiday( |
70
|
1 |
|
$country, |
71
|
1 |
|
$date->setTime(0, 0), |
72
|
1 |
|
$titleContainer->getAttribute('title'), // title |
|
|
|
|
73
|
1 |
|
$this->getLanguage() |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
return $holidays; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|