1 | <?php |
||
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 |
||
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 |
||
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 |
||
114 | |||
115 | /** |
||
116 | * Retrieve the value of the weekday. |
||
117 | * |
||
118 | * @return int |
||
119 | */ |
||
120 | public function getValue() : int |
||
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 |
||
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 |
||
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 |
||
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) |
||
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 |
||
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 |
||
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 |
||
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 |
||
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 |
||
277 | } |
||
278 |