Skip to content

Commit f395774

Browse files
committed
ignore any number of leading underscores when converting from snake case to camel case (ie. leave '_username' unchanged)
1 parent 778c9e8 commit f395774

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

Normalizer/CamelKeysNormalizer.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,17 @@ private function normalizeString($string)
7575
return $string;
7676
}
7777

78-
return preg_replace_callback('/_([a-zA-Z0-9])/', function ($matches) {
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) {
7986
return strtoupper($matches[1]);
8087
}, $string);
88+
89+
return $underscorePrefix.$string;
8190
}
8291
}

Tests/Normalizer/CamelKeysNormalizerTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public function normalizeProvider()
5050
'foo1ar' => array('foo_bar'),
5151
),
5252
),
53+
// ignore leading underscores
54+
array(array('__username' => 'foo', '_password' => 'bar', '_foo_bar' => 'foobar'), array('__username' => 'foo', '_password' => 'bar', '_fooBar' => 'foobar')),
5355
);
5456
}
5557
}

0 commit comments

Comments
 (0)