Skip to content

Commit d0e1547

Browse files
committed
merged branch Partugal/parameterBag (PR #4468)
Commits ------- 1227cc2 add escapeValue to ParameterBagInterface Discussion ---------- add escapeValue to ParameterBagInterface #4465 --------------------------------------------------------------------------- by travisbot at 2012-05-30T18:01:47Z This pull request [fails](http://travis-ci.org/symfony/symfony/builds/1479725) (merged 1227cc2 into 49e213c). --------------------------------------------------------------------------- by drak at 2012-05-31T02:42:44Z @bschussek - there are a few form tests failing that seem to have been merged into master and thus all other unrelated PRs are failing their travis build checks. @fabpot
2 parents fb3f771 + 1227cc2 commit d0e1547

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

src/Symfony/Component/DependencyInjection/ParameterBag/ParameterBag.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,27 @@ public function isResolved()
253253
return $this->resolved;
254254
}
255255

256+
/**
257+
* {@inheritDoc}
258+
*/
259+
public function escapeValue($value)
260+
{
261+
if (is_string($value)) {
262+
return str_replace('%', '%%', $value);
263+
}
264+
265+
if (is_array($value)) {
266+
$result = array();
267+
foreach ($value as $k => $v) {
268+
$result[$k] = $this->escapeValue($v);
269+
}
270+
271+
return $result;
272+
}
273+
274+
return $value;
275+
}
276+
256277
private function unescapeValue($value)
257278
{
258279
if (is_string($value)) {

src/Symfony/Component/DependencyInjection/ParameterBag/ParameterBagInterface.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,13 @@ function resolve();
9494
* @throws ParameterNotFoundException if a placeholder references a parameter that does not exist
9595
*/
9696
function resolveValue($value);
97+
98+
/**
99+
* Escape parameter placeholders %
100+
*
101+
* @param mixed $value
102+
*
103+
* @return mixed
104+
*/
105+
function escapeValue($value);
97106
}

src/Symfony/Component/DependencyInjection/Tests/ParameterBag/ParameterBagTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,22 @@ public function testResolveUnescapesValue()
201201
$this->assertEquals(array('bar' => array('ding' => 'I\'m a bar %foo %bar')), $bag->get('foo'), '->resolveValue() supports % escaping by doubling it');
202202
}
203203

204+
/**
205+
* @covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag::escapeValue
206+
*/
207+
public function testEscapeValue()
208+
{
209+
$bag = new ParameterBag();
210+
211+
$bag->add(array(
212+
'foo' => $bag->escapeValue(array('bar' => array('ding' => 'I\'m a bar %foo %bar', 'zero' => null))),
213+
'bar' => $bag->escapeValue('I\'m a %foo%'),
214+
));
215+
216+
$this->assertEquals('I\'m a %%foo%%', $bag->get('bar'), '->escapeValue() escapes % by doubling it');
217+
$this->assertEquals(array('bar' => array('ding' => 'I\'m a bar %%foo %%bar', 'zero' => null)), $bag->get('foo'), '->escapeValue() escapes % by doubling it');
218+
}
219+
204220
/**
205221
* @covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag::resolve
206222
* @dataProvider stringsWithSpacesProvider

0 commit comments

Comments
 (0)