Skip to content

Commit

Permalink
Merge pull request #13 from theiconic/feature/explicit-lastname-prefi…
Browse files Browse the repository at this point in the history
…x-getter

Allow separate retrieval of lastname and lastname prefixes (fixes #10)
  • Loading branch information
wyrfel authored Aug 28, 2018
2 parents 0bf85f4 + 6b38f9e commit 81de7cb
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
45 changes: 39 additions & 6 deletions src/Name.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

class Name
{
private const PARTS_NAMESPACE = 'TheIconic\NameParser\Part';

/**
* @var array the parts that make up this name
*/
Expand Down Expand Up @@ -94,11 +96,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 +172,39 @@ 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 && $this->isType($part, $type, $strict)) {
$matched[] = $part->normalize();
}
}

return implode(' ', $matched);
}

/**
* helper method to check if a part is of the given type
*
* @param AbstractPart $part
* @param string $type
* @param bool $strict
* @return bool
*/
protected function isType(AbstractPart $part, string $type, bool $strict = false): bool
{
$className = sprintf('%s\\%s', self::PARTS_NAMESPACE, $type);

if ($strict) {
return get_class($part) === $className;
}

return is_a($part, $className);
}
}
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 81de7cb

Please sign in to comment.