From bde3c2cb9ee3a2c85918b47e7855cb07d2082e71 Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Tue, 4 May 2021 23:04:41 +0200 Subject: [PATCH] Add a class NodaNameSplitterTest, for now splitting names into given name and family name --- src/NodaNameSplitter.php | 84 ++++++++++++++++++++++++++++++++++ tests/NodaNameSplitterTest.php | 46 +++++++++++++++++++ tests/NodaTimeSplitterTest.php | 2 - tests/config.xml | 12 ----- 4 files changed, 130 insertions(+), 14 deletions(-) create mode 100644 src/NodaNameSplitter.php create mode 100644 tests/NodaNameSplitterTest.php diff --git a/src/NodaNameSplitter.php b/src/NodaNameSplitter.php new file mode 100644 index 0000000..5e1c832 --- /dev/null +++ b/src/NodaNameSplitter.php @@ -0,0 +1,84 @@ + + */ +declare(strict_types = 1); + +/** + * Class for splitting actor names. + */ +final class NodaNameSplitter { + + const LANGS_FAMILY_NAME_FIRST = [ + 'hu' + ]; + + /** @var string */ + private string $_lang; + + /** + * Returns assumed given name and family name based on an input string. + * + * @param string $fullName Input string. + * + * @return array{family_name: string, given_name: string}; + */ + public function givenNameFamilyNameFromString(string $fullName):array { + + $words = array_diff(explode(' ', $fullName), ['']); + if (count($words) <= 1) { + return [ + 'family_name' => '', + 'given_name' => $fullName, + ]; + } + + // Languages, which have names starting with the family name, need to have a special + // way of splitting up the full name as follows. + if (in_array($this->_lang, self::LANGS_FAMILY_NAME_FIRST, true)) { + $familyName = array_shift($words); + $givenName = implode(' ', $words); + return [ + 'family_name' => $familyName, + 'given_name' => $givenName, + ]; + } + + // If the first "word" / name part of the name ends with a comma, it is assumed to be + // a family name. + + if (substr($words[0], -1) === ',') { + + $familyName = substr(array_shift($words), 0, -1); + $givenName = implode(' ', $words); + return [ + 'family_name' => $familyName, + 'given_name' => $givenName, + ]; + + } + + $familyName = array_pop($words); + $givenName = implode(' ', $words); + return [ + 'family_name' => $familyName, + 'given_name' => $givenName, + ]; + + } + + /** + * Constructor. + * + * @param string $lang The user's language. + * + * @return void + */ + public function __construct(string $lang) { + + $this->_lang = $lang; + + } +} diff --git a/tests/NodaNameSplitterTest.php b/tests/NodaNameSplitterTest.php new file mode 100644 index 0000000..df31170 --- /dev/null +++ b/tests/NodaNameSplitterTest.php @@ -0,0 +1,46 @@ + + */ +declare(strict_types = 1); +use PHPUnit\Framework\TestCase; +require_once __DIR__ . "/../src/NodaNameSplitter.php"; +require_once __DIR__ . "/../../MD_STD/src/MD_STD.php"; + +/** + * Tests for home page. + */ +final class NodaNameSplitterTest extends TestCase { + /** + * Test to check whether the HTML page is correctly generated. + * + * @author Joshua Ramon Enslin + * @group ValidOutput + * + * @return void + */ + public function testSplitGivenNameFamilyNameFromString():void { + + $splitter = new NodaNameSplitter('de'); + // This applied to all languages + self::assertEquals($splitter->givenNameFamilyNameFromString('Wilhelm'), ['family_name' => '', 'given_name' => 'Wilhelm']); + + self::assertEquals($splitter->givenNameFamilyNameFromString('Friedrich Wilhelm'), ['family_name' => 'Wilhelm', 'given_name' => 'Friedrich']); + self::assertEquals($splitter->givenNameFamilyNameFromString('Wilhelm, Friedrich '), ['family_name' => 'Wilhelm', 'given_name' => 'Friedrich']); + self::assertEquals($splitter->givenNameFamilyNameFromString('Therese von Bacheracht'), ['family_name' => 'Bacheracht', 'given_name' => 'Therese von']); + self::assertEquals($splitter->givenNameFamilyNameFromString('Bacheracht, Therese von'), ['family_name' => 'Bacheracht', 'given_name' => 'Therese von']); + self::assertEquals($splitter->givenNameFamilyNameFromString('von Bacheracht, Therese '), ['family_name' => 'Therese', 'given_name' => 'von Bacheracht,']); + self::assertEquals($splitter->givenNameFamilyNameFromString('Filomeno V. Aguilar, Jr.'), ['family_name' => 'Jr.', 'given_name' => 'Filomeno V. Aguilar,']); + + $splitter = new NodaNameSplitter('hu'); + self::assertEquals($splitter->givenNameFamilyNameFromString('Friedrich Wilhelm'), ['family_name' => 'Friedrich', 'given_name' => 'Wilhelm']); + self::assertEquals($splitter->givenNameFamilyNameFromString('Wilhelm, Friedrich '), ['family_name' => 'Wilhelm,', 'given_name' => 'Friedrich']); + self::assertEquals($splitter->givenNameFamilyNameFromString('Therese von Bacheracht'), ['family_name' => 'Therese', 'given_name' => 'von Bacheracht']); + self::assertEquals($splitter->givenNameFamilyNameFromString('Bacheracht, Therese von'), ['family_name' => 'Bacheracht,', 'given_name' => 'Therese von']); + self::assertEquals($splitter->givenNameFamilyNameFromString('von Bacheracht, Therese '), ['family_name' => 'von', 'given_name' => 'Bacheracht, Therese']); + self::assertEquals($splitter->givenNameFamilyNameFromString('Filomeno V. Aguilar, Jr.'), ['family_name' => 'Filomeno', 'given_name' => 'V. Aguilar, Jr.']); + + } +} diff --git a/tests/NodaTimeSplitterTest.php b/tests/NodaTimeSplitterTest.php index 265da26..a96ebb4 100644 --- a/tests/NodaTimeSplitterTest.php +++ b/tests/NodaTimeSplitterTest.php @@ -13,7 +13,6 @@ require_once __DIR__ . "/../../MD_STD/src/MD_STD.php"; * Tests for home page. */ final class NodaTimeSplitterTest extends TestCase { - /** * Test to check whether the HTML page is correctly generated. * @@ -525,5 +524,4 @@ final class NodaTimeSplitterTest extends TestCase { # self::assertEquals($output, []); } - } diff --git a/tests/config.xml b/tests/config.xml index 684eb42..ad2046e 100644 --- a/tests/config.xml +++ b/tests/config.xml @@ -9,14 +9,11 @@ beStrictAboutTodoAnnotatedTests="true" beStrictAboutCoversAnnotation="false" cacheResult="false" - cacheTokens="false" colors="true" convertErrorsToExceptions="true" convertDeprecationsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" - preserveGlobalState="false" - runTestInSeparateProcess="true" enforceTimeLimit="true" failOnWarning="true" forceCoversAnnotation="false" @@ -31,14 +28,5 @@ timeoutForMediumTests="10" timeoutForLargeTests="60" verbose="true"> - - - - -