Skip to content

Commit 961d61e

Browse files
committed
Add and use public normalize_name() function
1 parent 44962c9 commit 961d61e

File tree

3 files changed

+29
-28
lines changed

3 files changed

+29
-28
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"autoload": {
1010
"psr-4": {
1111
"LibDNS\\": "src/"
12-
}
12+
},
13+
"files": ["src/functions.php"]
1314
},
1415
"support": {
1516
"issues": "https://github.com/DaveRandom/LibDNS/issues"

src/Records/Types/DomainName.php

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@ class DomainName extends Type
2424
{
2525
const FLAG_NO_COMPRESSION = 0x80000000;
2626

27-
/**
28-
* @var callable|null
29-
*/
30-
private static $labelPreProcessor = null;
31-
3227
/**
3328
* @var string
3429
*/
@@ -47,27 +42,6 @@ class DomainName extends Type
4742
*/
4843
public function __construct($value = null)
4944
{
50-
if (!isset(self::$labelPreProcessor)) {
51-
self::$labelPreProcessor = \function_exists('idn_to_ascii')
52-
? static function($label) {
53-
if (false === $result = \idn_to_ascii($label, 0, INTL_IDNA_VARIANT_UTS46)) {
54-
throw new \InvalidArgumentException("Label '{$label}' could not be processed for IDN");
55-
}
56-
57-
return $result;
58-
}
59-
: static function($label) {
60-
if (\preg_match('/[\x80-\xff]/', $label)) {
61-
throw new \RuntimeException(
62-
"Label '{$label}' contains non-ASCII characters and IDN support is not available."
63-
. " Verify that ext/intl is installed for IDN support."
64-
);
65-
}
66-
67-
return \strtolower($label);
68-
};
69-
}
70-
7145
if (\is_array($value)) {
7246
$this->setLabels($value);
7347
} else {
@@ -113,7 +87,7 @@ public function setLabels(array $labels, $tldFirst = false)
11387
$length = $count = 0;
11488

11589
foreach ($labels as &$label) {
116-
$label = (self::$labelPreProcessor)($label);
90+
$label = \LibDNS\normalize_name($label);
11791
$labelLength = \strlen($label);
11892
if ($labelLength > 63) {
11993
throw new \InvalidArgumentException('Label list is not a valid domain name: Label ' . $label . ' length exceeds 63 byte limit');

src/functions.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace LibDNS;
4+
5+
if (\function_exists('idn_to_ascii')) {
6+
function normalize_name(string $label): string
7+
{
8+
if (false === $result = \idn_to_ascii($label, 0, INTL_IDNA_VARIANT_UTS46)) {
9+
throw new \InvalidArgumentException("Label '{$label}' could not be processed for IDN");
10+
}
11+
12+
return $result;
13+
}
14+
} else {
15+
function normalize_name(string $label): string
16+
{
17+
if (\preg_match('/[\x80-\xff]/', $label)) {
18+
throw new \InvalidArgumentException(
19+
"Label '{$label}' contains non-ASCII characters and IDN support is not available."
20+
. " Verify that ext/intl is installed for IDN support."
21+
);
22+
}
23+
24+
return \strtolower($label);
25+
}
26+
}

0 commit comments

Comments
 (0)