Skip to content

Commit

Permalink
Merge pull request #27 from theiconic/feature/add-given-name-getter
Browse files Browse the repository at this point in the history
Add a getter to retrieve given name in original order to Name object (Fixes #21)
  • Loading branch information
wyrfel authored Nov 5, 2019
2 parents 29c4f88 + 91b8091 commit b6c49b0
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 33 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ echo $name->getNickname();
echo $name->getInitials();
echo $name->getSuffix();

print_r($name->getAll()); // all parts as an associative array
print_r($name->getAll());

echo $name; // re-prints the full normalised name
echo $name;
```
An empty string is returned for missing parts.

Expand All @@ -104,6 +104,22 @@ echo $name->getNickname(); // The Giant
echo $name->getNickname(true); // (The Giant)
```

#### Re-print given name in the order as entered
You can re-print the parts that form a given name (that is first name, middle names and any initials)
in the order they were entered in while still applying normalisation
via `getGivenName()`:
```php
echo $name->getGivenName(); // J. Peter M.
```

#### Re-print full name (actual name parts only)
You can re-print the full name, that is the given name as above followed by
any last name parts (excluding any salutations, nick names or suffixes)
via `getFullName()`:
```php
echo $name->getFullName(); // J. Peter M. Schluter
```

### Setting Languages
```php
$parser = new TheIconic\NameParser\Parser([
Expand Down
30 changes: 30 additions & 0 deletions src/Name.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace TheIconic\NameParser;

use TheIconic\NameParser\Part\AbstractPart;
use TheIconic\NameParser\Part\GivenNamePart;

class Name
{
Expand Down Expand Up @@ -83,6 +84,35 @@ public function getAll(bool $format = false): array
return $results;
}

/**
* get the given name (first name, middle names and initials)
* in the order they were entered while still applying normalisation
*
* @return string
*/
public function getGivenName(): string
{
$fullNameParts = [];

foreach ($this->parts as $part) {
if ($part instanceof GivenNamePart) {
$fullNameParts[] = $part->normalize();
}
}

return implode(' ', $fullNameParts);
}

/**
* get the given name followed by the last name (including any prefixes)
*
* @return string
*/
public function getFullName(): string
{
return sprintf('%s %s', $this->getGivenName(), $this->getLastname());
}

/**
* get the first name
*
Expand Down
11 changes: 1 addition & 10 deletions src/Part/Firstname.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,6 @@

namespace TheIconic\NameParser\Part;

class Firstname extends AbstractPart
class Firstname extends GivenNamePart
{
/**
* camelcase the firstname
*
* @return string
*/
public function normalize(): string
{
return $this->camelcase($this->getValue());
}
}
7 changes: 7 additions & 0 deletions src/Part/GivenNamePart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace TheIconic\NameParser\Part;

abstract class GivenNamePart extends NamePart
{
}
2 changes: 1 addition & 1 deletion src/Part/Initial.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace TheIconic\NameParser\Part;

class Initial extends AbstractPart
class Initial extends GivenNamePart
{
/**
* uppercase the initial
Expand Down
11 changes: 1 addition & 10 deletions src/Part/Lastname.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,6 @@

namespace TheIconic\NameParser\Part;

class Lastname extends AbstractPart
class Lastname extends NamePart
{
/**
* camelcase the lastname
*
* @return string
*/
public function normalize(): string
{
return $this->camelcase($this->getValue());
}
}
11 changes: 1 addition & 10 deletions src/Part/Middlename.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,6 @@

namespace TheIconic\NameParser\Part;

class Middlename extends AbstractPart
class Middlename extends GivenNamePart
{
/**
* camelcase the middlename for normalization
*
* @return string
*/
public function normalize(): string
{
return $this->camelcase($this->getValue());
}
}
16 changes: 16 additions & 0 deletions src/Part/NamePart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace TheIconic\NameParser\Part;

abstract class NamePart extends AbstractPart
{
/**
* camelcase the lastname
*
* @return string
*/
public function normalize(): string
{
return $this->camelcase($this->getValue());
}
}
14 changes: 14 additions & 0 deletions tests/NameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,18 @@ public function testGettingLastnameAndLastnamePrefixSeparately()
$this->assertSame('Delft', $name->getLastname(true));
$this->assertSame('van Delft', $name->getLastname());
}

public function testGetGivenNameShouldReturnGivenNameInGivenOrder(): void
{
$parser = new Parser();
$name = $parser->parse('Schuler, J. Peter M.');
$this->assertSame('J. Peter M.', $name->getGivenName());
}

public function testGetFullNameShouldReturnTheFullNameInGivenOrder(): void
{
$parser = new Parser();
$name = $parser->parse('Schuler, J. Peter M.');
$this->assertSame('J. Peter M. Schuler', $name->getFullName());
}
}

0 comments on commit b6c49b0

Please sign in to comment.