Skip to content

Commit d4284d8

Browse files
authored
Merge pull request #8 from samsonasik/ensure-letter
Fix: Ensure the letter after apostrophe/hyphen is uppercase (Unicode-safe)
2 parents 9c655f6 + 2bd68ea commit d4284d8

File tree

3 files changed

+19
-20
lines changed

3 files changed

+19
-20
lines changed

rector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use Rector\Config\RectorConfig;
55

66
return RectorConfig::configure()
7-
->withPreparedSets(codingStyle: true, codeQuality: true, typeDeclarations: true)
7+
->withPreparedSets(codeQuality: true, codingStyle: true, typeDeclarations: true)
88
->withPhpSets(php81: true)
99
->withPaths([__DIR__ . '/src', __DIR__ . '/spec'])
1010
->withRootFiles()

spec/Filter/NamingSpec.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@
1111
it('set striptags, trim, and strip double space with ucwords with set lower first, upper after apostrophe and hyphen', function (): void {
1212

1313
$maps = [
14-
'<script>Abdul' => 'Abdul',
15-
'Abdul ' => 'Abdul',
16-
'ABduL' => 'Abdul',
17-
'aBDUL m. ikHsan' => 'Abdul M. Ikhsan',
18-
'abdul Malik I' => 'Abdul Malik I',
19-
"D'lilah" => "D'Lilah",
20-
'äX' => 'Äx',
21-
'Veli-matti' => 'Veli-Matti',
14+
'' => '',
15+
'<script>Abdul' => 'Abdul',
16+
'Abdul ' => 'Abdul',
17+
'ABduL' => 'Abdul',
18+
'aBDUL m. ikHsan' => 'Abdul M. Ikhsan',
19+
'abdul Malik I' => 'Abdul Malik I',
20+
"D'lilah" => "D'Lilah",
21+
'äX' => 'Äx',
22+
'Veli-matti' => 'Veli-Matti',
23+
"d'äx" => "D'Äx",
24+
'anna-maria-louise' => 'Anna-Maria-Louise',
2225
];
2326

2427
$naming = new Naming();

src/Filter/Naming.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@
99
use Laminas\Filter\StripTags;
1010
use Webmozart\Assert\Assert;
1111

12-
use function array_walk;
1312
use function mb_convert_case;
14-
use function mb_strpos;
1513
use function mb_strtoupper;
1614
use function preg_replace;
15+
use function preg_replace_callback;
1716

1817
use const MB_CASE_TITLE;
1918

@@ -32,15 +31,12 @@ public function filter(mixed $value): string
3231
$value = mb_convert_case($value, MB_CASE_TITLE, 'UTF-8');
3332
$value = preg_replace('#\s{2,}#', ' ', $value);
3433

35-
$chars = ["'", '-'];
36-
array_walk($chars, static function ($row) use (&$value): void {
37-
Assert::string($value);
38-
39-
$position = mb_strpos($value, $row);
40-
if ($position !== false && isset($value[$position + 1])) {
41-
$value[$position + 1] = mb_strtoupper($value[$position + 1]);
42-
}
43-
});
34+
// Ensure the letter after apostrophe/hyphen is uppercase (Unicode-safe)
35+
$value = preg_replace_callback(
36+
"/(['-])(\\p{L})/u",
37+
static fn(array $matches): string => $matches[1] . mb_strtoupper($matches[2], 'UTF-8'),
38+
(string) $value
39+
);
4440

4541
Assert::string($value);
4642
return $value;

0 commit comments

Comments
 (0)