Skip to content

Commit

Permalink
Allow separate retrieval of lastname and lastname prefixes (fixes #10)
Browse files Browse the repository at this point in the history
  • Loading branch information
wyrfel committed Aug 28, 2018
1 parent 0bf85f4 commit f72164c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
52 changes: 46 additions & 6 deletions src/Name.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand Down Expand Up @@ -159,19 +170,48 @@ 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)) {
if (!($part instanceof AbstractPart)) {
continue;
}

$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) {
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) {
return get_class($part) === 'TheIconic\\NameParser\\Part\\' . $type;
}
}
15 changes: 15 additions & 0 deletions tests/NameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}

0 comments on commit f72164c

Please sign in to comment.