8000 Added a castToArray() config helper by javiereguiluz · Pull Request #21893 · symfony/symfony · GitHub
[go: up one dir, main page]

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 1 commit
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
Next Next commit
Added a castToArray() config helper
  • Loading branch information
javiereguiluz committed Mar 6, 2017
commit d6e8e242c145a2e14b1d04a49c68c2f53883519e
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
0