Enabled camelCase format to be used on denormalize method, that way camel_case attribute can be used on object as getCamelCase()
This commit is contained in:
parent
d0aaf62557
commit
fbffdf0b44
@ -38,6 +38,7 @@ class GetSetMethodNormalizer extends SerializerAwareNormalizer implements Normal
|
||||
{
|
||||
protected $callbacks = array();
|
||||
protected $ignoredAttributes = array();
|
||||
protected $camelizedAttributes = array();
|
||||
|
||||
/**
|
||||
* Set normalization callbacks
|
||||
@ -66,6 +67,16 @@ class GetSetMethodNormalizer extends SerializerAwareNormalizer implements Normal
|
||||
$this->ignoredAttributes = $ignoredAttributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set attributes to be camelized on denormalize
|
||||
*
|
||||
* @param array $camelizedAttributes
|
||||
*/
|
||||
public function setCamelizedAttributes(array $camelizedAttributes)
|
||||
{
|
||||
$this->camelizedAttributes = $camelizedAttributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@ -111,7 +122,7 @@ class GetSetMethodNormalizer extends SerializerAwareNormalizer implements Normal
|
||||
|
||||
$params = array();
|
||||
foreach ($constructorParameters as $constructorParameter) {
|
||||
$paramName = lcfirst($constructorParameter->name);
|
||||
$paramName = lcfirst($this->formatAttribute($constructorParameter->name));
|
||||
|
||||
if (isset($data[$paramName])) {
|
||||
$params[] = $data[$paramName];
|
||||
@ -132,7 +143,8 @@ class GetSetMethodNormalizer extends SerializerAwareNormalizer implements Normal
|
||||
}
|
||||
|
||||
foreach ($data as $attribute => $value) {
|
||||
$setter = 'set'.$attribute;
|
||||
$setter = 'set'.$this->formatAttribute($attribute);
|
||||
|
||||
if (method_exists($object, $setter)) {
|
||||
$object->$setter($value);
|
||||
}
|
||||
@ -141,6 +153,27 @@ class GetSetMethodNormalizer extends SerializerAwareNormalizer implements Normal
|
||||
return $object;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format attribute name to access parameters or methods
|
||||
* As option, if attribute name is found on camelizedAttributes array
|
||||
* returns attribute name in camelcase format
|
||||
*
|
||||
* @param string $attribute
|
||||
* @return string
|
||||
*/
|
||||
protected function formatAttribute($attributeName)
|
||||
{
|
||||
if (in_array($attributeName, $this->camelizedAttributes)) {
|
||||
return preg_replace_callback(
|
||||
'/(^|_|\.)+(.)/', function ($match) {
|
||||
return ('.' === $match[1] ? '_' : '').strtoupper($match[2]);
|
||||
}, $attributeName
|
||||
);
|
||||
}
|
||||
|
||||
return $attributeName;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
|
@ -26,8 +26,9 @@ class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase
|
||||
$obj = new GetSetDummy;
|
||||
$obj->setFoo('foo');
|
||||
$obj->setBar('bar');
|
||||
$obj->setCamelCase('camelcase');
|
||||
$this->assertEquals(
|
||||
array('foo' => 'foo', 'bar' => 'bar', 'fooBar' => 'foobar'),
|
||||
array('foo' => 'foo', 'bar' => 'bar', 'fooBar' => 'foobar', 'camelCase' => 'camelcase'),
|
||||
$this->normalizer->normalize($obj, 'any')
|
||||
);
|
||||
}
|
||||
@ -43,6 +44,39 @@ class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals('bar', $obj->getBar());
|
||||
}
|
||||
|
||||
public function testDenormalizeOnCamelCaseFormat()
|
||||
{
|
||||
$this->normalizer->setCamelizedAttributes(array('camel_case'));
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array('camel_case' => 'camelCase'),
|
||||
__NAMESPACE__.'\GetSetDummy'
|
||||
);
|
||||
$this->assertEquals('camelCase', $obj->getCamelCase());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider attributeProvider
|
||||
*/
|
||||
public function testFormatAttribute($attribute, $camelizedAttributes, $result)
|
||||
{
|
||||
$r = new \ReflectionObject($this->normalizer);
|
||||
$m = $r->getMethod('formatAttribute');
|
||||
$m->setAccessible(true);
|
||||
|
||||
$this->normalizer->setCamelizedAttributes($camelizedAttributes);
|
||||
$this->assertEquals($m->invoke($this->normalizer, $attribute, $camelizedAttributes), $result);
|
||||
}
|
||||
|
||||
public function attributeProvider()
|
||||
{
|
||||
return array(
|
||||
array('attribute_test', array('attribute_test'),'AttributeTest'),
|
||||
array('attribute_test', array('any'),'attribute_test'),
|
||||
array('attribute', array('attribute'),'Attribute'),
|
||||
array('attribute', array(), 'attribute'),
|
||||
);
|
||||
}
|
||||
|
||||
public function testConstructorDenormalize()
|
||||
{
|
||||
$obj = $this->normalizer->denormalize(
|
||||
@ -82,7 +116,7 @@ class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
public function testIgnoredAttributes()
|
||||
{
|
||||
$this->normalizer->setIgnoredAttributes(array('foo', 'bar'));
|
||||
$this->normalizer->setIgnoredAttributes(array('foo', 'bar', 'camelCase'));
|
||||
|
||||
$obj = new GetSetDummy;
|
||||
$obj->setFoo('foo');
|
||||
@ -160,6 +194,7 @@ class GetSetDummy
|
||||
{
|
||||
protected $foo;
|
||||
private $bar;
|
||||
protected $camelCase;
|
||||
|
||||
public function getFoo()
|
||||
{
|
||||
@ -186,6 +221,16 @@ class GetSetDummy
|
||||
return $this->foo . $this->bar;
|
||||
}
|
||||
|
||||
public function getCamelCase()
|
||||
{
|
||||
return $this->camelCase;
|
||||
}
|
||||
|
||||
public function setCamelCase($camelCase)
|
||||
{
|
||||
$this->camelCase = $camelCase;
|
||||
}
|
||||
|
||||
public function otherMethod()
|
||||
{
|
||||
throw new \RuntimeException("Dummy::otherMethod() should not be called");
|
||||
|
Reference in New Issue
Block a user