From 902d1466b8574ebb88c67b3326101b005a564f60 Mon Sep 17 00:00:00 2001 From: Andre Wyrwa Date: Tue, 28 Aug 2018 22:07:29 +1000 Subject: [PATCH 1/2] Allow separate retrieval of lastname and lastname prefixes (fixes #10) --- src/Name.php | 50 ++++++++++++++++++++++++++++++++++++++++------ tests/NameTest.php | 15 ++++++++++++++ 2 files changed, 59 insertions(+), 6 deletions(-) diff --git a/src/Name.php b/src/Name.php index 304a91f..2e47497 100644 --- a/src/Name.php +++ b/src/Name.php @@ -94,11 +94,22 @@ public function getFirstname(): string /** * get the last name * + * @param bool $pure * @return string */ - public function getLastname(): string + public function getLastname(bool $pure = false): string { - return $this->export('Lastname'); + return $this->export('Lastname', $pure); + } + + /** + * get the last name prefix + * + * @return string + */ + public function getLastnamePrefix(): string + { + return $this->export('LastnamePrefix'); } /** @@ -159,19 +170,46 @@ public function getMiddlename(): string /** * helper method used by getters to extract and format relevant name parts * - * @param string $type the part type to export - * @return string the exported parts + * @param string $type + * @param bool $pure + * @return string */ - protected function export($type): string + protected function export(string $type, bool $strict = false): string { $matched = []; foreach ($this->parts as $part) { - if ($part instanceof AbstractPart && is_a($part, 'TheIconic\\NameParser\\Part\\' . $type)) { + $method = ($strict) ? 'isStrictType' : 'isType'; + + if (call_user_func([$this, $method], $part, $type)) { $matched[] = $part->normalize(); } } return implode(' ', $matched); } + + /** + * helper method to check if a part is of the given type + * + * @param AbstractPart $part + * @param string $type + * @return bool + */ + protected function isType(AbstractPart $part, string $type): bool + { + return is_a($part, 'TheIconic\\NameParser\\Part\\' . $type); + } + + /** + * helper method to check if a part is the exact given type (not considering superclasses) + * + * @param AbstractPart $part + * @param string $type + * @return bool + */ + protected function isStrictType(AbstractPart $part, string $type): bool + { + return get_class($part) === 'TheIconic\\NameParser\\Part\\' . $type; + } } diff --git a/tests/NameTest.php b/tests/NameTest.php index 6830f57..b759093 100644 --- a/tests/NameTest.php +++ b/tests/NameTest.php @@ -6,6 +6,7 @@ use TheIconic\NameParser\Part\Firstname; use TheIconic\NameParser\Part\Initial; use TheIconic\NameParser\Part\Lastname; +use TheIconic\NameParser\Part\LastnamePrefix; use TheIconic\NameParser\Part\Middlename; use TheIconic\NameParser\Part\Nickname; use TheIconic\NameParser\Part\Salutation; @@ -40,4 +41,18 @@ public function testGetNickname() $this->assertSame('Jim', $name->getNickname()); $this->assertSame('(Jim)', $name->getNickname(true)); } + + public function testGettingLastnameAndLastnamePrefixSeparately() + { + $name = new Name([ + new Firstname('Frank'), + new LastnamePrefix('van'), + new Lastname('Delft'), + ]); + + $this->assertSame('Frank', $name->getFirstname()); + $this->assertSame('van', $name->getLastnamePrefix()); + $this->assertSame('Delft', $name->getLastname(true)); + $this->assertSame('van Delft', $name->getLastname()); + } } From 6b38f9e27a51f5f8616fc8a86a55e9efc1584a19 Mon Sep 17 00:00:00 2001 From: Andre Wyrwa Date: Tue, 28 Aug 2018 22:20:05 +1000 Subject: [PATCH 2/2] Simplify code --- src/Name.php | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/src/Name.php b/src/Name.php index 2e47497..83bc2bc 100644 --- a/src/Name.php +++ b/src/Name.php @@ -6,6 +6,8 @@ class Name { + private const PARTS_NAMESPACE = 'TheIconic\NameParser\Part'; + /** * @var array the parts that make up this name */ @@ -179,9 +181,7 @@ protected function export(string $type, bool $strict = false): string $matched = []; foreach ($this->parts as $part) { - $method = ($strict) ? 'isStrictType' : 'isType'; - - if (call_user_func([$this, $method], $part, $type)) { + if ($part instanceof AbstractPart && $this->isType($part, $type, $strict)) { $matched[] = $part->normalize(); } } @@ -194,22 +194,17 @@ protected function export(string $type, bool $strict = false): string * * @param AbstractPart $part * @param string $type + * @param bool $strict * @return bool */ - protected function isType(AbstractPart $part, string $type): bool + protected function isType(AbstractPart $part, string $type, bool $strict = false): bool { - return is_a($part, 'TheIconic\\NameParser\\Part\\' . $type); - } + $className = sprintf('%s\\%s', self::PARTS_NAMESPACE, $type); - /** - * helper method to check if a part is the exact given type (not considering superclasses) - * - * @param AbstractPart $part - * @param string $type - * @return bool - */ - protected function isStrictType(AbstractPart $part, string $type): bool - { - return get_class($part) === 'TheIconic\\NameParser\\Part\\' . $type; + if ($strict) { + return get_class($part) === $className; + } + + return is_a($part, $className); } }