Prevent splitting of non-existing exact dates (e.g. 31.04.XXXX)

Close #35
This commit is contained in:
Joshua Ramon Enslin 2025-04-08 03:48:04 +02:00
parent dba60dbce6
commit 00638152cf
Signed by: jrenslin
GPG Key ID: 46016F84501B70AE
2 changed files with 30 additions and 3 deletions

View File

@ -28,9 +28,10 @@ final class NodaSplitTime {
/**
* Returns a single, exact date.
*
* @param string $year Year.
* @param string $month Month.
* @param string $day Day.
* @param string $year Year.
* @param string $month Month.
* @param string $day Day.
* @param NodaTimeBeforeAfterIndicator $before_after_indicator Determines if the time is exact or before / after.
*
* @return NodaSplitTime
*/
@ -296,6 +297,15 @@ final class NodaSplitTime {
/**
* Constructor.
*
* @param string $start_year Year.
* @param string $end_year Year.
* @param string $counting_time_month Month.
* @param string $counting_time_day Day.
* @param NodaCountingTimeIndicator $counting_time_indicator Determines if the time is BCE or CCE.
* @param NodaTimeBeforeAfterIndicator $before_after_indicator Determines if the time is inexact to one direction.
* @param false|string $start_date Start date.
* @param false|string $end_date End date.
*
* @return void
*/
public function __construct(string $start_year, string $end_year,
@ -379,5 +389,19 @@ final class NodaSplitTime {
}
}
// Validate
$startDateTime = MD_STD::strtotime("2000-" . substr($this->start_date, -5));
if (checkdate((int)date('m', $startDateTime), (int)date('d', $startDateTime), (int)date('Y', $startDateTime)) === false) {
throw new MDgenericInvalidInputsException("Invalid start date: " . $this->start_date);
}
if (!empty((int)$this->counting_time_day)) {
// The year 2000 is used here as it is a leap year and lots of years accepted in md are not accepted
// by checkdate.
if (checkdate((int)$this->counting_time_month, (int)$this->counting_time_day, 2000) === false) {
throw new MDgenericInvalidInputsException("Invalid date formed by counting time: " . $this->counting_time_month . ' -- ' . $this->counting_time_day);
}
}
}
}

View File

@ -550,6 +550,9 @@ final class NodaTimeSplitterTest extends TestCase {
];
foreach ([
"30.02.2024",
"31.11.2024",
"31.4.2024",
"1.2.2020-2.2.2020",
"2020 Januar 2-2020 Februar 2",
"2020 Januar-2020 Februar",