diff --git a/src/Symfony/Component/OptionsResolver/OptionsResolver.php b/src/Symfony/Component/OptionsResolver/OptionsResolver.php index 88c1e3c031..bd9f4f58b8 100644 --- a/src/Symfony/Component/OptionsResolver/OptionsResolver.php +++ b/src/Symfony/Component/OptionsResolver/OptionsResolver.php @@ -27,6 +27,26 @@ use Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException; */ class OptionsResolver implements Options { + private const VALIDATION_FUNCTIONS = [ + 'bool' => 'is_bool', + 'boolean' => 'is_bool', + 'int' => 'is_int', + 'integer' => 'is_int', + 'long' => 'is_int', + 'float' => 'is_float', + 'double' => 'is_float', + 'real' => 'is_float', + 'numeric' => 'is_numeric', + 'string' => 'is_string', + 'scalar' => 'is_scalar', + 'array' => 'is_array', + 'iterable' => 'is_iterable', + 'countable' => 'is_countable', + 'callable' => 'is_callable', + 'object' => 'is_object', + 'resource' => 'is_resource', + ]; + /** * The names of all defined options. */ @@ -110,12 +130,6 @@ class OptionsResolver implements Options private $parentsOptions = []; - private static $typeAliases = [ - 'boolean' => 'bool', - 'integer' => 'int', - 'double' => 'float', - ]; - /** * Sets the default value of a given option. * @@ -995,8 +1009,6 @@ class OptionsResolver implements Options $invalidTypes = []; foreach ($this->allowedTypes[$option] as $type) { - $type = self::$typeAliases[$type] ?? $type; - if ($valid = $this->verifyTypes($type, $value, $invalidTypes)) { break; } @@ -1007,7 +1019,7 @@ class OptionsResolver implements Options $fmtAllowedTypes = implode('" or "', $this->allowedTypes[$option]); $fmtProvidedTypes = implode('|', array_keys($invalidTypes)); $allowedContainsArrayType = \count(array_filter($this->allowedTypes[$option], static function ($item) { - return '[]' === substr(self::$typeAliases[$item] ?? $item, -2); + return '[]' === substr($item, -2); })) > 0; if (\is_array($value) && $allowedContainsArrayType) { @@ -1135,7 +1147,7 @@ class OptionsResolver implements Options return $valid; } - if (('null' === $type && null === $value) || (\function_exists($func = 'is_'.$type) && $func($value)) || $value instanceof $type) { + if (('null' === $type && null === $value) || (isset(self::VALIDATION_FUNCTIONS[$type]) ? self::VALIDATION_FUNCTIONS[$type]($value) : $value instanceof $type)) { return true; } diff --git a/src/Symfony/Component/OptionsResolver/composer.json b/src/Symfony/Component/OptionsResolver/composer.json index 4a580cb386..4c365b978a 100644 --- a/src/Symfony/Component/OptionsResolver/composer.json +++ b/src/Symfony/Component/OptionsResolver/composer.json @@ -18,6 +18,7 @@ "require": { "php": "^7.2.5", "symfony/deprecation-contracts": "^2.1", + "symfony/polyfill-php73": "~1.0", "symfony/polyfill-php80": "^1.15" }, "autoload": { diff --git a/src/Symfony/Component/Validator/Constraints/TypeValidator.php b/src/Symfony/Component/Validator/Constraints/TypeValidator.php index 99f3d9c630..0a938c6d95 100644 --- a/src/Symfony/Component/Validator/Constraints/TypeValidator.php +++ b/src/Symfony/Component/Validator/Constraints/TypeValidator.php @@ -20,6 +20,38 @@ use Symfony\Component\Validator\Exception\UnexpectedTypeException; */ class TypeValidator extends ConstraintValidator { + private const VALIDATION_FUNCTIONS = [ + 'bool' => 'is_bool', + 'boolean' => 'is_bool', + 'int' => 'is_int', + 'integer' => 'is_int', + 'long' => 'is_int', + 'float' => 'is_float', + 'double' => 'is_float', + 'real' => 'is_float', + 'numeric' => 'is_numeric', + 'string' => 'is_string', + 'scalar' => 'is_scalar', + 'array' => 'is_array', + 'iterable' => 'is_iterable', + 'countable' => 'is_countable', + 'callable' => 'is_callable', + 'object' => 'is_object', + 'resource' => 'is_resource', + 'null' => 'is_null', + 'alnum' => 'ctype_alnum', + 'alpha' => 'ctype_alpha', + 'cntrl' => 'ctype_cntrl', + 'digit' => 'ctype_digit', + 'graph' => 'ctype_graph', + 'lower' => 'ctype_lower', + 'print' => 'ctype_print', + 'punct' => 'ctype_punct', + 'space' => 'ctype_space', + 'upper' => 'ctype_upper', + 'xdigit' => 'ctype_xdigit', + ]; + /** * {@inheritdoc} */ @@ -37,14 +69,11 @@ class TypeValidator extends ConstraintValidator foreach ($types as $type) { $type = strtolower($type); - $type = 'boolean' === $type ? 'bool' : $type; - $isFunction = 'is_'.$type; - $ctypeFunction = 'ctype_'.$type; - if (\function_exists($isFunction) && $isFunction($value)) { + if (isset(self::VALIDATION_FUNCTIONS[$type]) && self::VALIDATION_FUNCTIONS[$type]($value)) { return; - } elseif (\function_exists($ctypeFunction) && $ctypeFunction($value)) { - return; - } elseif ($value instanceof $type) { + } + + if ($value instanceof $type) { return; } } diff --git a/src/Symfony/Component/Validator/composer.json b/src/Symfony/Component/Validator/composer.json index 0ed2945d3c..6938592775 100644 --- a/src/Symfony/Component/Validator/composer.json +++ b/src/Symfony/Component/Validator/composer.json @@ -19,6 +19,7 @@ "php": "^7.2.5", "symfony/polyfill-ctype": "~1.8", "symfony/polyfill-mbstring": "~1.0", + "symfony/polyfill-php73": "~1.0", "symfony/polyfill-php80": "^1.15", "symfony/translation-contracts": "^1.1|^2" },