27
27
*/
28
28
class OptionsResolver implements Options
29
29
{
30
+ private const VALIDATION_FUNCTIONS = [
31
+ 'bool ' => 'is_bool ' ,
32
+ 'boolean ' => 'is_bool ' ,
33
+ 'int ' => 'is_int ' ,
34
+ 'integer ' => 'is_int ' ,
35
+ 'long ' => 'is_int ' ,
36
+ 'float ' => 'is_float ' ,
37
+ 'double ' => 'is_float ' ,
38
+ 'real ' => 'is_float ' ,
39
+ 'numeric ' => 'is_numeric ' ,
40
+ 'string ' => 'is_string ' ,
41
+ 'scalar ' => 'is_scalar ' ,
42
+ 'array ' => 'is_array ' ,
43
+ 'iterable ' => 'is_iterable ' ,
44
+ 'countable ' => 'is_countable ' ,
45
+ 'callable ' => 'is_callable ' ,
46
+ 'object ' => 'is_object ' ,
47
+ 'resource ' => 'is_resource ' ,
48
+ ];
49
+
30
50
/**
31
51
* The names of all defined options.
32
52
*/
@@ -110,12 +130,6 @@ class OptionsResolver implements Options
110
130
111
131
private $ parentsOptions = [];
112
132
113
- private static $ typeAliases = [
114
- 'boolean ' => 'bool ' ,
115
- 'integer ' => 'int ' ,
116
- 'double ' => 'float ' ,
117
- ];
118
-
119
133
/**
120
134
* Sets the default value of a given option.
121
135
*
@@ -995,8 +1009,6 @@ public function offsetGet($option, bool $triggerDeprecation = true)
995
1009
$ invalidTypes = [];
996
1010
997
1011
foreach ($ this ->allowedTypes [$ option ] as $ type ) {
998
- $ type = self ::$ typeAliases [$ type ] ?? $ type ;
999
-
1000
1012
if ($ valid = $ this ->verifyTypes ($ type , $ value , $ invalidTypes )) {
1001
1013
break ;
1002
1014
}
@@ -1007,7 +1019,7 @@ public function offsetGet($option, bool $triggerDeprecation = true)
1007
1019
$ fmtAllowedTypes = implode ('" or " ' , $ this ->allowedTypes [$ option ]);
1008
1020
$ fmtProvidedTypes = implode ('| ' , array_keys ($ invalidTypes ));
1009
1021
$ allowedContainsArrayType = \count (array_filter ($ this ->allowedTypes [$ option ], static function ($ item ) {
1010
- return '[] ' === substr (self :: $ typeAliases [ $ item ] ?? $ item , -2 );
1022
+ return '[] ' === substr ($ item , -2 );
1011
1023
})) > 0 ;
1012
1024
1013
1025
if (\is_array ($ value ) && $ allowedContainsArrayType ) {
@@ -1135,7 +1147,7 @@ private function verifyTypes(string $type, $value, array &$invalidTypes, int $le
1135
1147
return $ valid ;
1136
1148
}
1137
1149
1138
- if (('null ' === $ type && null === $ value ) || (\function_exists ( $ func = ' is_ ' . $ type) && $ func ($ value )) || $ value instanceof $ type ) {
1150
+ if (('null ' === $ type && null === $ value ) || (isset ( self :: VALIDATION_FUNCTIONS [ $ type]) ? self :: VALIDATION_FUNCTIONS [ $ type ] ($ value ) : $ value instanceof $ type) ) {
1139
1151
return true ;
1140
1152
}
1141
1153
0 commit comments