1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Andreshg112\HolidaysPhp\Providers; |
4
|
|
|
|
5
|
|
|
use Andreshg112\HolidaysPhp\HolidaysPhpException; |
6
|
|
|
|
7
|
|
|
abstract class BaseProvider |
8
|
|
|
{ |
9
|
|
|
protected $language; |
10
|
|
|
|
11
|
4 |
|
public function __construct(string $language) |
12
|
|
|
{ |
13
|
4 |
|
if (!in_array($language, $this->supportedLanguages())) { |
14
|
1 |
|
throw new HolidaysPhpException( |
15
|
1 |
|
"The language code {$language} is not supported by the provider " . static::class |
16
|
|
|
); |
17
|
|
|
} |
18
|
|
|
|
19
|
3 |
|
$this->language = $language; |
20
|
3 |
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Returns the base URL of the provider. |
24
|
|
|
* |
25
|
|
|
* @return string |
26
|
|
|
*/ |
27
|
|
|
abstract protected function baseUrl(): string; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Returns the name of the country according to the language specified in the constructor. |
31
|
|
|
* |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
2 |
|
public function country(): string |
35
|
|
|
{ |
36
|
2 |
|
return $this->countryTranslations()[$this->language]; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Returns a list of the translations of the country name. |
41
|
|
|
* It must have an structure like this: `[ ['en' => 'United States', 'es' => 'Estados Unidos'] ]`. |
42
|
|
|
* Every language code, the key of each array, will be a supported language. |
43
|
|
|
* |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
|
|
abstract public function countryTranslations(): array; |
47
|
|
|
|
48
|
2 |
|
public function getLanguage(): string |
49
|
|
|
{ |
50
|
2 |
|
return $this->language; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Returns a list of holidays based on the year and language specified in the constructor. |
55
|
|
|
* |
56
|
|
|
* @param int $year Must be supported by the provider. By default, the current year. |
57
|
|
|
* @return array |
58
|
|
|
* @throws \Andreshg112\HolidaysPhp\HolidaysPhpException |
59
|
|
|
*/ |
60
|
|
|
abstract public function holidays(int $year = null): array; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Returns a list of the languages supported by the provider specified in `countryTranslations()`. |
64
|
|
|
* |
65
|
|
|
* |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
4 |
|
public function supportedLanguages(): array |
69
|
|
|
{ |
70
|
4 |
|
return array_keys($this->countryTranslations()); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|