Skip to content

Commit

Permalink
try new regex
Browse files Browse the repository at this point in the history
  • Loading branch information
goetas committed Sep 8, 2023
1 parent 5c90075 commit 5e5f026
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
18 changes: 14 additions & 4 deletions src/Jms/YamlValidatorConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,20 @@ private function loadValidatorType(array &$property, Type $type, $arrayized = fa
foreach ($check as $item) {
// initial support for https://www.w3.org/TR/xsd-unicode-blocknames/
// not supported by standard php regex implementation
$regexPattern = strtr($item['value'], [
'\p{IsBasicLatin}' => '\p{Latin}',
'\p{IsLatin-1Supplement}' => '\p{S}',
]);
// \p{IsBasicLatin} represents a range, but the expression might be alraedy in a range,
// so we try our best and detect it if is in a range or not
$regexPattern = $item['value'];
$unicodeClasses = [
'\p{IsBasicLatin}' => '\x{0000}-\x{007F}',
'\p{IsLatin-1Supplement}' => '\x{0080}-\x{00FF}',
];
foreach ($unicodeClasses as $from => $to) {
if (preg_match('~\[.*'.preg_quote($from, '~').'.*\]~', $regexPattern)) {
$regexPattern = str_replace($from, $to, $regexPattern);
} else {
$regexPattern = str_replace($from, "[$to]", $regexPattern);
}
}
$rules[] = [
'Regex' => ['pattern' => "~{$regexPattern}~u"],
];
Expand Down
15 changes: 13 additions & 2 deletions tests/Converter/Validator/Xsd2ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,22 @@ public function getRestrictionsValidations()
],
// pattern / Regex
[
'<xs:pattern value="\\p{IsBasicLatin}\\p{IsLatin-1Supplement}"/>',
'<xs:pattern value="\\p{IsBasicLatin}+\\p{IsLatin-1Supplement}"/>',
[
[
'Regex' => [ // adds [] parenthesis
'pattern' => '~[\x{0000}-\x{007F}]+[\x{0080}-\x{00FF}]~u',
'groups' => ['xsd_rules'],
],
],
],
],
[
'<xs:pattern value="[A-Z\\p{IsBasicLatin}\\p{IsLatin-1Supplement}]"/>',
[
[
'Regex' => [
'pattern' => '~\p{Latin}\p{S}~u',
'pattern' => '~[\x{0000}-\x{007F}\x{0080}-\x{00FF}]~u',
'groups' => ['xsd_rules'],
],
],
Expand Down

0 comments on commit 5e5f026

Please sign in to comment.