1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ChinLeung\PhpWeekday; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
class PhpWeekday |
8
|
|
|
{ |
9
|
|
|
use HasVerboseMethods; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* The locale of the application. |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $locale; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The name of the weekday. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $name; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The value of the weekday. |
27
|
|
|
* |
28
|
|
|
* @var int |
29
|
|
|
*/ |
30
|
|
|
protected $value; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Constructor of the class. |
34
|
|
|
* |
35
|
|
|
* @param string|int $value |
36
|
|
|
* @param string $locale |
37
|
|
|
*/ |
38
|
|
|
public function __construct($value, string $locale) |
39
|
|
|
{ |
40
|
|
|
$this->setLocale($locale) |
41
|
|
|
->set($value); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Retrieve the current locale. |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
public function getLocale() : string |
50
|
|
|
{ |
51
|
|
|
return $this->locale; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Retrieve the supported locales. |
56
|
|
|
* |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
|
|
public static function getLocales() : array |
60
|
|
|
{ |
61
|
|
|
return array_map( |
62
|
|
|
'basename', |
63
|
|
|
glob(__DIR__.'/../resources/lang/*') ?: [] |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Retrieve the name of the weekday. |
69
|
|
|
* |
70
|
|
|
* @param string $locale |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
public function getName(string $locale = null) : string |
74
|
|
|
{ |
75
|
|
|
if (! is_null($locale) && $locale != $this->locale) { |
76
|
|
|
return $this->parseName($this->getValue(), $locale); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if (is_null($this->name)) { |
80
|
|
|
$this->name = $this->parseName($this->getValue()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $this->name; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Retrieve the name from a value. |
88
|
|
|
* |
89
|
|
|
* @param int $value |
90
|
|
|
* @param string $locale |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
public static function getNameFromValue(int $value, string $locale) : string |
94
|
|
|
{ |
95
|
|
|
return static::parse($value, $locale)->getName(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Retrieve the names of every weekday for a locale. |
100
|
|
|
* |
101
|
|
|
* @param string $locale |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
|
|
public static function getNames(string $locale) : array |
105
|
|
|
{ |
106
|
|
|
$path = __DIR__."/../resources/lang/$locale/names.php"; |
107
|
|
|
|
108
|
|
|
if (! file_exists($path)) { |
109
|
|
|
static::throwNotSupportedLocaleException($locale); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return require $path; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Retrieve the value of the weekday. |
117
|
|
|
* |
118
|
|
|
* @return int |
119
|
|
|
*/ |
120
|
|
|
public function getValue() : int |
121
|
|
|
{ |
122
|
|
|
if (is_null($this->value)) { |
123
|
|
|
$this->value = $this->parseValue($this->getName()); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $this->value; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Retrieve the value from a name. |
131
|
|
|
* |
132
|
|
|
* @param string $name |
133
|
|
|
* @param string $locale |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
|
|
public static function getValueFromName(string $name, string $locale) : string |
137
|
|
|
{ |
138
|
|
|
return static::parse($name, $locale)->getValue(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Check if the locale is supported. |
143
|
|
|
* |
144
|
|
|
* @param string $locale |
145
|
|
|
* @return bool |
146
|
|
|
*/ |
147
|
|
|
public function isSupported(string $locale) : bool |
148
|
|
|
{ |
149
|
|
|
return file_exists(__DIR__."/../resources/lang/$locale"); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Check if the locale is not supported. |
154
|
|
|
* |
155
|
|
|
* @param string $locale |
156
|
|
|
* @return bool |
157
|
|
|
*/ |
158
|
|
|
public function isNotSupported(string $locale) : bool |
159
|
|
|
{ |
160
|
|
|
return ! $this->isSupported($locale); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Create a new instance from a value. |
165
|
|
|
* |
166
|
|
|
* @param string|int $value |
167
|
|
|
* @param string $locale |
168
|
|
|
* @return self |
169
|
|
|
*/ |
170
|
|
|
public static function parse($value, string $locale) |
171
|
|
|
{ |
172
|
|
|
return new static($value, $locale); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Parse the name of the weekday based on the value. |
177
|
|
|
* |
178
|
|
|
* @param int $value |
179
|
|
|
* @param string $locale |
180
|
|
|
* @return string |
181
|
|
|
*/ |
182
|
|
|
public function parseName(int $value, string $locale = null) : string |
183
|
|
|
{ |
184
|
|
|
if ($value < 0 || $value > 6) { |
185
|
|
|
throw new InvalidArgumentException( |
186
|
|
|
"The provided value ($value) is not a valid weekday." |
187
|
|
|
); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return array_values(static::getNames($locale ?: $this->locale))[$value]; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Parse the value from the weekday name. |
195
|
|
|
* |
196
|
|
|
* @param string $name |
197
|
|
|
* @param string $locale |
198
|
|
|
* @return int |
199
|
|
|
*/ |
200
|
|
|
public function parseValue(string $name, string $locale = null) : int |
201
|
|
|
{ |
202
|
|
|
$names = array_map( |
203
|
|
|
'strtolower', |
204
|
|
|
static::getNames($locale ?: $this->locale) |
205
|
|
|
); |
206
|
|
|
|
207
|
|
|
$value = array_search( |
208
|
|
|
strtolower($name), |
209
|
|
|
array_values($names) |
210
|
|
|
); |
211
|
|
|
|
212
|
|
|
if ($value === false) { |
213
|
|
|
throw new InvalidArgumentException( |
214
|
|
|
sprintf( |
215
|
|
|
'The value could not be parsed for %s in %s.', |
216
|
|
|
$name, |
217
|
|
|
$locale ?: $this->locale |
218
|
|
|
) |
219
|
|
|
); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
return $value; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Set the weekday based on the integer or string value. |
227
|
|
|
* |
228
|
|
|
* @param string|int $value |
229
|
|
|
* @return self |
230
|
|
|
*/ |
231
|
|
|
public function set($value) : self |
232
|
|
|
{ |
233
|
|
|
if (is_numeric($value)) { |
234
|
|
|
$this->value = intval($value); |
235
|
|
|
$this->name = null; |
236
|
|
|
} else { |
237
|
|
|
$this->name = $value; |
238
|
|
|
$this->value = null; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
return $this; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Set the locale of the instance. |
246
|
|
|
* |
247
|
|
|
* @param string $locale |
248
|
|
|
* @return self |
249
|
|
|
*/ |
250
|
|
|
public function setLocale(string $locale) : self |
251
|
|
|
{ |
252
|
|
|
if ($locale != $this->locale) { |
253
|
|
|
if ($this->isNotSupported($locale)) { |
254
|
|
|
static::throwNotSupportedLocaleException($locale); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
$this->name = null; |
258
|
|
|
$this->locale = $locale; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
return $this; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Throw an exception to let the user know that the locale is not yet |
266
|
|
|
* supported. |
267
|
|
|
* |
268
|
|
|
* @param string $locale |
269
|
|
|
* @return void |
270
|
|
|
*/ |
271
|
|
|
protected static function throwNotSupportedLocaleException(string $locale) : void |
272
|
|
|
{ |
273
|
|
|
throw new InvalidArgumentException( |
274
|
|
|
"The locale ($locale) is not yet supported." |
275
|
|
|
); |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|