Add type aliases for allowed types in OptionsResolver

This commit is contained in:
Henrik Bjornskov 2014-11-29 10:15:31 +01:00 committed by Fabien Potencier
parent 193d69c4d1
commit ae544e5d8f
2 changed files with 23 additions and 0 deletions

View File

@ -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;

View File

@ -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(