Skip to content

Commit f1b39b0

Browse files
committed
v2.1.1
* Improve strftime to intlDate pattern replacement. * Add new function `thaiIntlDate()`.
1 parent 722d576 commit f1b39b0

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

Rundiz/Thaidate/Thaidate.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
*
44
* @package Thaidate
5-
* @version 2.1.0
5+
* @version 2.1.1
66
* @author Vee W.
77
* @license http://opensource.org/licenses/MIT
88
*
@@ -42,9 +42,14 @@ class Thaidate
4242
* @param string $format The format as same as PHP date function format. See http://php.net/manual/en/function.date.php
4343
* @param int $timestamp The optional timestamp is an integer Unix timestamp.
4444
* @return string Return the formatted date/time string.
45+
* @throws \InvalidArgumentException Throw the exception if invalid argument type is specify.
4546
*/
4647
public function date($format, $timestamp = '')
4748
{
49+
if (!is_string($format)) {
50+
throw new \InvalidArgumentException('The argument $format must be string.');
51+
}
52+
4853
if (!is_numeric($timestamp)) {
4954
$timestamp = time();
5055
}
@@ -94,12 +99,18 @@ public function date($format, $timestamp = '')
9499
* Thai date use `\IntlDateFormatter()` class.
95100
*
96101
* @since 2.1.0
102+
* @see https://www.php.net/manual/en/class.intldateformatter.php
97103
* @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.
99105
* @return string Return the formatted date/time string.
106+
* @throws \InvalidArgumentException Throw the exception if invalid argument type is specify.
100107
*/
101108
public function intlDate($format, $timestamp = '')
102109
{
110+
if (!is_string($format)) {
111+
throw new \InvalidArgumentException('The argument $format must be string.');
112+
}
113+
103114
if (!is_numeric($timestamp)) {
104115
$timestamp = time();
105116
}
@@ -134,23 +145,30 @@ public function intlDate($format, $timestamp = '')
134145
* @param int $timestamp The optional timestamp is an integer Unix timestamp.
135146
* @return string Return the formatted date/time string.<br>
136147
* 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.
137149
*/
138150
public function strftime($format, $timestamp = '')
139151
{
140152
if (!function_exists('strftime') || version_compare(PHP_VERSION, '8.1', '>=')) {
153+
// if function `strftime` is not exists or deprecated (since PHP 8.1).
141154
// notice the developers to upgrade their code.
142155
// this method can keep running with new version of PHP but need more attention about format/pattern.
143156
// so, use notice instead of warning, error, deprecated level.
144157
trigger_error(
145158
'Function `strftime()` is deprecated
146159
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.',
148161
E_USER_NOTICE
149162
);
150163

151164
if (class_exists('\IntlDateFormatter')) {
152165
return $this->intlDate($this->strftimeFormatToIntlDatePattern($format), $timestamp);
153166
}
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.');
154172
}
155173

156174
if (!is_numeric($timestamp)) {
@@ -200,11 +218,12 @@ public function strftime($format, $timestamp = '')
200218

201219

202220
/**
203-
* Convert from `strftime()` format to `\IntlDateFormatter()` pattern.
221+
* Convert from `strftime()` format to `\IntlDateFormatter()` that use ICU pattern.
204222
*
205223
* There are no patterns that has no word 'วัน' from day of week.
206224
*
207225
* @since 2.1.0
226+
* @see https://unicode-org.github.io/icu/userguide/format_parse/datetime/ See more about ICU pattern.
208227
* @param string $format The date format that used by `strftime()` function.
209228
* @return string Return converted format.
210229
*/

0 commit comments

Comments
 (0)