Skip to content

Commit 84ec345

Browse files
committed
Enabled camelCase format to be used on denormalize method, that way camel_case attribute can be used on object as getCamelCase()
1 parent 7e37c64 commit 84ec345

File tree

2 files changed

+82
-4
lines changed

2 files changed

+82
-4
lines changed

Normalizer/GetSetMethodNormalizer.php

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class GetSetMethodNormalizer extends SerializerAwareNormalizer implements Normal
3838
{
3939
protected $callbacks = array();
4040
protected $ignoredAttributes = array();
41+
protected $camelizedAttributes = array();
4142

4243
/**
4344
* Set normalization callbacks
@@ -66,6 +67,16 @@ public function setIgnoredAttributes(array $ignoredAttributes)
6667
$this->ignoredAttributes = $ignoredAttributes;
6768
}
6869

70+
/**
71+
* Set attributes to be camelized on denormalize
72+
*
73+
* @param array $camelizedAttributes
74+
*/
75+
public function setCamelizedAttributes(array $camelizedAttributes)
76+
{
77+
$this->camelizedAttributes = $camelizedAttributes;
78+
}
79+
6980
/**
7081
* {@inheritdoc}
7182
*/
@@ -111,7 +122,7 @@ public function denormalize($data, $class, $format = null, array $context = arra
111122

112123
$params = array();
113124
foreach ($constructorParameters as $constructorParameter) {
114-
$paramName = lcfirst($constructorParameter->name);
125+
$paramName = lcfirst($this->formatAttribute($constructorParameter->name));
115126

116127
if (isset($data[$paramName])) {
117128
$params[] = $data[$paramName];
@@ -132,7 +143,8 @@ public function denormalize($data, $class, $format = null, array $context = arra
132143
}
133144

134145
foreach ($data as $attribute => $value) {
135-
$setter = 'set'.$attribute;
146+
$setter = 'set'.$this->formatAttribute($attribute);
147+
136148
if (method_exists($object, $setter)) {
137149
$object->$setter($value);
138150
}
@@ -141,6 +153,27 @@ public function denormalize($data, $class, $format = null, array $context = arra
141153
return $object;
142154
}
143155

156+
/**
157+
* Format attribute name to access parameters or methods
158+
* As option, if attribute name is found on camelizedAttributes array
159+
* returns attribute name in camelcase format
160+
*
161+
* @param string $attribute
162+
* @return string
163+
*/
164+
protected function formatAttribute($attributeName)
165+
{
166+
if (in_array($attributeName, $this->camelizedAttributes)) {
167+
return preg_replace_callback(
168+
'/(^|_|\.)+(.)/', function ($match) {
169+
return ('.' === $match[1] ? '_' : '').strtoupper($match[2]);
170+
}, $attributeName
171+
);
172+
}
173+
174+
return $attributeName;
175+
}
176+
144177
/**
145178
* {@inheritDoc}
146179
*/

Tests/Normalizer/GetSetMethodNormalizerTest.php

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ public function testNormalize()
2626
$obj = new GetSetDummy;
2727
$obj->setFoo('foo');
2828
$obj->setBar('bar');
29+
$obj->setCamelCase('camelcase');
2930
$this->assertEquals(
30-
array('foo' => 'foo', 'bar' => 'bar', 'fooBar' => 'foobar'),
31+
array('foo' => 'foo', 'bar' => 'bar', 'fooBar' => 'foobar', 'camelCase' => 'camelcase'),
3132
$this->normalizer->normalize($obj, 'any')
3233
);
3334
}
@@ -43,6 +44,39 @@ public function testDenormalize()
4344
$this->assertEquals('bar', $obj->getBar());
4445
}
4546

47+
public function testDenormalizeOnCamelCaseFormat()
48+
{
49+
$this->normalizer->setCamelizedAttributes(array('camel_case'));
50+
$obj = $this->normalizer->denormalize(
51+
array('camel_case' => 'camelCase'),
52+
__NAMESPACE__.'\GetSetDummy'
53+
);
54+
$this->assertEquals('camelCase', $obj->getCamelCase());
55+
}
56+
57+
/**
58+
* @dataProvider attributeProvider
59+
*/
60+
public function testFormatAttribute($attribute, $camelizedAttributes, $result)
61+
{
62+
$r = new \ReflectionObject($this->normalizer);
63+
$m = $r->getMethod('formatAttribute');
64+
$m->setAccessible(true);
65+
66+
$this->normalizer->setCamelizedAttributes($camelizedAttributes);
67+
$this->assertEquals($m->invoke($this->normalizer, $attribute, $camelizedAttributes), $result);
68+
}
69+
70+
public function attributeProvider()
71+
{
72+
return array(
73+
array('attribute_test', array('attribute_test'),'AttributeTest'),
74+
array('attribute_test', array('any'),'attribute_test'),
75+
array('attribute', array('attribute'),'Attribute'),
76+
array('attribute', array(), 'attribute'),
77+
);
78+
}
79+
4680
public function testConstructorDenormalize()
4781
{
4882
$obj = $this->normalizer->denormalize(
@@ -82,7 +116,7 @@ public function testUncallableCallbacks()
82116

83117
public function testIgnoredAttributes()
84118
{
85-
$this->normalizer->setIgnoredAttributes(array('foo', 'bar'));
119+
$this->normalizer->setIgnoredAttributes(array('foo', 'bar', 'camelCase'));
86120

87121
$obj = new GetSetDummy;
88122
$obj->setFoo('foo');
@@ -160,6 +194,7 @@ class GetSetDummy
160194
{
161195
protected $foo;
162196
private $bar;
197+
protected $camelCase;
163198

164199
public function getFoo()
165200
{
@@ -186,6 +221,16 @@ public function getFooBar()
186221
return $this->foo . $this->bar;
187222
}
188223

224+
public function getCamelCase()
225+
{
226+
return $this->camelCase;
227+
}
228+
229+
public function setCamelCase($camelCase)
230+
{
231+
$this->camelCase = $camelCase;
232+
}
233+
189234
public function otherMethod()
190235
{
191236
throw new \RuntimeException("Dummy::otherMethod() should not be called");

0 commit comments

Comments
 (0)