diff --git a/src/Jms/YamlValidatorConverter.php b/src/Jms/YamlValidatorConverter.php index ba0c764..0847a78 100644 --- a/src/Jms/YamlValidatorConverter.php +++ b/src/Jms/YamlValidatorConverter.php @@ -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"], ]; diff --git a/tests/Converter/Validator/Xsd2ValidatorTest.php b/tests/Converter/Validator/Xsd2ValidatorTest.php index 985c7e0..bdf5e04 100644 --- a/tests/Converter/Validator/Xsd2ValidatorTest.php +++ b/tests/Converter/Validator/Xsd2ValidatorTest.php @@ -126,11 +126,22 @@ public function getRestrictionsValidations() ], // pattern / Regex [ - '', + '', + [ + [ + 'Regex' => [ // adds [] parenthesis + 'pattern' => '~[\x{0000}-\x{007F}]+[\x{0080}-\x{00FF}]~u', + 'groups' => ['xsd_rules'], + ], + ], + ], + ], + [ + '', [ [ 'Regex' => [ - 'pattern' => '~\p{Latin}\p{S}~u', + 'pattern' => '~[\x{0000}-\x{007F}\x{0080}-\x{00FF}]~u', 'groups' => ['xsd_rules'], ], ],