Skip to content

Commit 1adc0d6

Browse files
committed
Use RFC3066 instead of the obsoleted RFC1766
1 parent c98db08 commit 1adc0d6

2 files changed

Lines changed: 25 additions & 4 deletions

File tree

src/XML/Assert/LanguageTrait.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,26 @@
1111
*/
1212
trait LanguageTrait
1313
{
14-
private static string $language_regex = '/^([a-z]{2}|[i]-[a-z]+|[x]-[a-z]{1,8})(-[a-z]{1,8})*$/Di';
14+
/**
15+
* BCP 47 language tag validator (RFC 5646)
16+
* - Full syntax validation including grandfathered and private-use tags
17+
* - Named capture groups for easy subtag extraction
18+
* - 'x' flag + comments + whitespace for maximum readability
19+
* - Case-insensitive (/i)
20+
*/
21+
private static string $language_regex = '/^
22+
(?:
23+
(
24+
en-GB-oed|i-ami|i-bnn|i-default|i-enochian|i-hak|i-klingon|i-lux
25+
|i-mingo|i-navajo|i-pwn|i-tao|i-tay|i-tsu|sgn-BE-FR|sgn-BE-NL|sgn-CH-DE
26+
)
27+
|(art-lojban|cel-gaulish|no-bok|no-nyn|zh-guoyu|zh-hakka|zh-min|zh-min-nan|zh-xiang)
28+
)$
29+
|^((?:[a-z]{2,3}(?:(?:-[a-z]{3}){1,3})?)|[a-z]{4}|[a-z]{5,8})(?:-([a-z]{4}))?(?:-([a-z]{2}|\d{3}))
30+
?((?:-(?:[\da-z]{5,8}|\d[\da-z]{3}))*)
31+
?((?:-[\da-wy-z](?:-[\da-z]{2,8})+)*)
32+
?(-x(?:-[\da-z]{1,8})+)?$
33+
|^(x(?:-[\da-z]{1,8})+)$/mxi';
1534

1635

1736
/**

tests/XML/Assert/LanguageTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ public static function provideValidLanguage(): array
4343
return [
4444
'one part' => [true, 'es'],
4545
'two parts' => [true, 'en-US'],
46-
'many parts' => [true, 'es-this-goes-on-forever'],
47-
'x-case' => [true, 'x-klingon'],
48-
'i-case' => [true, 'i-sami-no'],
46+
'many parts' => [true, 'tlh-Kore-AQ-fonipa'],
47+
'three-letter primary code' => [true, 'nso'],
48+
'x-case' => [true, 'x-my-custom-language'],
49+
'i-case' => [true, 'i-klingon'],
4950
];
5051
}
5152

@@ -59,6 +60,7 @@ public static function provideInvalidLanguage(): array
5960
'empty string' => [false, ''],
6061
'whitespace' => [false, 'en- us'],
6162
'too long' => [false, 'toolongLanguageCode'],
63+
'not-grandfathered' => [false, 'i-sami-no'],
6264
];
6365
}
6466
}

0 commit comments

Comments
 (0)