Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0199fbf

Browse files
committed
[Config] type specific check for emptiness
1 parent 6bcdee0 commit 0199fbf

File tree

8 files changed

+129
-1
lines changed

8 files changed

+129
-1
lines changed

src/Symfony/Component/Config/Definition/BooleanNode.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,13 @@ protected function validateType($value)
3636
throw $ex;
3737
}
3838
}
39+
40+
/**
41+
* {@inheritdoc}
42+
*/
43+
protected function isValueEmpty($value)
44+
{
45+
// a boolean value cannot be empty
46+
return false;
47+
}
3948
}

src/Symfony/Component/Config/Definition/NumericNode.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,13 @@ protected function finalizeValue($value)
5252

5353
return $value;
5454
}
55+
56+
/**
57+
* {@inheritdoc}
58+
*/
59+
protected function isValueEmpty($value)
60+
{
61+
// a numeric value cannot be empty
62+
return false;
63+
}
5564
}

src/Symfony/Component/Config/Definition/ScalarNode.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,12 @@ protected function validateType($value)
4343
throw $ex;
4444
}
4545
}
46+
47+
/**
48+
* {@inheritdoc}
49+
*/
50+
protected function isValueEmpty($value)
51+
{
52+
return null === $value || '' === $value;
53+
}
4654
}

src/Symfony/Component/Config/Definition/VariableNode.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ protected function validateType($value)
8484
*/
8585
protected function finalizeValue($value)
8686
{
87-
if (!$this->allowEmptyValue && empty($value)) {
87+
if (!$this->allowEmptyValue && $this->isValueEmpty($value)) {
8888
$ex = new InvalidConfigurationException(sprintf(
8989
'The path "%s" cannot contain an empty value, but got %s.',
9090
$this->getPath(),
@@ -113,4 +113,20 @@ protected function mergeValues($leftSide, $rightSide)
113113
{
114114
return $rightSide;
115115
}
116+
117+
/**
118+
* Evaluates if the given value is to be treated as empty.
119+
*
120+
* By default, PHP's empty() function is used to test for emptiness. This
121+
* method may be overridden by subtypes to better match their understanding
122+
* of empty data.
123+
*
124+
* @param mixed $value
125+
*
126+
* @return bool
127+
*/
128+
protected function isValueEmpty($value)
129+
{
130+
return empty($value);
131+
}
116132
}

src/Symfony/Component/Config/Tests/Definition/BooleanNodeTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ public function testNormalize($value)
2424
$this->assertSame($value, $node->normalize($value));
2525
}
2626

27+
/**
28+
* @dataProvider getValidValues
29+
*
30+
* @param bool $value
31+
*/
32+
public function testValidNonEmptyValues($value)
33+
{
34+
$node = new BooleanNode('test');
35+
$node->setAllowEmptyValue(false);
36+
37+
$this->assertSame($value, $node->finalize($value));
38+
}
39+
2740
public function getValidValues()
2841
{
2942
return array(

src/Symfony/Component/Config/Tests/Definition/FloatNodeTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ public function testNormalize($value)
2424
$this->assertSame($value, $node->normalize($value));
2525
}
2626

27+
/**
28+
* @dataProvider getValidValues
29+
*
30+
* @param int $value
31+
*/
32+
public function testValidNonEmptyValues($value)
33+
{
34+
$node = new FloatNode('test');
35+
$node->setAllowEmptyValue(false);
36+
37+
$this->assertSame($value, $node->finalize($value));
38+
}
39+
2740
public function getValidValues()
2841
{
2942
return array(

src/Symfony/Component/Config/Tests/Definition/IntegerNodeTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ public function testNormalize($value)
2424
$this->assertSame($value, $node->normalize($value));
2525
}
2626

27+
/**
28+
* @dataProvider getValidValues
29+
*
30+
* @param int $value
31+
*/
32+
public function testValidNonEmptyValues($value)
33+
{
34+
$node = new IntegerNode('test');
35+
$node->setAllowEmptyValue(false);
36+
37+
$this->assertSame($value, $node->finalize($value));
38+
}
39+
2740
public function getValidValues()
2841
{
2942
return array(

src/Symfony/Component/Config/Tests/Definition/ScalarNodeTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,51 @@ public function getInvalidValues()
5757
array(new \stdClass()),
5858
);
5959
}
60+
61+
/**
62+
* @dataProvider getValidNonEmptyValues
63+
*
64+
* @param mixed $value
65+
*/
66+
public function testValidNonEmptyValues($value)
67+
{
68+
$node = new ScalarNode('test');
69+
$node->setAllowEmptyValue(false);
70+
71+
$this->assertSame($value, $node->finalize($value));
72+
}
73+
74+
public function getValidNonEmptyValues()
75+
{
76+
return array(
77+
array(false),
78+
array(true),
79+
array('foo'),
80+
array(0),
81+
array(1),
82+
array(0.0),
83+
array(0.1),
84+
);
85+
}
86+
87+
/**
88+
* @dataProvider getEmptyValues
89+
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
90+
*
91+
* @param mixed $value
92+
*/
93+
public function testNotAllowedEmptyValuesThrowException($value)
94+
{
95+
$node = new ScalarNode('test');
96+
$node->setAllowEmptyValue(false);
97+
$node->finalize($value);
98+
}
99+
100+
public function getEmptyValues()
101+
{
102+
return array(
103+
array(null),
104+
array(''),
105+
);
106+
}
60107
}

0 commit comments

Comments
 (0)