Skip to content

Commit adcfe64

Browse files
authored
Merge pull request #10 from samsonasik/handle-no-letter
[validator] Validate no letter
2 parents 247f5d1 + 3b45c06 commit adcfe64

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Validation checks
4242

4343
- Allowed characters: letters, hyphens, apostrophe, spaces, full stops.
4444
- Not allowed:
45+
- no letter, eg: ` - `
4546
- include number
4647
- special characters
4748
- single `.` character

spec/Validator/NamingSpec.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
it('returns false for incorrect naming', function (): void {
1414

1515
$namings = [
16+
// no letter
17+
' ',
18+
' ',
19+
' - ',
20+
'- -',
21+
1622
// include special character(s)
1723
'~~',
1824
'abdul%',
@@ -38,6 +44,9 @@
3844
"''",
3945
'...',
4046
'Foo.....',
47+
48+
// invalid characters
49+
'<>'
4150
];
4251

4352
foreach ($namings as $naming) {
@@ -57,6 +66,7 @@
5766
"D'Lilah",
5867
'Veli-Matti',
5968
'Setälä',
69+
'X Æ A-Xii',
6070
];
6171

6272
foreach ($namings as $naming) {

src/Validator/Naming.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616

1717
final class Naming extends AbstractValidator
1818
{
19+
/**
20+
* @var string
21+
*/
22+
private const MUST_CONTAIN_LETTER = 'MUST_CONTAIN_LETTER';
23+
1924
/**
2025
* @var string
2126
*/
@@ -58,6 +63,7 @@ final class Naming extends AbstractValidator
5863

5964
/** @var array<string, string> */
6065
protected array $messageTemplates = [
66+
self::MUST_CONTAIN_LETTER => 'Name must contain at least one letter',
6167
self::SPECIAL_OR_NUMBER => 'Names can contain only letters, hyphens, apostrophe, spaces & full stops',
6268
self::SINGLE_DOT => 'Single "." character is not allowed',
6369
self::SINGLE_HYPHEN => 'Single "-" character is not allowed',
@@ -81,7 +87,7 @@ public function isValid(mixed $value): bool
8187
return false;
8288
}
8389

84-
$length = mb_strlen($value);
90+
$length = mb_strlen($value, 'UTF-8');
8591
if ($length === 1) {
8692
$messageTemplates = [
8793
'.' => self::SINGLE_DOT,
@@ -116,6 +122,11 @@ public function isValid(mixed $value): bool
116122
return false;
117123
}
118124

125+
if (! preg_match('#\p{L}#u', $value)) {
126+
$this->error(self::MUST_CONTAIN_LETTER);
127+
return false;
128+
}
129+
119130
return true;
120131
}
121132
}

0 commit comments

Comments
 (0)