Ensure duplicate time names can be parsed in NodaTimeSplitter (e.g.

1.1.2024-1.1.2024)
This commit is contained in:
Joshua Ramon Enslin 2024-11-20 10:02:10 +01:00
parent 4dcd93b947
commit b7bb7364d4
Signed by: jrenslin
GPG Key ID: 46016F84501B70AE
2 changed files with 34 additions and 0 deletions

View File

@ -1208,6 +1208,30 @@ final class NodaTimeSplitter {
}
/**
* Removes superfluous characters and makes an input string roughly parsable.
*
* @param string $input Input string.
*
* @return string
*/
private static function _runBasicNameCleanup(string $input):string {
$input = trim(trim($input), ',;');
// Clean away duplicate inputs
// 1440-1440
if (str_contains($input, '-')) {
$parts = explode('-', $input);
if (count($parts) === 2 && $parts[0] === $parts[1]) {
$input = $parts[0];
}
}
return $input;
}
/**
* Wrapper to check if any splitting command works.
*
@ -1217,6 +1241,8 @@ final class NodaTimeSplitter {
*/
public static function attempt_splitting(string $datum):NodaSplitTime|false {
$datum = self::_runBasicNameCleanup($datum);
try {
if (!empty($moda = self::is_timespan($datum))) {
return $moda;

View File

@ -1056,6 +1056,14 @@ final class NodaTimeSplitterTest extends TestCase {
self::assertEquals('-1400', $output->start_year);
self::assertEquals('-1400-01-01', $output->start_date);
// Same start as end with day
$output = NodaTimeSplitter::attempt_splitting("11.10.1991-11.10.1991");
self::assertNotEmpty($output);
self::assertEquals('1991', $output->start_year);
self::assertEquals('1991-10-11', $output->start_date);
self::assertEquals('1991', $output->end_year);
self::assertEquals('1991-10-11', $output->end_date);
}
/**