|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Holiday Library. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Michał Mańko <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Michalmanko\Holiday; |
|
13
|
|
|
|
|
14
|
|
|
use DateTimeZone; |
|
15
|
|
|
use Michalmanko\Holiday\Exception\InvalidArgumentException; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Holiday providers factory. |
|
19
|
|
|
* |
|
20
|
|
|
* Declared abstract, as we have no need for instantiation. |
|
21
|
|
|
* |
|
22
|
|
|
* @author Michał Mańko <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
abstract class HolidayFactory |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Registered providers. |
|
28
|
|
|
* |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
protected static $providers = array( |
|
32
|
|
|
'PL' => 'Poland', |
|
33
|
|
|
'DK' => 'Denmark', |
|
34
|
|
|
); |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Returns array with registered providers. |
|
38
|
|
|
* |
|
39
|
|
|
* @return array |
|
40
|
|
|
*/ |
|
41
|
4 |
|
public static function getProviders() |
|
42
|
|
|
{ |
|
43
|
4 |
|
return static::$providers; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Registers a holidays provider class. |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $countryCode Country code |
|
50
|
|
|
* @param string $providerClassName Provider class name |
|
51
|
|
|
*/ |
|
52
|
7 |
|
public static function registerProvider($countryCode, $providerClassName) |
|
53
|
|
|
{ |
|
54
|
7 |
|
$countryCode = strtoupper($countryCode); |
|
55
|
7 |
|
$providerClassName = (string) $providerClassName; |
|
56
|
|
|
|
|
57
|
7 |
|
static::$providers[$countryCode] = $providerClassName; |
|
58
|
7 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Unregisters a holidays provider class. |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $providerClassName Country code of the provider or the provider class name |
|
64
|
|
|
* |
|
65
|
|
|
* @return bool |
|
66
|
|
|
*/ |
|
67
|
4 |
|
public static function unregisterProvider($providerClassName) |
|
68
|
|
|
{ |
|
69
|
4 |
|
if (array_key_exists(strtoupper($providerClassName), static::$providers)) { |
|
70
|
2 |
|
unset(static::$providers[strtoupper($providerClassName)]); |
|
71
|
|
|
|
|
72
|
2 |
|
return true; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
2 |
|
$index = array_search((string) $providerClassName, static::$providers, true); |
|
76
|
2 |
|
if ($index !== false) { |
|
77
|
1 |
|
unset(static::$providers[$index]); |
|
78
|
|
|
|
|
79
|
1 |
|
return true; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
1 |
|
return false; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Creates a holidays provider based on $countryCode code. |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $countryCode ISO Country Code, County Name or provider class name |
|
89
|
|
|
* @param null|DateTimeZone $timezone (optional) Time zone |
|
90
|
|
|
* |
|
91
|
|
|
* @throws InvalidArgumentException |
|
92
|
|
|
* |
|
93
|
|
|
* @return Provider\AbstractProvider |
|
94
|
|
|
*/ |
|
95
|
36 |
|
public static function createProvider($countryCode, DateTimeZone $timezone = null) |
|
96
|
|
|
{ |
|
97
|
|
|
// Use the supplied provider class |
|
98
|
36 |
|
if (substr($countryCode, 0, 1) == '\\') { |
|
99
|
3 |
|
$className = $countryCode; |
|
100
|
36 |
|
} elseif (array_key_exists(strtoupper($countryCode), static::$providers)) { |
|
101
|
32 |
|
$countryCode = strtoupper($countryCode); |
|
102
|
32 |
|
if (substr(static::$providers[$countryCode], 0, 1) == '\\') { |
|
103
|
3 |
|
$className = static::$providers[$countryCode]; |
|
104
|
3 |
|
} else { |
|
105
|
29 |
|
$className = '\\' . __NAMESPACE__ . '\\Provider\\' |
|
106
|
29 |
|
. static::$providers[$countryCode]; |
|
107
|
|
|
} |
|
108
|
32 |
|
} else { |
|
109
|
3 |
|
$className = '\\' . __NAMESPACE__ . '\\Provider\\' . $countryCode; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
36 |
|
if (!class_exists($className)) { |
|
113
|
4 |
|
throw new InvalidArgumentException(sprintf( |
|
114
|
4 |
|
'Cannot find Holiday provider class "%s"', |
|
115
|
|
|
$className |
|
116
|
4 |
|
)); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
32 |
|
$provider = new $className($timezone); |
|
120
|
|
|
|
|
121
|
32 |
|
if (!$provider instanceof Provider\AbstractProvider) { |
|
122
|
1 |
|
throw new InvalidArgumentException(sprintf( |
|
123
|
|
|
'Class "%s" must be an instance of ' |
|
124
|
1 |
|
. '\\Michalmanko\\Holiday\\Provider\\AbstractProvider', |
|
125
|
|
|
$className |
|
126
|
1 |
|
)); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
31 |
|
return $provider; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|