Skip to content

Commit 5e5f026

Browse files
committed
try new regex
1 parent 5c90075 commit 5e5f026

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

src/Jms/YamlValidatorConverter.php

+14-4
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,20 @@ private function loadValidatorType(array &$property, Type $type, $arrayized = fa
145145
foreach ($check as $item) {
146146
// initial support for https://www.w3.org/TR/xsd-unicode-blocknames/
147147
// not supported by standard php regex implementation
148-
$regexPattern = strtr($item['value'], [
149-
'\p{IsBasicLatin}' => '\p{Latin}',
150-
'\p{IsLatin-1Supplement}' => '\p{S}',
151-
]);
148+
// \p{IsBasicLatin} represents a range, but the expression might be alraedy in a range,
149+
// so we try our best and detect it if is in a range or not
150+
$regexPattern = $item['value'];
151+
$unicodeClasses = [
152+
'\p{IsBasicLatin}' => '\x{0000}-\x{007F}',
153+
'\p{IsLatin-1Supplement}' => '\x{0080}-\x{00FF}',
154+
];
155+
foreach ($unicodeClasses as $from => $to) {
156+
if (preg_match('~\[.*'.preg_quote($from, '~').'.*\]~', $regexPattern)) {
157+
$regexPattern = str_replace($from, $to, $regexPattern);
158+
} else {
159+
$regexPattern = str_replace($from, "[$to]", $regexPattern);
160+
}
161+
}
152162
$rules[] = [
153163
'Regex' => ['pattern' => "~{$regexPattern}~u"],
154164
];

tests/Converter/Validator/Xsd2ValidatorTest.php

+13-2
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,22 @@ public function getRestrictionsValidations()
126126
],
127127
// pattern / Regex
128128
[
129-
'<xs:pattern value="\\p{IsBasicLatin}\\p{IsLatin-1Supplement}"/>',
129+
'<xs:pattern value="\\p{IsBasicLatin}+\\p{IsLatin-1Supplement}"/>',
130+
[
131+
[
132+
'Regex' => [ // adds [] parenthesis
133+
'pattern' => '~[\x{0000}-\x{007F}]+[\x{0080}-\x{00FF}]~u',
134+
'groups' => ['xsd_rules'],
135+
],
136+
],
137+
],
138+
],
139+
[
140+
'<xs:pattern value="[A-Z\\p{IsBasicLatin}\\p{IsLatin-1Supplement}]"/>',
130141
[
131142
[
132143
'Regex' => [
133-
'pattern' => '~\p{Latin}\p{S}~u',
144+
'pattern' => '~[\x{0000}-\x{007F}\x{0080}-\x{00FF}]~u',
134145
'groups' => ['xsd_rules'],
135146
],
136147
],

0 commit comments

Comments
 (0)