Skip to content

Added a castToArray() config helper #21893

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Symfony/Component/Config/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ CHANGELOG
* added second `$exists` constructor argument to `ClassExistenceResource`
* made `ClassExistenceResource` work with interfaces and traits
* added `ConfigCachePass` (originally in FrameworkBundle)
* added `castToArray()` helper to turn any config value into an array

3.0.0
-----
Expand Down Expand Up @@ -36,7 +37,7 @@ Before: `InvalidArgumentException` (variable must contain at least two
distinct elements).
After: the code will work as expected and it will restrict the values of the
`variable` option to just `value`.

* deprecated the `ResourceInterface::isFresh()` method. If you implement custom resource types and they
can be validated that way, make them implement the new `SelfCheckingResourceInterface`.
* deprecated the getResource() method in ResourceInterface. You can still call this method
Expand All @@ -49,7 +50,7 @@ After: the code will work as expected and it will restrict the values of the

* added `ConfigCacheInterface`, `ConfigCacheFactoryInterface` and a basic `ConfigCacheFactory`
implementation to delegate creation of ConfigCache instances

2.2.0
-----

Expand Down
13 changes: 13 additions & 0 deletions src/Symfony/Component/Config/Definition/Builder/ExprBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,19 @@ public function ifNotInArray(array $array)
return $this;
}

/**
* Transforms variables of any type into an array.
*
* @return $this
*/
public function castToArray()
{
$this->ifPart = function ($v) { return !is_array($v); };
$this->thenPart = function ($v) { return array($v); };

return $this;
}

/**
* Sets the closure to run if the test pass.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,25 @@ public function testThenEmptyArrayExpression()
$this->assertFinalizedValueIs(array(), $test);
}

/**
* @dataProvider castToArrayValues
*/
public function testcastToArrayExpression($configValue, $expectedValue)
{
$test = $this->getTestBuilder()
->castToArray()
->end();
$this->assertFinalizedValueIs($expectedValue, $test, array('key' => $configValue));
}

public function castToArrayValues()
{
yield array('value', array('value'));
yield array(-3.14, array(-3.14));
yield array(null, array(null));
yield array(array('value'), array('value'));
}

/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
*/
Expand Down