From cb05070d08d23ad3a79f97c423399eb61fe97eaf Mon Sep 17 00:00:00 2001 From: oliverstasa Date: Thu, 28 Jan 2021 14:01:57 +0000 Subject: [PATCH 1/6] Create timestamp{}, timestampToArray() --- src/Lib/Util/Timestamp.php | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/Lib/Util/Timestamp.php diff --git a/src/Lib/Util/Timestamp.php b/src/Lib/Util/Timestamp.php new file mode 100644 index 0000000..c373c86 --- /dev/null +++ b/src/Lib/Util/Timestamp.php @@ -0,0 +1,33 @@ + + */ + public static function timestampToArray(string $str): array + { + $ts = explode(' ', $str); + $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]) && + strlen($date[0]) == 4 && strlen($date[2]) == 2 && strlen($date[2]) == 2 && + strlen($date[0]) == 4 && strlen($date[2]) == 2 && strlen($date[2]) == 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:MM:SS', 400); + } + } +} \ No newline at end of file From 762ee880dae4766d82a558fbdde9ffe99edb9ce2 Mon Sep 17 00:00:00 2001 From: oliverstasa Date: Thu, 28 Jan 2021 14:02:23 +0000 Subject: [PATCH 2/6] Update isTimestamp() --- src/Lib/Assert/Text.php | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/Lib/Assert/Text.php b/src/Lib/Assert/Text.php index b402e45..66d7f30 100644 --- a/src/Lib/Assert/Text.php +++ b/src/Lib/Assert/Text.php @@ -4,6 +4,7 @@ namespace App\Lib\Assert; + class Text { /** @@ -11,9 +12,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 +38,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; + } + } } From 7432416ef0c88576d66d8780d0f1dc2cd690236a Mon Sep 17 00:00:00 2001 From: oliverstasa Date: Thu, 28 Jan 2021 14:14:44 +0000 Subject: [PATCH 3/6] Fix no need to test ints for length here --- src/Lib/Util/Timestamp.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Lib/Util/Timestamp.php b/src/Lib/Util/Timestamp.php index c373c86..71f9b8d 100644 --- a/src/Lib/Util/Timestamp.php +++ b/src/Lib/Util/Timestamp.php @@ -22,9 +22,7 @@ public static function timestampToArray(string $str): array 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]) && - strlen($date[0]) == 4 && strlen($date[2]) == 2 && strlen($date[2]) == 2 && - strlen($date[0]) == 4 && strlen($date[2]) == 2 && strlen($date[2]) == 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:MM:SS', 400); From 955367426b9dc58555bb14562d852a82c5d61d07 Mon Sep 17 00:00:00 2001 From: oliverstasa Date: Thu, 28 Jan 2021 14:26:59 +0000 Subject: [PATCH 4/6] Fix exceeption bad comment --- src/Lib/Util/Timestamp.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Lib/Util/Timestamp.php b/src/Lib/Util/Timestamp.php index 71f9b8d..0723096 100644 --- a/src/Lib/Util/Timestamp.php +++ b/src/Lib/Util/Timestamp.php @@ -25,7 +25,7 @@ public static function timestampToArray(string $str): array 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:MM:SS', 400); + throw new Exception('Timestamp is Not in right format YYYY-MM-DD HH:II:SS', 400); } } -} \ No newline at end of file +} From f5310496b749bb13312357d961e8aa1e33de67c7 Mon Sep 17 00:00:00 2001 From: oliverstasa Date: Thu, 28 Jan 2021 14:51:05 +0000 Subject: [PATCH 5/6] Fix cs:fix --- src/Lib/Assert/Text.php | 5 ++--- src/Lib/Util/Timestamp.php | 8 ++++---- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Lib/Assert/Text.php b/src/Lib/Assert/Text.php index 66d7f30..4b134c6 100644 --- a/src/Lib/Assert/Text.php +++ b/src/Lib/Assert/Text.php @@ -4,7 +4,6 @@ namespace App\Lib\Assert; - class Text { /** @@ -42,7 +41,7 @@ public static function hasMin(int $lim, string $str): bool /** * Test if string is date - * + * * @return bool */ public static function isTimestamp(string $str): bool @@ -52,7 +51,7 @@ public static function isTimestamp(string $str): bool $ts['h'] >= 0 && $ts['h'] <= 23 && $ts['i'] >= 0 && $ts['i'] <= 59 && $ts['s'] >= 0 && $ts['s'] <= 59) { - return true; + return true; } else { return false; } diff --git a/src/Lib/Util/Timestamp.php b/src/Lib/Util/Timestamp.php index 0723096..5f37ec2 100644 --- a/src/Lib/Util/Timestamp.php +++ b/src/Lib/Util/Timestamp.php @@ -6,15 +6,15 @@ use Exception; - -class Timestamp { +class Timestamp +{ /** * Retruns array from date string * * @param string $str * @return array */ - public static function timestampToArray(string $str): array + public static function timestampToArray(string $str): array { $ts = explode(' ', $str); $date = explode('-', $ts[0]); @@ -23,7 +23,7 @@ public static function timestampToArray(string $str): array 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]))); + 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); } From c9f0a895d7f559f498b954964458df73242cefbc Mon Sep 17 00:00:00 2001 From: oliverstasa Date: Thu, 28 Jan 2021 15:02:52 +0000 Subject: [PATCH 6/6] Update add possibility of format yyyy-mm-ddThh:ii --- src/Lib/Util/Timestamp.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Lib/Util/Timestamp.php b/src/Lib/Util/Timestamp.php index 5f37ec2..b1589f9 100644 --- a/src/Lib/Util/Timestamp.php +++ b/src/Lib/Util/Timestamp.php @@ -16,7 +16,15 @@ class Timestamp */ public static function timestampToArray(string $str): array { - $ts = explode(' ', $str); + 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]);