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