[Config] type specific check for emptiness
This commit is contained in:
parent
6bcdee057e
commit
0199fbf545
@ -36,4 +36,13 @@ class BooleanNode extends ScalarNode
|
|||||||
throw $ex;
|
throw $ex;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function isValueEmpty($value)
|
||||||
|
{
|
||||||
|
// a boolean value cannot be empty
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -52,4 +52,13 @@ class NumericNode extends ScalarNode
|
|||||||
|
|
||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function isValueEmpty($value)
|
||||||
|
{
|
||||||
|
// a numeric value cannot be empty
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -43,4 +43,12 @@ class ScalarNode extends VariableNode
|
|||||||
throw $ex;
|
throw $ex;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function isValueEmpty($value)
|
||||||
|
{
|
||||||
|
return null === $value || '' === $value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -84,7 +84,7 @@ class VariableNode extends BaseNode implements PrototypeNodeInterface
|
|||||||
*/
|
*/
|
||||||
protected function finalizeValue($value)
|
protected function finalizeValue($value)
|
||||||
{
|
{
|
||||||
if (!$this->allowEmptyValue && empty($value)) {
|
if (!$this->allowEmptyValue && $this->isValueEmpty($value)) {
|
||||||
$ex = new InvalidConfigurationException(sprintf(
|
$ex = new InvalidConfigurationException(sprintf(
|
||||||
'The path "%s" cannot contain an empty value, but got %s.',
|
'The path "%s" cannot contain an empty value, but got %s.',
|
||||||
$this->getPath(),
|
$this->getPath(),
|
||||||
@ -113,4 +113,20 @@ class VariableNode extends BaseNode implements PrototypeNodeInterface
|
|||||||
{
|
{
|
||||||
return $rightSide;
|
return $rightSide;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Evaluates if the given value is to be treated as empty.
|
||||||
|
*
|
||||||
|
* By default, PHP's empty() function is used to test for emptiness. This
|
||||||
|
* method may be overridden by subtypes to better match their understanding
|
||||||
|
* of empty data.
|
||||||
|
*
|
||||||
|
* @param mixed $value
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
protected function isValueEmpty($value)
|
||||||
|
{
|
||||||
|
return empty($value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,6 +24,19 @@ class BooleanNodeTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertSame($value, $node->normalize($value));
|
$this->assertSame($value, $node->normalize($value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider getValidValues
|
||||||
|
*
|
||||||
|
* @param bool $value
|
||||||
|
*/
|
||||||
|
public function testValidNonEmptyValues($value)
|
||||||
|
{
|
||||||
|
$node = new BooleanNode('test');
|
||||||
|
$node->setAllowEmptyValue(false);
|
||||||
|
|
||||||
|
$this->assertSame($value, $node->finalize($value));
|
||||||
|
}
|
||||||
|
|
||||||
public function getValidValues()
|
public function getValidValues()
|
||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
|
@ -24,6 +24,19 @@ class FloatNodeTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertSame($value, $node->normalize($value));
|
$this->assertSame($value, $node->normalize($value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider getValidValues
|
||||||
|
*
|
||||||
|
* @param int $value
|
||||||
|
*/
|
||||||
|
public function testValidNonEmptyValues($value)
|
||||||
|
{
|
||||||
|
$node = new FloatNode('test');
|
||||||
|
$node->setAllowEmptyValue(false);
|
||||||
|
|
||||||
|
$this->assertSame($value, $node->finalize($value));
|
||||||
|
}
|
||||||
|
|
||||||
public function getValidValues()
|
public function getValidValues()
|
||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
|
@ -24,6 +24,19 @@ class IntegerNodeTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertSame($value, $node->normalize($value));
|
$this->assertSame($value, $node->normalize($value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider getValidValues
|
||||||
|
*
|
||||||
|
* @param int $value
|
||||||
|
*/
|
||||||
|
public function testValidNonEmptyValues($value)
|
||||||
|
{
|
||||||
|
$node = new IntegerNode('test');
|
||||||
|
$node->setAllowEmptyValue(false);
|
||||||
|
|
||||||
|
$this->assertSame($value, $node->finalize($value));
|
||||||
|
}
|
||||||
|
|
||||||
public function getValidValues()
|
public function getValidValues()
|
||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
|
@ -57,4 +57,51 @@ class ScalarNodeTest extends \PHPUnit_Framework_TestCase
|
|||||||
array(new \stdClass()),
|
array(new \stdClass()),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider getValidNonEmptyValues
|
||||||
|
*
|
||||||
|
* @param mixed $value
|
||||||
|
*/
|
||||||
|
public function testValidNonEmptyValues($value)
|
||||||
|
{
|
||||||
|
$node = new ScalarNode('test');
|
||||||
|
$node->setAllowEmptyValue(false);
|
||||||
|
|
||||||
|
$this->assertSame($value, $node->finalize($value));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getValidNonEmptyValues()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
array(false),
|
||||||
|
array(true),
|
||||||
|
array('foo'),
|
||||||
|
array(0),
|
||||||
|
array(1),
|
||||||
|
array(0.0),
|
||||||
|
array(0.1),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider getEmptyValues
|
||||||
|
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
|
||||||
|
*
|
||||||
|
* @param mixed $value
|
||||||
|
*/
|
||||||
|
public function testNotAllowedEmptyValuesThrowException($value)
|
||||||
|
{
|
||||||
|
$node = new ScalarNode('test');
|
||||||
|
$node->setAllowEmptyValue(false);
|
||||||
|
$node->finalize($value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getEmptyValues()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
array(null),
|
||||||
|
array(''),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user