Skip to content
Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

### Fixed

- Combined initials no longer split digits, punctuation, or caseless characters
out of a token, so `Smith, A1 John` keeps `A1` as the first name.

- Keep confidence tokenization, delimiter handling, and comma credential tails
linear and bounded for hostile input while preserving parser/config parity.

Expand Down
16 changes: 14 additions & 2 deletions src/Mapper/InitialMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@ public function map(array $parts): array
&& $length <= $this->combinedMax
&& $stripped !== mb_strtolower($stripped, 'UTF-8')
) {
$initials = Text::graphemes($stripped);

// A case signal alone does not make digits, punctuation, or
// caseless characters initials. Keep mixed tokens intact.
foreach ($initials as $initial) {
if (! $this->isInitial($initial)) {
$mapped[] = $part;

continue 2;
}
}

$expandedParts += $length;
if ($expandedParts > self::MAX_COMBINED_EXPANSION_PARTS) {
throw new \LengthException(
Expand All @@ -109,8 +121,8 @@ public function map(array $parts): array
);
}

foreach (Text::graphemes($stripped) as $initial) {
$mapped[] = $this->isInitial($initial) ? new Initial($initial) : $initial;
foreach ($initials as $initial) {
$mapped[] = new Initial($initial);
}

continue;
Expand Down
64 changes: 64 additions & 0 deletions tests/CombinedInitialTokenTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Tests\Iliaal\NameParser;

use Iliaal\NameParser\Mapper\InitialMapper;
use Iliaal\NameParser\Parser;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

class CombinedInitialTokenTest extends TestCase
{
/**
* @return iterable<string, array{string, int}>
*/
public static function nonInitialTokens(): iterable
{
yield 'digit after letter' => ['A1', 2];
yield 'digit before letter' => ['1A', 2];
yield 'trailing hyphen' => ['A-', 2];
yield 'caseless character' => ['A李', 2];
yield 'hyphenated token' => ['J-P', 3];
yield 'apostrophe token' => ["O'B", 3];
yield 'mixed dotted token' => ['A.1.', 2];
}

#[DataProvider('nonInitialTokens')]
public function testCombinedMappingPreservesWholeNonInitialTokens(string $token, int $limit): void
{
$parts = ['John', $token, 'Smith'];

$this->assertSame($parts, (new InitialMapper($limit))->map($parts));
}

public function testWesternNameDoesNotSplitAlphanumericMiddleName(): void
{
$name = (new Parser())->parse('John A1 Smith');

$this->assertSame('John', $name->getFirstname());
$this->assertSame('A1', $name->getMiddlename());
$this->assertSame('', $name->getInitials());
$this->assertSame('John A1 Smith', $name->getFullName());
}

public function testCommaNameDoesNotPromoteDigitToFirstname(): void
{
$name = (new Parser())->parse('Smith, A1 John');

$this->assertSame('A1', $name->getFirstname());
$this->assertSame('John', $name->getMiddlename());
$this->assertSame('', $name->getInitials());
$this->assertSame('A1 John Smith', $name->getFullName());
}

public function testRealCombinedInitialsStillSplit(): void
{
foreach (['JM', 'J.M.', "JE\u{0301}"] as $token) {
$name = (new Parser())->parse('John ' . $token . ' Smith');
$expected = $token === "JE\u{0301}" ? "J E\u{0301}" : 'J M';

$this->assertSame($expected, $name->getInitials());
$this->assertSame('', $name->getMiddlename());
}
}
}
Loading