Skip to content

Commit 656f8b6

Browse files
committed
allow more control on GetSetMethodNormalizer by using callback functions and an ignoreAttributes list
1 parent 03da095 commit 656f8b6

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed

Normalizer/GetSetMethodNormalizer.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,34 @@
3535
*/
3636
class GetSetMethodNormalizer extends SerializerAwareNormalizer implements NormalizerInterface, DenormalizerInterface
3737
{
38+
protected $callbacks = array();
39+
protected $ignoredAttributes = array();
40+
41+
/**
42+
* Set normalization callbacks
43+
*
44+
* @param array $callbacks help normalize the result
45+
*/
46+
public function setCallbacks(array $callbacks)
47+
{
48+
foreach ($callbacks as $attribute => $callback) {
49+
if (!is_callable($callback)) {
50+
throw new \InvalidArgumentException(sprintf('The given callback for attribute "%s" is not callable.', $attribute));
51+
}
52+
}
53+
$this->callbacks = $callbacks;
54+
}
55+
56+
/**
57+
* Set ignored attributes for normalization
58+
*
59+
* @param array $ignoredAttributes
60+
*/
61+
public function setIgnoredAttributes(array $ignoredAttributes)
62+
{
63+
$this->ignoredAttributes = $ignoredAttributes;
64+
}
65+
3866
/**
3967
* {@inheritdoc}
4068
*/
@@ -48,7 +76,14 @@ public function normalize($object, $format = null)
4876
if ($this->isGetMethod($method)) {
4977
$attributeName = lcfirst(substr($method->getName(), 3));
5078

79+
if (in_array($attributeName, $this->ignoredAttributes)) {
80+
continue;
81+
}
82+
5183
$attributeValue = $method->invoke($object);
84+
if (array_key_exists($attributeName, $this->callbacks)) {
85+
$attributeValue = call_user_func($this->callbacks[$attributeName], $attributeValue);
86+
}
5287
if (null !== $attributeValue && !is_scalar($attributeValue)) {
5388
$attributeValue = $this->serializer->normalize($attributeValue, $format);
5489
}

Tests/Normalizer/GetSetMethodNormalizerTest.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,108 @@ public function testConstructorDenormalize()
5151
$this->assertEquals('foo', $obj->getFoo());
5252
$this->assertEquals('bar', $obj->getBar());
5353
}
54+
55+
/**
56+
* @dataProvider provideCallbacks
57+
*/
58+
public function testCallbacks($callbacks, $value, $result, $message)
59+
{
60+
$this->normalizer->setCallbacks($callbacks);
61+
62+
$obj = new GetConstructorDummy('', $value);
63+
64+
$this->assertEquals(
65+
$result,
66+
$this->normalizer->normalize($obj, 'any'),
67+
$message
68+
);
69+
}
70+
71+
/**
72+
* @expectedException \InvalidArgumentException
73+
*/
74+
public function testUncallableCallbacks()
75+
{
76+
$this->normalizer->setCallbacks(array('bar' => null));
77+
78+
$obj = new GetConstructorDummy('baz', 'quux');
79+
80+
$this->normalizer->normalize($obj, 'any');
81+
}
82+
83+
public function testIgnoredAttributes()
84+
{
85+
$this->normalizer->setIgnoredAttributes(array('foo', 'bar'));
86+
87+
$obj = new GetSetDummy;
88+
$obj->setFoo('foo');
89+
$obj->setBar('bar');
90+
91+
$this->assertEquals(
92+
array('fooBar' => 'foobar'),
93+
$this->normalizer->normalize($obj, 'any')
94+
);
95+
}
96+
97+
public function provideCallbacks()
98+
{
99+
return array(
100+
array(
101+
array(
102+
'bar' => function ($bar) {
103+
return 'baz';
104+
},
105+
),
106+
'baz',
107+
array('foo' => '', 'bar' => 'baz'),
108+
'Change a string',
109+
),
110+
array(
111+
array(
112+
'bar' => function ($bar) {
113+
return null;
114+
},
115+
),
116+
'baz',
117+
array('foo' => '', 'bar' => null),
118+
'Null an item'
119+
),
120+
array(
121+
array(
122+
'bar' => function ($bar) {
123+
return $bar->format('d-m-Y H:i:s');
124+
},
125+
),
126+
new \DateTime('2011-09-10 06:30:00'),
127+
array('foo' => '', 'bar' => '10-09-2011 06:30:00'),
128+
'Format a date',
129+
),
130+
array(
131+
array(
132+
'bar' => function ($bars) {
133+
$foos = '';
134+
foreach ($bars as $bar) {
135+
$foos .= $bar->getFoo();
136+
}
137+
return $foos;
138+
},
139+
),
140+
array(new GetConstructorDummy('baz', ''), new GetConstructorDummy('quux', '')),
141+
array('foo' => '', 'bar' => 'bazquux'),
142+
'Collect a property',
143+
),
144+
array(
145+
array(
146+
'bar' => function ($bars) {
147+
return count($bars);
148+
},
149+
),
150+
array(new GetConstructorDummy('baz', ''), new GetConstructorDummy('quux', '')),
151+
array('foo' => '', 'bar' => 2),
152+
'Count a property',
153+
),
154+
);
155+
}
54156
}
55157

56158
class GetSetDummy

0 commit comments

Comments
 (0)