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

Skip to content

Commit 3a589df

Browse files
javiereguiluzfabpot
authored andcommitted
Added a castToArray() config helper
1 parent e7c12d3 commit 3a589df

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

src/Symfony/Component/Config/CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ CHANGELOG
88
* added second `$exists` constructor argument to `ClassExistenceResource`
99
* made `ClassExistenceResource` work with interfaces and traits
1010
* added `ConfigCachePass` (originally in FrameworkBundle)
11+
* added `castToArray()` helper to turn any config value into an array
1112

1213
3.0.0
1314
-----
@@ -36,7 +37,7 @@ Before: `InvalidArgumentException` (variable must contain at least two
3637
distinct elements).
3738
After: the code will work as expected and it will restrict the values of the
3839
`variable` option to just `value`.
39-
40+
4041
* deprecated the `ResourceInterface::isFresh()` method. If you implement custom resource types and they
4142
can be validated that way, make them implement the new `SelfCheckingResourceInterface`.
4243
* deprecated the getResource() method in ResourceInterface. You can still call this method
@@ -49,7 +50,7 @@ After: the code will work as expected and it will restrict the values of the
4950

5051
* added `ConfigCacheInterface`, `ConfigCacheFactoryInterface` and a basic `ConfigCacheFactory`
5152
implementation to delegate creation of ConfigCache instances
52-
53+
5354
2.2.0
5455
-----
5556

src/Symfony/Component/Config/Definition/Builder/ExprBuilder.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,19 @@ public function ifNotInArray(array $array)
149149
return $this;
150150
}
151151

152+
/**
153+
* Transforms variables of any type into an array.
154+
*
155+
* @return $this
156+
*/
157+
public function castToArray()
158+
{
159+
$this->ifPart = function ($v) { return !is_array($v); };
160+
$this->thenPart = function ($v) { return array($v); };
161+
162+
return $this;
163+
}
164+
152165
/**
153166
* Sets the closure to run if the test pass.
154167
*

src/Symfony/Component/Config/Tests/Definition/Builder/ExprBuilderTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,25 @@ public function testThenEmptyArrayExpression()
145145
$this->assertFinalizedValueIs(array(), $test);
146146
}
147147

148+
/**
149+
* @dataProvider castToArrayValues
150+
*/
151+
public function testcastToArrayExpression($configValue, $expectedValue)
152+
{
153+
$test = $this->getTestBuilder()
154+
->castToArray()
155+
->end();
156+
$this->assertFinalizedValueIs($expectedValue, $test, array('key' => $configValue));
157+
}
158+
159+
public function castToArrayValues()
160+
{
161+
yield array('value', array('value'));
162+
yield array(-3.14, array(-3.14));
163+
yield array(null, array(null));
164+
yield array(array('value'), array('value'));
165+
}
166+
148167
/**
149168
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
150169
*/

0 commit comments

Comments
 (0)