Skip to content

Commit 8f30161

Browse files
committed
ignore leading underscores in base normalizer
In #1267, a new normalizer class was introduced that ignores underscores when converting snake case to camel case. However, the same change was made to the base class. This means that both classes act in exactly the same way. Furthermore, this change was a BC break as the behavior of the base normalizer class was changed without fixing an actual bug.
1 parent 679f6ed commit 8f30161

File tree

2 files changed

+31
-29
lines changed

2 files changed

+31
-29
lines changed

Normalizer/CamelKeysNormalizer.php

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,30 @@ public function normalize(array $data)
3939
*/
4040
private function normalizeArray(array &$data)
4141
{
42+
$normalizedData = array();
43+
4244
foreach ($data as $key => $val) {
4345
$normalizedKey = $this->normalizeString($key);
4446

4547
if ($normalizedKey !== $key) {
46-
if (array_key_exists($normalizedKey, $data)) {
48+
if (array_key_exists($normalizedKey, $normalizedData)) {
4749
throw new NormalizationException(sprintf(
4850
'The key "%s" is invalid as it will override the existing key "%s"',
4951
$key,
5052
$normalizedKey
5153
));
5254
}
53-
54-
unset($data[$key]);
55-
$data[$normalizedKey] = $val;
56-
$key = $normalizedKey;
5755
}
5856

57+
$normalizedData[$normalizedKey] = $val;
58+
$key = $normalizedKey;
59+
5960
if (is_array($val)) {
60-
$this->normalizeArray($data[$key]);
61+
$this->normalizeArray($normalizedData[$key]);
6162
}
6263
}
64+
65+
$data = $normalizedData;
6366
}
6467

6568
/**
@@ -75,17 +78,8 @@ protected function normalizeString($string)
7578
return $string;
7679
}
7780

78-
if (preg_match('/^(_+)(.*)/', $string, $matches)) {
79-
$underscorePrefix = $matches[1];
80-
$string = $matches[2];
81-
} else {
82-
$underscorePrefix = '';
83-
}
84-
85-
$string = preg_replace_callback('/_([a-zA-Z0-9])/', function ($matches) {
81+
return preg_replace_callback('/_([a-zA-Z0-9])/', function ($matches) {
8682
return strtoupper($matches[1]);
8783
}, $string);
88-
89-
return $underscorePrefix.$string;
9084
}
9185
}

Tests/Normalizer/CamelKeysNormalizerTest.php

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,14 @@ public function testNormalize(array $array, array $expected)
4141

4242
public function normalizeProvider()
4343
{
44-
return array(
45-
array(array(), array()),
46-
array(
47-
array('foo' => array('Foo_bar_baz' => array('foo_Bar' => array('foo_bar' => 'foo_bar'))),
48-
'foo_1ar' => array('foo_bar'),
49-
),
50-
array('foo' => array('FooBarBaz' => array('fooBar' => array('fooBar' => 'foo_bar'))),
51-
'foo1ar' => array('foo_bar'),
52-
),
53-
),
54-
);
44+
$array = $this->normalizeProviderCommon();
45+
$array[] = array(array('__username' => 'foo', '_password' => 'bar', '_foo_bar' => 'foobar'), array('_Username' => 'foo', 'Password' => 'bar', 'FooBar' => 'foobar'));
46+
47+
return $array;
5548
}
5649

5750
/**
58-
* @dataProvider normalizeProvider
51+
* @dataProvider normalizeProviderLeadingUnderscore
5952
*/
6053
public function testNormalizeLeadingUnderscore(array $array, array $expected)
6154
{
@@ -65,9 +58,24 @@ public function testNormalizeLeadingUnderscore(array $array, array $expected)
6558

6659
public function normalizeProviderLeadingUnderscore()
6760
{
68-
$array = $this->normalizeProvider();
61+
$array = $this->normalizeProviderCommon();
6962
$array[] = array(array('__username' => 'foo', '_password' => 'bar', '_foo_bar' => 'foobar'), array('__username' => 'foo', '_password' => 'bar', '_fooBar' => 'foobar'));
7063

7164
return $array;
7265
}
66+
67+
private function normalizeProviderCommon()
68+
{
69+
return array(
70+
array(array(), array()),
71+
array(
72+
array('foo' => array('Foo_bar_baz' => array('foo_Bar' => array('foo_bar' => 'foo_bar'))),
73+
'foo_1ar' => array('foo_bar'),
74+
),
75+
array('foo' => array('FooBarBaz' => array('fooBar' => array('fooBar' => 'foo_bar'))),
76+
'foo1ar' => array('foo_bar'),
77+
),
78+
),
79+
);
80+
}
7381
}

0 commit comments

Comments
 (0)