Move validation of year, month and day to separate methods

This commit is contained in:
Meritoo
2019-06-20 21:32:03 +02:00
parent c5a68b54af
commit a2c875556e

View File

@@ -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);
}
}