diff --git a/src/Lib/Assert/Text.php b/src/Lib/Assert/Text.php index b402e45..4b134c6 100644 --- a/src/Lib/Assert/Text.php +++ b/src/Lib/Assert/Text.php @@ -11,9 +11,9 @@ class Text * * @return bool */ - public static function isEmail(string $email): bool + public static function isEmail(string $str): bool { - return filter_var($email, FILTER_VALIDATE_EMAIL) === false ? false : true; + return filter_var($str, FILTER_VALIDATE_EMAIL) === false ? false : true; } @@ -37,4 +37,23 @@ public static function hasMin(int $lim, string $str): bool { return strlen($str) >= $lim; } + + + /** + * Test if string is date + * + * @return bool + */ + public static function isTimestamp(string $str): bool + { + $ts = \App\Lib\Util\Timestamp::timestampToArray($str); + if (checkdate($ts['m'], $ts['d'], $ts['y']) && + $ts['h'] >= 0 && $ts['h'] <= 23 && + $ts['i'] >= 0 && $ts['i'] <= 59 && + $ts['s'] >= 0 && $ts['s'] <= 59) { + return true; + } else { + return false; + } + } } diff --git a/src/Lib/Util/Timestamp.php b/src/Lib/Util/Timestamp.php new file mode 100644 index 0000000..b1589f9 --- /dev/null +++ b/src/Lib/Util/Timestamp.php @@ -0,0 +1,39 @@ + + */ + public static function timestampToArray(string $str): array + { + if (strpos($str, ' ')) { + $ts = explode(' ', $str); + } else if (strpos($str, 'T')) { + $ts = explode('T', $str); + $ts[1] .= ':00'; + } else { + throw new Exception('Timestamp is Not in right format YYYY-MM-DD HH:II:SS', 400); + } + + $date = explode('-', $ts[0]); + $time = explode(':', $ts[1]); + + if (sizeof($date) == 3 && sizeof($time) == 3 && + is_numeric($date[0]) && is_numeric($date[1]) && is_numeric($date[2]) && + is_numeric($time[0]) && is_numeric($time[1]) && is_numeric($time[2])) { + return (array('y' => intval($date[0]), 'm' => intval($date[1]), 'd' => intval($date[2]), 'h' => intval($time[0]), 'i' => intval($time[1]), 's' => intval($time[2]))); + } else { + throw new Exception('Timestamp is Not in right format YYYY-MM-DD HH:II:SS', 400); + } + } +}