Skip to content

Commit

Permalink
Merge pull request #159 from koriym/php84
Browse files Browse the repository at this point in the history
Fix empty domain handling in Email validation and add PHP 8.3/8.4 support
  • Loading branch information
harikt authored Nov 29, 2024
2 parents e19f465 + f1d18ea commit 9eb7616
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ jobs:
- '8.0'
- '8.1'
- '8.2'
- '8.3'
- '8.4'
steps:
- name: Checkout
uses: actions/checkout@v1
Expand Down
8 changes: 4 additions & 4 deletions src/Rule/AbstractStrlen.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ protected function strlen(string $str): int
return strlen(utf8_to_iso8859_1($str));
}

/**
/**
*
* Wrapper for `iconv_substr()` to throw an exception on malformed UTF-8.
*
Expand All @@ -87,10 +87,10 @@ protected function strlen(string $str): int
* @throws Exception\MalformedUtf8
*
*/
protected function substrIconv(string $str,int $start,int $length)
protected function substrIconv(string $str, int $start, ?int $length = null): string
{
$level = error_reporting(0);
$substr = iconv_substr($str,$start,$length, 'UTF-8');
$substr = iconv_substr($str, $start, $length ?? 0, 'UTF-8');
error_reporting($level);

if ($substr !== false) {
Expand All @@ -111,7 +111,7 @@ protected function substrIconv(string $str,int $start,int $length)
* @throws Exception\MalformedUtf8
*
*/
protected function strlenIconv(string $str)
protected function strlenIconv(string $str): int
{
$level = error_reporting(0);
$strlen = iconv_strlen($str, 'UTF-8');
Expand Down
19 changes: 14 additions & 5 deletions src/Rule/Validate/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,15 +404,24 @@ protected function intl(): bool
*/
protected function idnToAscii(string $email): string
{
if (strpos($email, '@') === false) {
return $email;
}

$parts = explode('@', $email);
$domain = array_pop($parts);
if (! $parts) {
// no parts remaining, so no @ symbol, so not valid to begin with

if (!isset($parts[1]) || empty($parts[1])) {
return $email;
}

// put the parts back together, with the domain part converted to ascii
return implode('@', $parts) . '@' . idn_to_ascii($domain, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46);
$domain = array_pop($parts);
$localPart = implode('@', $parts);

$asciiDomain = idn_to_ascii($domain, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46);
if ($asciiDomain === false) {
return $email;
}
return $localPart . '@' . $asciiDomain;
}

/**
Expand Down

0 comments on commit 9eb7616

Please sign in to comment.