Skip to content

Commit d8b42aa

Browse files
committed
merged branch GromNaN/dic-camelize (PR #8536)
This PR was merged into the 2.2 branch. Discussion ---------- [DependencyInjection] Fix Container::camelize to convert beginning and ending . and _ | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #7431 | License | MIT | Doc PR | n/a I'm using strtr to make the conversion in order to ensure that the behavior is the same as `Container::get`. From the test cases I've added, the following were not passing: Commits ------- 485d53a [DependencyInjection] Fix Container::camelize to convert beginning and ending chars
2 parents 4970770 + 485d53a commit d8b42aa

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/Symfony/Component/DependencyInjection/Container.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ public function isScopeActive($name)
471471
*/
472472
public static function camelize($id)
473473
{
474-
return preg_replace_callback('/(^|_|\.)+(.)/', function ($match) { return ('.' === $match[1] ? '_' : '').strtoupper($match[2]); }, $id);
474+
return strtr(ucwords(strtr($id, array('_' => ' ', '.' => '_ '))), array(' ' => ''));
475475
}
476476

477477
/**

src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,29 @@ public function testConstructor()
3030
$this->assertEquals(array('foo' => 'bar'), $sc->getParameterBag()->all(), '__construct() takes an array of parameters as its first argument');
3131
}
3232

33+
/**
34+
* @dataProvider dataForTestCamelize
35+
*/
36+
public function testCamelize($id, $expected)
37+
{
38+
$this->assertEquals($expected, Container::camelize($id), sprintf('Container::camelize("%s")', $id));
39+
}
40+
41+
public function dataForTestCamelize()
42+
{
43+
return array(
44+
array('foo_bar', 'FooBar'),
45+
array('foo.bar', 'Foo_Bar'),
46+
array('foo.bar_baz', 'Foo_BarBaz'),
47+
array('foo._bar', 'Foo_Bar'),
48+
array('foo_.bar', 'Foo_Bar'),
49+
array('_foo', 'Foo'),
50+
array('.foo', '_Foo'),
51+
array('foo_', 'Foo'),
52+
array('foo.', 'Foo_'),
53+
);
54+
}
55+
3356
/**
3457
* @covers Symfony\Component\DependencyInjection\Container::compile
3558
*/

0 commit comments

Comments
 (0)