Skip to content

Commit

Permalink
Merge pull request #24 from wsssoftware/personal-name-stringable
Browse files Browse the repository at this point in the history
Personal name stringable formatation
  • Loading branch information
allanmcarvalho authored Nov 4, 2024
2 parents ebdfa1a + c1b564f commit a08e186
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/Macros/StrMacro.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class StrMacro
public function __invoke(): void
{
$this->applyMask();
$this->personalName();
}

public function applyMask(): void
Expand Down Expand Up @@ -65,4 +66,32 @@ public function applyMask(): void
return new Stringable(Str::applyMask($this->value, $mask));
});
}

public function personalName(): void
{
Str::macro('personalName', function (string $name): string {
$toIgnore = [
'da', 'de', 'di', 'do', 'du',
'das', 'des', 'dis', 'dos', 'dus',
'a', 'e', 'i', 'o', 'u',
'as', 'es', 'is', 'os', 'us',
'd’',
];
$words = str($name)->deduplicate()
->explode(' ')
->map(function (string $word) use ($toIgnore) {
$word = mb_strtolower($word);
if (! in_array($word, $toIgnore)) {
$word = mb_strtoupper(mb_substr($word, 0, 1)).mb_substr($word, 1);
}

return $word;
});

return $words->implode(' ');
});
Stringable::macro('personalName', function (): Stringable {
return new Stringable(Str::personalName($this->value));
});
}
}
11 changes: 11 additions & 0 deletions tests/Macros/StrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,14 @@
->toEqual('12');

});

it('can format personal name', function () {
expect(Str::personalName('ALLAN MARIUCCI DE CARVALHO'))
->toEqual('Allan Mariucci de Carvalho')
->and(str('ALLAN MARIUCCI DE CARVALHO')->personalName())
->toEqual('Allan Mariucci de Carvalho')
->and(Str::personalName('luiz ap. de carvalho'))
->toEqual('Luiz Ap. de Carvalho')
->and(str('Luiz ap. De Carvalho')->personalName())
->toEqual('Luiz Ap. de Carvalho');
});

0 comments on commit a08e186

Please sign in to comment.