From a2c875556e18b16892077a47839926934e92bda8 Mon Sep 17 00:00:00 2001 From: Meritoo Date: Thu, 20 Jun 2019 21:32:03 +0200 Subject: [PATCH] Move validation of year, month and day to separate methods --- src/Utilities/Date.php | 65 +++++++++++++++++++++++++++++++++--------- 1 file changed, 51 insertions(+), 14 deletions(-) diff --git a/src/Utilities/Date.php b/src/Utilities/Date.php index e8d870d..27037f0 100644 --- a/src/Utilities/Date.php +++ b/src/Utilities/Date.php @@ -259,20 +259,9 @@ class Date */ public static function getDayOfWeek(int $year, int $month, int $day): int { - // Oops, given year is incorrect - if ($year <= 0) { - throw UnknownDatePartTypeException::createException(DatePartType::YEAR, $year); - } - - // Oops, given month is incorrect - if ($month < 1 || $month > 12) { - throw UnknownDatePartTypeException::createException(DatePartType::MONTH, $month); - } - - // Oops, given day is incorrect - if ($day < 1 || $day > 31) { - throw UnknownDatePartTypeException::createException(DatePartType::DAY, $day); - } + static::validateYear($year); + static::validateMonth($month); + static::validateDay($day); if ($month < 3) { $count = 0; @@ -705,4 +694,52 @@ class Date return $fromFormat instanceof DateTime; } + + /** + * Verifies/validates given year + * + * @param int $year Year to verify/validate + * @throws UnknownDatePartTypeException + */ + private static function validateYear(int $year): void + { + // Oops, given year is incorrect + if ($year >= 0) { + return; + } + + throw UnknownDatePartTypeException::createException(DatePartType::YEAR, $year); + } + + /** + * Verifies/validates given month + * + * @param int $month Month to verify/validate + * @throws UnknownDatePartTypeException + */ + private static function validateMonth(int $month): void + { + // Oops, given month is incorrect + if ($month >= 1 && $month <= 12) { + return; + } + + throw UnknownDatePartTypeException::createException(DatePartType::MONTH, $month); + } + + /** + * Verifies/validates given day + * + * @param int $day Day to verify/validate + * @throws UnknownDatePartTypeException + */ + private static function validateDay(int $day): void + { + // Oops, given day is incorrect + if ($day >= 1 && $day <= 31) { + return; + } + + throw UnknownDatePartTypeException::createException(DatePartType::DAY, $day); + } }