Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Language/English.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ class English implements LanguageInterface
];

const SALUTATIONS = [
'dame' => 'Dame',
'dr' => 'Dr.',
'fr' => 'Fr.',
'lady' => 'Lady',
'lord' => 'Lord',
'madam' => 'Madam',
'master' => 'Mr.',
'miss' => 'Miss',
Expand All @@ -45,9 +48,14 @@ class English implements LanguageInterface
'mrs' => 'Mrs.',
'ms' => 'Ms.',
'mx' => 'Mx.',
'pastor' => 'Pr.',
'pr' => 'Pr.',
'rev' => 'Rev.',
'reverend' => 'Rev.',
'rt hon' => 'Rt Hon',
'sir' => 'Sir',
'prof' => 'Prof.',
'professor' => 'Prof.',
'his honour' => 'His Honour',
'her honour' => 'Her Honour'
];
Expand Down
31 changes: 31 additions & 0 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ class Parser
*/
protected $nicknameDelimiters = [];

/**
* @var array
*/
protected $customSalutations = [];

/**
* @var int
*/
Expand Down Expand Up @@ -263,10 +268,36 @@ protected function getSalutations()
foreach ($this->languages as $language) {
$salutations += $language->getSalutations();
}

$salutations += $this->customSalutations;

return $salutations;
}

/**
* @param array $customSalutations
* @return Parser
*/
public function addCustomSalutation(string $key, string $salutation): Parser
{
$this->customSalutations += [$key => $salutation];

return $this;
}

/**
* @param array $customSalutations
* @return Parser
*/
public function addCustomSalutations(array $customSalutations): Parser
{
foreach ($customSalutations as $key => $salutation) {
$this->addCustomSalutation($key, $salutation);
}

return $this;
}

/**
* @return array
*/
Expand Down
14 changes: 14 additions & 0 deletions tests/NameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,18 @@ public function testGetFullNameShouldReturnTheFullNameInGivenOrder(): void
$name = $parser->parse('Schuler, J. Peter M.');
$this->assertSame('J. Peter M. Schuler', $name->getFullName());
}

public function testAddOneCustomSalutation()
{
$parser = new Parser();
$name = $parser->addCustomSalutation('custom', 'Custom.')->parse('Custom John Smith');
$this->assertSame('Custom. John Smith', $name->getSalutation() . ' ' . $name->getFullName());
}

public function testAddBulkCustomSalutations()
{
$parser = new Parser();
$name = $parser->addCustomSalutations(['custom' => 'Custom.'])->parse('Custom John Smith');
$this->assertSame('Custom. John Smith', $name->getSalutation() . ' ' . $name->getFullName());
}
}