feature #12602 Add type aliases for allowed types in OptionsResolver (henrikbjorn)
This PR was submitted for the 2.3 branch but it was merged into the 2.7 branch instead (closes #12602).
Discussion
----------
Add type aliases for allowed types in OptionsResolver
| Q | A
| ------------- | ---
| Bug fix? | [yes]
| New feature? | [no]
| BC breaks? | [no]
| Deprecations? | [no]
| Tests pass? | [yes]
| Fixed tickets | [#12586]
| License | MIT
Improves DX by allowing type names that does not correspond 1 to 1
with php is_ functions.
integer => int
boolean => bool
double => float
(have one for 2.6 branch aswell, but cant never remember what branch i am supposed to base stuff off)
Commits
-------
ae544e5
Add type aliases for allowed types in OptionsResolver
This commit is contained in:
commit
a0e3757bf0
@ -110,6 +110,12 @@ class OptionsResolver implements Options, OptionsResolverInterface
|
||||
*/
|
||||
private $locked = false;
|
||||
|
||||
private static $typeAliases = array(
|
||||
'boolean' => 'bool',
|
||||
'integer' => 'int',
|
||||
'double' => 'float',
|
||||
);
|
||||
|
||||
/**
|
||||
* Sets the default value of a given option.
|
||||
*
|
||||
@ -844,6 +850,8 @@ class OptionsResolver implements Options, OptionsResolverInterface
|
||||
$valid = false;
|
||||
|
||||
foreach ($this->allowedTypes[$option] as $type) {
|
||||
$type = isset(self::$typeAliases[$type]) ? self::$typeAliases[$type] : $type;
|
||||
|
||||
if (function_exists($isFunction = 'is_'.$type)) {
|
||||
if ($isFunction($value)) {
|
||||
$valid = true;
|
||||
|
@ -78,6 +78,21 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
|
||||
), $this->resolver->resolve(array()));
|
||||
}
|
||||
|
||||
public function testTypeAliasesForAllowedTypes()
|
||||
{
|
||||
$this->resolver->setDefaults(array(
|
||||
'force' => false,
|
||||
));
|
||||
|
||||
$this->resolver->setAllowedTypes(array(
|
||||
'force' => 'boolean',
|
||||
));
|
||||
|
||||
$this->resolver->resolve(array(
|
||||
'force' => true,
|
||||
));
|
||||
}
|
||||
|
||||
public function testResolveLazyDependencyOnOptional()
|
||||
{
|
||||
$this->resolver->setDefaults(array(
|
||||
|
Reference in New Issue
Block a user