|
2 | 2 | /** |
3 | 3 | * |
4 | 4 | * @package Thaidate |
5 | | - * @version 2.1.0 |
| 5 | + * @version 2.1.1 |
6 | 6 | * @author Vee W. |
7 | 7 | * @license http://opensource.org/licenses/MIT |
8 | 8 | * |
@@ -42,9 +42,14 @@ class Thaidate |
42 | 42 | * @param string $format The format as same as PHP date function format. See http://php.net/manual/en/function.date.php |
43 | 43 | * @param int $timestamp The optional timestamp is an integer Unix timestamp. |
44 | 44 | * @return string Return the formatted date/time string. |
| 45 | + * @throws \InvalidArgumentException Throw the exception if invalid argument type is specify. |
45 | 46 | */ |
46 | 47 | public function date($format, $timestamp = '') |
47 | 48 | { |
| 49 | + if (!is_string($format)) { |
| 50 | + throw new \InvalidArgumentException('The argument $format must be string.'); |
| 51 | + } |
| 52 | + |
48 | 53 | if (!is_numeric($timestamp)) { |
49 | 54 | $timestamp = time(); |
50 | 55 | } |
@@ -94,12 +99,18 @@ public function date($format, $timestamp = '') |
94 | 99 | * Thai date use `\IntlDateFormatter()` class. |
95 | 100 | * |
96 | 101 | * @since 2.1.0 |
| 102 | + * @see https://www.php.net/manual/en/class.intldateformatter.php |
97 | 103 | * @param string $format The format or pattern as **same** as ICU format. See https://unicode-org.github.io/icu/userguide/format_parse/datetime/ |
98 | | - * @param int $timestamp |
| 104 | + * @param int $timestamp The optional timestamp is an integer Unix timestamp. |
99 | 105 | * @return string Return the formatted date/time string. |
| 106 | + * @throws \InvalidArgumentException Throw the exception if invalid argument type is specify. |
100 | 107 | */ |
101 | 108 | public function intlDate($format, $timestamp = '') |
102 | 109 | { |
| 110 | + if (!is_string($format)) { |
| 111 | + throw new \InvalidArgumentException('The argument $format must be string.'); |
| 112 | + } |
| 113 | + |
103 | 114 | if (!is_numeric($timestamp)) { |
104 | 115 | $timestamp = time(); |
105 | 116 | } |
@@ -134,23 +145,30 @@ public function intlDate($format, $timestamp = '') |
134 | 145 | * @param int $timestamp The optional timestamp is an integer Unix timestamp. |
135 | 146 | * @return string Return the formatted date/time string.<br> |
136 | 147 | * This method will be show the notice if function `strftime()` is deprecated or removed from currently running PHP version. |
| 148 | + * @throws \InvalidArgumentException Throw the exception if invalid argument type is specify. |
137 | 149 | */ |
138 | 150 | public function strftime($format, $timestamp = '') |
139 | 151 | { |
140 | 152 | if (!function_exists('strftime') || version_compare(PHP_VERSION, '8.1', '>=')) { |
| 153 | + // if function `strftime` is not exists or deprecated (since PHP 8.1). |
141 | 154 | // notice the developers to upgrade their code. |
142 | 155 | // this method can keep running with new version of PHP but need more attention about format/pattern. |
143 | 156 | // so, use notice instead of warning, error, deprecated level. |
144 | 157 | trigger_error( |
145 | 158 | 'Function `strftime()` is deprecated |
146 | 159 | and method `\Rundiz\Thaidate\Thaidate::strftime()` is using replacement which may return incorrect result. |
147 | | - Please upgrade your code to use `\Rundiz\Thaidate\Thaidate::intlDate()` instead.', |
| 160 | + Please update your code to use `\Rundiz\Thaidate\Thaidate::intlDate()` instead.', |
148 | 161 | E_USER_NOTICE |
149 | 162 | ); |
150 | 163 |
|
151 | 164 | if (class_exists('\IntlDateFormatter')) { |
152 | 165 | return $this->intlDate($this->strftimeFormatToIntlDatePattern($format), $timestamp); |
153 | 166 | } |
| 167 | + // if IntlDateFormatter is not exists then let it run and error occur. |
| 168 | + } |
| 169 | + |
| 170 | + if (!is_string($format)) { |
| 171 | + throw new \InvalidArgumentException('The argument $format must be string.'); |
154 | 172 | } |
155 | 173 |
|
156 | 174 | if (!is_numeric($timestamp)) { |
@@ -200,11 +218,12 @@ public function strftime($format, $timestamp = '') |
200 | 218 |
|
201 | 219 |
|
202 | 220 | /** |
203 | | - * Convert from `strftime()` format to `\IntlDateFormatter()` pattern. |
| 221 | + * Convert from `strftime()` format to `\IntlDateFormatter()` that use ICU pattern. |
204 | 222 | * |
205 | 223 | * There are no patterns that has no word 'วัน' from day of week. |
206 | 224 | * |
207 | 225 | * @since 2.1.0 |
| 226 | + * @see https://unicode-org.github.io/icu/userguide/format_parse/datetime/ See more about ICU pattern. |
208 | 227 | * @param string $format The date format that used by `strftime()` function. |
209 | 228 | * @return string Return converted format. |
210 | 229 | */ |
|
0 commit comments