Skip to content

Commit 9eb7616

Browse files
authored
Merge pull request #159 from koriym/php84
Fix empty domain handling in Email validation and add PHP 8.3/8.4 support
2 parents e19f465 + f1d18ea commit 9eb7616

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

.github/workflows/continuous-integration.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ jobs:
2121
- '8.0'
2222
- '8.1'
2323
- '8.2'
24+
- '8.3'
25+
- '8.4'
2426
steps:
2527
- name: Checkout
2628
uses: actions/checkout@v1

src/Rule/AbstractStrlen.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ protected function strlen(string $str): int
7272
return strlen(utf8_to_iso8859_1($str));
7373
}
7474

75-
/**
75+
/**
7676
*
7777
* Wrapper for `iconv_substr()` to throw an exception on malformed UTF-8.
7878
*
@@ -87,10 +87,10 @@ protected function strlen(string $str): int
8787
* @throws Exception\MalformedUtf8
8888
*
8989
*/
90-
protected function substrIconv(string $str,int $start,int $length)
90+
protected function substrIconv(string $str, int $start, ?int $length = null): string
9191
{
9292
$level = error_reporting(0);
93-
$substr = iconv_substr($str,$start,$length, 'UTF-8');
93+
$substr = iconv_substr($str, $start, $length ?? 0, 'UTF-8');
9494
error_reporting($level);
9595

9696
if ($substr !== false) {
@@ -111,7 +111,7 @@ protected function substrIconv(string $str,int $start,int $length)
111111
* @throws Exception\MalformedUtf8
112112
*
113113
*/
114-
protected function strlenIconv(string $str)
114+
protected function strlenIconv(string $str): int
115115
{
116116
$level = error_reporting(0);
117117
$strlen = iconv_strlen($str, 'UTF-8');

src/Rule/Validate/Email.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -404,15 +404,24 @@ protected function intl(): bool
404404
*/
405405
protected function idnToAscii(string $email): string
406406
{
407+
if (strpos($email, '@') === false) {
408+
return $email;
409+
}
410+
407411
$parts = explode('@', $email);
408-
$domain = array_pop($parts);
409-
if (! $parts) {
410-
// no parts remaining, so no @ symbol, so not valid to begin with
412+
413+
if (!isset($parts[1]) || empty($parts[1])) {
411414
return $email;
412415
}
413416

414-
// put the parts back together, with the domain part converted to ascii
415-
return implode('@', $parts) . '@' . idn_to_ascii($domain, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46);
417+
$domain = array_pop($parts);
418+
$localPart = implode('@', $parts);
419+
420+
$asciiDomain = idn_to_ascii($domain, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46);
421+
if ($asciiDomain === false) {
422+
return $email;
423+
}
424+
return $localPart . '@' . $asciiDomain;
416425
}
417426

418427
/**

0 commit comments

Comments
 (0)