From 1685d78f6586b1aecb61b2d565a254f619237c9c Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Sun, 4 Oct 2020 19:27:23 +0200 Subject: [PATCH] Allow splitting times "before " --- src/NodaTimeSplitter.php | 24 +++++++++++++++++++++++- tests/NodaTimeSplitterTest.php | 24 ++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/NodaTimeSplitter.php b/src/NodaTimeSplitter.php index d32d776..e9f2aeb 100644 --- a/src/NodaTimeSplitter.php +++ b/src/NodaTimeSplitter.php @@ -189,7 +189,12 @@ final class NodaTimeSplitter { */ public static function timePartsToTimeName(array $moda):string { - if (!empty($moda[5]) and $moda[5] === "Nach" and $moda[1] === "?") { + if (!empty($moda[5]) and $moda[5] === "Vor" and $moda[0] === "?") { + if (empty(trim($moda[2], "0 "))) $moda[1] = strval(intval($moda[1]) + 1); + $moda[0] = $moda[1]; + $prefix = "Vor "; + } + else if (!empty($moda[5]) and $moda[5] === "Nach" and $moda[1] === "?") { if (empty(trim($moda[2], "0 "))) $moda[0] = strval(intval($moda[0]) - 1); $moda[1] = $moda[0]; $prefix = "Nach "; @@ -246,6 +251,9 @@ final class NodaTimeSplitter { public static function timePartsToCountingYear(array $moda):int { if ($moda[0] === "?") { + if (!empty($moda[5]) and $moda[5] === "Vor" and empty(trim($moda[2], " 0"))) { + return abs(intval($moda[1])) + 1; + } return abs(intval($moda[1])); } @@ -698,6 +706,7 @@ final class NodaTimeSplitter { return $output; } } + if (preg_match("/^[0-9]{4}\ (\(nach)/", $datum)) { if (($spacePos = strpos($datum, " ")) === false) { return []; @@ -716,6 +725,19 @@ final class NodaTimeSplitter { } } + if (preg_match("/^(Vor|vor)\ /", $datum)) { + if (($spacePos = strpos($datum, " ")) === false) { + return []; + } + if ($output = self::attempt_splitting(substr($datum, $spacePos))) { + + $output[0] = "?"; + if (empty(trim($output[2], "0 .,"))) $output[1] = strval((intval($output[1]) - 1)); + $output[5] = "Vor"; + return $output; + } + } + if (preg_match("/^(Ab|Seit|seit)\ /", $datum)) { if (($spacePos = strpos($datum, " ")) === false) { return []; diff --git a/tests/NodaTimeSplitterTest.php b/tests/NodaTimeSplitterTest.php index ea1d2df..0460df6 100644 --- a/tests/NodaTimeSplitterTest.php +++ b/tests/NodaTimeSplitterTest.php @@ -144,6 +144,30 @@ final class NodaTimeSplitterTest extends TestCase { self::assertEquals(NodaTimeSplitter::timePartsToTimeName($output), "1920-1929"); self::assertEquals(NodaTimeSplitter::timePartsToCountingYear($output), 1925); + $output = NodaTimeSplitter::attempt_splitting("Vor Dezember 2020"); + self::assertEquals($output, [ + 0 => "?", + 1 => "2020", + 2 => "12", + 3 => "00", + 4 => "+", + 5 => "Vor", + ]); + self::assertEquals(NodaTimeSplitter::timePartsToTimeName($output), "Vor Dezember 2020"); + self::assertEquals(NodaTimeSplitter::timePartsToCountingYear($output), 2020); + + $output = NodaTimeSplitter::attempt_splitting("Vor 2020"); + self::assertEquals($output, [ + 0 => "?", + 1 => "2019", + 2 => "00", + 3 => "00", + 4 => "+", + 5 => "Vor", + ]); + self::assertEquals(NodaTimeSplitter::timePartsToTimeName($output), "Vor 2020"); + self::assertEquals(NodaTimeSplitter::timePartsToCountingYear($output), 2020); + $output = NodaTimeSplitter::attempt_splitting("Nach Januar 2020"); self::assertEquals($output, [ 0 => "2020",