From b7bb7364d4ebba0cfff810f51b2be63cf1ab61b1 Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Wed, 20 Nov 2024 10:02:10 +0100 Subject: [PATCH] Ensure duplicate time names can be parsed in NodaTimeSplitter (e.g. 1.1.2024-1.1.2024) --- src/NodaTimeSplitter.php | 26 ++++++++++++++++++++++++++ tests/NodaTimeSplitterTest.php | 8 ++++++++ 2 files changed, 34 insertions(+) diff --git a/src/NodaTimeSplitter.php b/src/NodaTimeSplitter.php index 66b4a5a..e691ce3 100644 --- a/src/NodaTimeSplitter.php +++ b/src/NodaTimeSplitter.php @@ -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; diff --git a/tests/NodaTimeSplitterTest.php b/tests/NodaTimeSplitterTest.php index 7e3baad..0547bd0 100644 --- a/tests/NodaTimeSplitterTest.php +++ b/tests/NodaTimeSplitterTest.php @@ -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); + } /**