Skip to content

Commit 480219f

Browse files
Marcin Chwedziakfabpot
authored andcommitted
[Serializer] added support for is.* methods in GetSetMethodNormalizer
1 parent 872647a commit 480219f

File tree

3 files changed

+48
-19
lines changed

3 files changed

+48
-19
lines changed

src/Symfony/Component/Serializer/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
2.5.0
5+
-----
6+
7+
* added support for `is.*` getters in `GetSetMethodNormalizer`
8+
49
2.4.0
510
-----
611

src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function normalize($object, $format = null, array $context = array())
8888
$attributes = array();
8989
foreach ($reflectionMethods as $method) {
9090
if ($this->isGetMethod($method)) {
91-
$attributeName = lcfirst(substr($method->name, 3));
91+
$attributeName = lcfirst(substr($method->name, 0 === strpos($method->name, 'is') ? 2 : 3));
9292

9393
if (in_array($attributeName, $this->ignoredAttributes)) {
9494
continue;
@@ -211,17 +211,19 @@ private function supports($class)
211211
}
212212

213213
/**
214-
* Checks if a method's name is get.* and can be called without parameters.
214+
* Checks if a method's name is get.* or is.*, and can be called without parameters.
215215
*
216216
* @param \ReflectionMethod $method the method to check
217217
*
218-
* @return Boolean whether the method is a getter.
218+
* @return Boolean whether the method is a getter or boolean getter.
219219
*/
220220
private function isGetMethod(\ReflectionMethod $method)
221221
{
222+
$methodLength = strlen($method->name);
223+
222224
return (
223-
0 === strpos($method->name, 'get') &&
224-
3 < strlen($method->name) &&
225+
((0 === strpos($method->name, 'get') && 3 < $methodLength) ||
226+
(0 === strpos($method->name, 'is') && 2 < $methodLength)) &&
225227
0 === $method->getNumberOfRequiredParameters()
226228
);
227229
}

src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,24 @@ public function testNormalize()
2626
$obj = new GetSetDummy();
2727
$obj->setFoo('foo');
2828
$obj->setBar('bar');
29+
$obj->setBaz(true);
2930
$obj->setCamelCase('camelcase');
3031
$this->assertEquals(
31-
array('foo' => 'foo', 'bar' => 'bar', 'fooBar' => 'foobar', 'camelCase' => 'camelcase'),
32+
array('foo' => 'foo', 'bar' => 'bar', 'baz' => true, 'fooBar' => 'foobar', 'camelCase' => 'camelcase'),
3233
$this->normalizer->normalize($obj, 'any')
3334
);
3435
}
3536

3637
public function testDenormalize()
3738
{
3839
$obj = $this->normalizer->denormalize(
39-
array('foo' => 'foo', 'bar' => 'bar', 'fooBar' => 'foobar'),
40+
array('foo' => 'foo', 'bar' => 'bar', 'baz' => true, 'fooBar' => 'foobar'),
4041
__NAMESPACE__.'\GetSetDummy',
4142
'any'
4243
);
4344
$this->assertEquals('foo', $obj->getFoo());
4445
$this->assertEquals('bar', $obj->getBar());
46+
$this->assertTrue($obj->isBaz());
4547
}
4648

4749
public function testDenormalizeOnCamelCaseFormat()
@@ -80,10 +82,11 @@ public function attributeProvider()
8082
public function testConstructorDenormalize()
8183
{
8284
$obj = $this->normalizer->denormalize(
83-
array('foo' => 'foo', 'bar' => 'bar', 'fooBar' => 'foobar'),
85+
array('foo' => 'foo', 'bar' => 'bar', 'baz' => true, 'fooBar' => 'foobar'),
8486
__NAMESPACE__.'\GetConstructorDummy', 'any');
8587
$this->assertEquals('foo', $obj->getFoo());
8688
$this->assertEquals('bar', $obj->getBar());
89+
$this->assertTrue($obj->isBaz());
8790
}
8891

8992
/**
@@ -93,7 +96,7 @@ public function testCallbacks($callbacks, $value, $result, $message)
9396
{
9497
$this->normalizer->setCallbacks($callbacks);
9598

96-
$obj = new GetConstructorDummy('', $value);
99+
$obj = new GetConstructorDummy('', $value, true);
97100

98101
$this->assertEquals(
99102
$result,
@@ -109,18 +112,19 @@ public function testUncallableCallbacks()
109112
{
110113
$this->normalizer->setCallbacks(array('bar' => null));
111114

112-
$obj = new GetConstructorDummy('baz', 'quux');
115+
$obj = new GetConstructorDummy('baz', 'quux', true);
113116

114117
$this->normalizer->normalize($obj, 'any');
115118
}
116119

117120
public function testIgnoredAttributes()
118121
{
119-
$this->normalizer->setIgnoredAttributes(array('foo', 'bar', 'camelCase'));
122+
$this->normalizer->setIgnoredAttributes(array('foo', 'bar', 'baz', 'camelCase'));
120123

121124
$obj = new GetSetDummy();
122125
$obj->setFoo('foo');
123126
$obj->setBar('bar');
127+
$obj->setBaz(true);
124128

125129
$this->assertEquals(
126130
array('fooBar' => 'foobar'),
@@ -138,7 +142,7 @@ public function provideCallbacks()
138142
},
139143
),
140144
'baz',
141-
array('foo' => '', 'bar' => 'baz'),
145+
array('foo' => '', 'bar' => 'baz', 'baz' => true),
142146
'Change a string',
143147
),
144148
array(
@@ -148,7 +152,7 @@ public function provideCallbacks()
148152
},
149153
),
150154
'baz',
151-
array('foo' => '', 'bar' => null),
155+
array('foo' => '', 'bar' => null, 'baz' => true),
152156
'Null an item'
153157
),
154158
array(
@@ -158,7 +162,7 @@ public function provideCallbacks()
158162
},
159163
),
160164
new \DateTime('2011-09-10 06:30:00'),
161-
array('foo' => '', 'bar' => '10-09-2011 06:30:00'),
165+
array('foo' => '', 'bar' => '10-09-2011 06:30:00', 'baz' => true),
162166
'Format a date',
163167
),
164168
array(
@@ -172,8 +176,8 @@ public function provideCallbacks()
172176
return $foos;
173177
},
174178
),
175-
array(new GetConstructorDummy('baz', ''), new GetConstructorDummy('quux', '')),
176-
array('foo' => '', 'bar' => 'bazquux'),
179+
array(new GetConstructorDummy('baz', '', false), new GetConstructorDummy('quux', '', false)),
180+
array('foo' => '', 'bar' => 'bazquux', 'baz' => true),
177181
'Collect a property',
178182
),
179183
array(
@@ -182,8 +186,8 @@ public function provideCallbacks()
182186
return count($bars);
183187
},
184188
),
185-
array(new GetConstructorDummy('baz', ''), new GetConstructorDummy('quux', '')),
186-
array('foo' => '', 'bar' => 2),
189+
array(new GetConstructorDummy('baz', '', false), new GetConstructorDummy('quux', '', false)),
190+
array('foo' => '', 'bar' => 2, 'baz' => true),
187191
'Count a property',
188192
),
189193
);
@@ -194,6 +198,7 @@ class GetSetDummy
194198
{
195199
protected $foo;
196200
private $bar;
201+
private $baz;
197202
protected $camelCase;
198203

199204
public function getFoo()
@@ -216,6 +221,16 @@ public function setBar($bar)
216221
$this->bar = $bar;
217222
}
218223

224+
public function isBaz()
225+
{
226+
return $this->baz;
227+
}
228+
229+
public function setBaz($baz)
230+
{
231+
$this->baz = $baz;
232+
}
233+
219234
public function getFooBar()
220235
{
221236
return $this->foo.$this->bar;
@@ -241,11 +256,13 @@ class GetConstructorDummy
241256
{
242257
protected $foo;
243258
private $bar;
259+
private $baz;
244260

245-
public function __construct($foo, $bar)
261+
public function __construct($foo, $bar, $baz)
246262
{
247263
$this->foo = $foo;
248264
$this->bar = $bar;
265+
$this->baz = $baz;
249266
}
250267

251268
public function getFoo()
@@ -258,6 +275,11 @@ public function getBar()
258275
return $this->bar;
259276
}
260277

278+
public function isBaz()
279+
{
280+
return $this->baz;
281+
}
282+
261283
public function otherMethod()
262284
{
263285
throw new \RuntimeException("Dummy::otherMethod() should not be called");

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy