Skip to content

Commit 3742c66

Browse files
committed
Handle different cases of suffixes properly in splitname
1 parent fabd4e4 commit 3742c66

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/AdvStr.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,11 @@ public static function splitName($name)
198198
'IX',
199199
'X',
200200
];
201+
202+
// Add lower case version of the prefixes while keeping the original versions
203+
$prefixes = array_merge($prefixes, array_map('strtolower', $prefixes), array_map('strtoupper', $prefixes));
204+
$suffixes = array_merge($suffixes, array_map('strtolower', $suffixes), array_map('strtoupper', $suffixes));
205+
201206
// Remove any matching prefixes
202207
foreach ($prefixes as $prefix) {
203208
if (stripos($name, $prefix) === 0) { // Check if the prefix is at the beginning

tests/SplitNameTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,27 @@
4444
->and($name['first'])->toBe('John')
4545
->and($name['last'])->toBe('Smith');
4646
});
47+
48+
test('Dr. Matthew Paul Stenson Jr. is parsed correctly', function () {
49+
$name = AdvStr::splitName('dr. matthew paul stenson jr.');
50+
expect($name)->toHaveCount(3)
51+
->and($name['first'])->toBe('matthew')
52+
->and($name['middle'])->toBe('paul')
53+
->and($name['last'])->toBe('stenson');
54+
});
55+
56+
test('Two prefixes are parsed correctly', function () {
57+
$name = AdvStr::splitName('Rev Dr. matthew paul stenson jr.');
58+
expect($name)->toHaveCount(3)
59+
->and($name['first'])->toBe('matthew')
60+
->and($name['middle'])->toBe('paul')
61+
->and($name['last'])->toBe('stenson');
62+
});
63+
64+
test('Catch all caps versions', function () {
65+
$name = AdvStr::splitName('REV DR. MATTHEW PAUL STENSON JR.');
66+
expect($name)->toHaveCount(3)
67+
->and($name['first'])->toBe('MATTHEW')
68+
->and($name['middle'])->toBe('PAUL')
69+
->and($name['last'])->toBe('STENSON');
70+
});

0 commit comments

Comments
 (0)