From ff35ca7bd9edde93a8190514a4cfd20af877cb84 Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Sat, 3 Oct 2020 16:10:43 +0200 Subject: [PATCH] Enable time splitter to deal with some roman numbers --- src/NodaTimeSplitter.php | 14 +++++++++++++- tests/NodaTimeSplitterTest.php | 12 ++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/NodaTimeSplitter.php b/src/NodaTimeSplitter.php index c79e89f..035af37 100644 --- a/src/NodaTimeSplitter.php +++ b/src/NodaTimeSplitter.php @@ -54,6 +54,14 @@ final class NodaTimeSplitter { "vor Christus" => "v. Chr.", ]; + const STRINGS_TO_CLEAN_START = [ + "V. " => "5. ", + "IV. " => "4. ", + "III. " => "3. ", + "II. " => "2. ", + "I. " => "1. ", + ]; + const STOP_STRINGS_GERMAN = [ "-", ",", @@ -118,6 +126,11 @@ final class NodaTimeSplitter { while (strpos($input, " -") !== false) $input = str_replace(" -", "-", $input); while (strpos($input, "- ") !== false) $input = str_replace("- ", "-", $input); $input = strtr($input, self::STRINGS_TO_CLEAN); + foreach (self::STRINGS_TO_CLEAN_START as $toCleanFrom => $toCleanTo) { + if (strpos($input, $toCleanFrom) === 0) { + $input = str_replace($toCleanFrom, $toCleanTo, $input); + } + } while (strpos($input, "..") !== false) $input = str_replace("..", ".", $input); return trim($input, ", [](){}"); @@ -752,7 +765,6 @@ final class NodaTimeSplitter { // 1. Jahrhundert if (preg_match("/^[0-9]\.\ (Jh\.|Jahrhundert|sz|század)$/", $datum)) { - echo "HI"; if ($centuryNo = intval(substr($datum, 0, 1))) { $centuryNo--; return [(string)$centuryNo . "01", strval($centuryNo + 1) . "00", "00", "00", $bcBceIndicator, ""]; diff --git a/tests/NodaTimeSplitterTest.php b/tests/NodaTimeSplitterTest.php index a0c45b5..c287c87 100644 --- a/tests/NodaTimeSplitterTest.php +++ b/tests/NodaTimeSplitterTest.php @@ -362,6 +362,18 @@ final class NodaTimeSplitterTest extends TestCase { self::assertEquals(NodaTimeSplitter::timePartsToTimeName($output), "1901-2000"); self::assertEquals(NodaTimeSplitter::timePartsToCountingYear($output), 1951); + $output = NodaTimeSplitter::attempt_splitting("III. század"); + self::assertEquals($output, [ + 0 => "201", + 1 => "300", + 2 => "00", + 3 => "00", + 4 => "+", + 5 => "", + ]); + self::assertEquals(NodaTimeSplitter::timePartsToTimeName($output), "201-300 n. Chr."); + self::assertEquals(NodaTimeSplitter::timePartsToCountingYear($output), 251); + $output = NodaTimeSplitter::attempt_splitting("20. század"); self::assertEquals($output, [ 0 => "1901",