From b601d5080cada16e0ceac30aeda159bed130d8de Mon Sep 17 00:00:00 2001 From: Lars Olesen Date: Fri, 8 Oct 2021 12:26:17 +0200 Subject: [PATCH] Set 9. klasse correctly --- src/Client.php | 16 +++++++++++----- src/Utility.php | 17 +++++++++++++++++ tests/RegistrationRepositoryTest.php | 1 + tests/UtilityTest.php | 10 ++++++++++ 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/src/Client.php b/src/Client.php index 059a1f2..fd0a1ce 100644 --- a/src/Client.php +++ b/src/Client.php @@ -86,11 +86,6 @@ public function createNewRegistrations(array $registrations) $value = substr($value, 0, 30); } } - foreach (array('.TidlSkole') as $variable) { - if (strpos($key, $variable) !== false) { - $value = substr($value, 0, 48); - } - } foreach (array('.CprNr') as $variable) { if (strpos($key, $variable) !== false) { $value = substr($value, 0, 20); @@ -107,6 +102,17 @@ public function createNewRegistrations(array $registrations) if (strpos($key, '.Kommune') !== false) { $value = $this->getUtilityClass()->getMunicipalityCode($value); } + foreach (array('.TidlSkole') as $variable) { + if (strpos($key, $variable) !== false) { + $value = substr($value, 0, 48); + } + } + foreach (array('.Klasse') as $variable) { + if (strpos($key, $variable) !== false) { + $value = substr($value, 0, 2); + $value = $this->getUtilityClass()->fixClass($value); + } + } $reg->addChild($key, htmlspecialchars($value)); } } diff --git a/src/Utility.php b/src/Utility.php index 0cd88b0..3fc0266 100644 --- a/src/Utility.php +++ b/src/Utility.php @@ -36,6 +36,9 @@ class Utility implements UtilityInterface public function getCountryCode($country) { $rows = $this->getCSV(DIRNAME(__FILE__) . "/data/Landekoder.csv", $country); + if ($rows == 0) { + return 0; + } $stored_in_column = 1; return $rows[$stored_in_column]; } @@ -50,6 +53,9 @@ public function getCountryCode($country) public function getMunicipalityCode($municipality) { $rows = $this->getCSV(DIRNAME(__FILE__) . "/data/Kommuner.csv", $municipality); + if ($rows == 0) { + return 0; + } $stored_in_column = 2; return $rows[$stored_in_column]; } @@ -67,6 +73,17 @@ public function fixPhoneNumber($phonenumber) return $string; } + /** + * Make sure that 'klasse' only returns number + * + * $param string $class Klasse string + * + * $return integer + */ + public function fixClass($class) + { + return preg_replace('/[^0-9]/', '', $class); + } /** * Loops through csv file and stops with search diff --git a/tests/RegistrationRepositoryTest.php b/tests/RegistrationRepositoryTest.php index 2495a27..45f45a9 100644 --- a/tests/RegistrationRepositoryTest.php +++ b/tests/RegistrationRepositoryTest.php @@ -247,6 +247,7 @@ public function testEducationLongCourses() 'Elev.Adresse' => 'Ørnebjergvej 28', 'Elev.Lokalby' => 'Grejs', 'Elev.Kommune' => 'Vejle', + 'Elev.Klasse' => '9.', 'Elev.Postnr' => '7100', 'Elev.Bynavn' => 'Vejle', 'Elev.CprNr' => '0101421942', diff --git a/tests/UtilityTest.php b/tests/UtilityTest.php index 57f7055..f49f16c 100644 --- a/tests/UtilityTest.php +++ b/tests/UtilityTest.php @@ -44,4 +44,14 @@ public function testGetMunicipalityCodeThatIsNotFound() $utility = new Utility(); $this->assertEquals(0, $utility->getMunicipalityCode("NotFound")); } + + /** + * Test FixClass() + */ + public function testFixClassReturnsNumericValue() + { + $utility = new Utility(); + $this->assertEquals(9, $utility->fixClass("9.")); + $this->assertTrue(is_numeric($utility->fixClass("9."))); + } }