Skip to content

Commit bdd4b9d

Browse files
committed
feature #10314 [Serializer] added support for is.* methods in GetSetMethodNormalizer (tiraeth)
This PR was squashed before being merged into the 2.5-dev branch (closes #10314). Discussion ---------- [Serializer] added support for is.* methods in GetSetMethodNormalizer | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #10297 | License | MIT | Doc PR | N/A Using ``is`` prefix for boolean variables is considered a standard, not only in PHP but also in Java (from which Symfony2 derives lot standards). I was not sure if this is BCB but answered "no". Was not sure if I should update ``CHANGELOG`` file and did so, but if you find it irrelevant, go ahead and merge without it. I don't know if I should create a PR for docs because it does not explicitly say that the normalizer supports only ``get.*`` methods as getters. _Note: Objects that contain behaviour also can use other prefixes like ``can``, ``has``, ``should``, but their presence in ``GetSetMethodNormalizer`` is relevant as they do not provide state (``has`` is debatable)._ Commits ------- 480219f [Serializer] added support for is.* methods in GetSetMethodNormalizer
2 parents b326dce + edf13b5 commit bdd4b9d

File tree

3 files changed

+48
-18
lines changed

3 files changed

+48
-18
lines changed

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

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;
@@ -214,17 +214,19 @@ private function supports($class)
214214
}
215215

216216
/**
217-
* Checks if a method's name is get.* and can be called without parameters.
217+
* Checks if a method's name is get.* or is.*, and can be called without parameters.
218218
*
219219
* @param \ReflectionMethod $method the method to check
220220
*
221-
* @return Boolean whether the method is a getter.
221+
* @return Boolean whether the method is a getter or boolean getter.
222222
*/
223223
private function isGetMethod(\ReflectionMethod $method)
224224
{
225+
$methodLength = strlen($method->name);
226+
225227
return (
226-
0 === strpos($method->name, 'get') &&
227-
3 < strlen($method->name) &&
228+
((0 === strpos($method->name, 'get') && 3 < $methodLength) ||
229+
(0 === strpos($method->name, 'is') && 2 < $methodLength)) &&
228230
0 === $method->getNumberOfRequiredParameters()
229231
);
230232
}

Tests/Normalizer/GetSetMethodNormalizerTest.php

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public function testNormalize()
3030
$object = new \stdClass();
3131
$obj->setFoo('foo');
3232
$obj->setBar('bar');
33+
$obj->setBaz(true);
3334
$obj->setCamelCase('camelcase');
3435
$obj->setObject($object);
3536

@@ -44,6 +45,7 @@ public function testNormalize()
4445
array(
4546
'foo' => 'foo',
4647
'bar' => 'bar',
48+
'baz' => true,
4749
'fooBar' => 'foobar',
4850
'camelCase' => 'camelcase',
4951
'object' => 'string_object',
@@ -55,12 +57,13 @@ public function testNormalize()
5557
public function testDenormalize()
5658
{
5759
$obj = $this->normalizer->denormalize(
58-
array('foo' => 'foo', 'bar' => 'bar', 'fooBar' => 'foobar'),
60+
array('foo' => 'foo', 'bar' => 'bar', 'baz' => true, 'fooBar' => 'foobar'),
5961
__NAMESPACE__.'\GetSetDummy',
6062
'any'
6163
);
6264
$this->assertEquals('foo', $obj->getFoo());
6365
$this->assertEquals('bar', $obj->getBar());
66+
$this->assertTrue($obj->isBaz());
6467
}
6568

6669
public function testDenormalizeOnCamelCaseFormat()
@@ -99,10 +102,11 @@ public function attributeProvider()
99102
public function testConstructorDenormalize()
100103
{
101104
$obj = $this->normalizer->denormalize(
102-
array('foo' => 'foo', 'bar' => 'bar', 'fooBar' => 'foobar'),
105+
array('foo' => 'foo', 'bar' => 'bar', 'baz' => true, 'fooBar' => 'foobar'),
103106
__NAMESPACE__.'\GetConstructorDummy', 'any');
104107
$this->assertEquals('foo', $obj->getFoo());
105108
$this->assertEquals('bar', $obj->getBar());
109+
$this->assertTrue($obj->isBaz());
106110
}
107111

108112
/**
@@ -112,7 +116,7 @@ public function testCallbacks($callbacks, $value, $result, $message)
112116
{
113117
$this->normalizer->setCallbacks($callbacks);
114118

115-
$obj = new GetConstructorDummy('', $value);
119+
$obj = new GetConstructorDummy('', $value, true);
116120

117121
$this->assertEquals(
118122
$result,
@@ -128,18 +132,19 @@ public function testUncallableCallbacks()
128132
{
129133
$this->normalizer->setCallbacks(array('bar' => null));
130134

131-
$obj = new GetConstructorDummy('baz', 'quux');
135+
$obj = new GetConstructorDummy('baz', 'quux', true);
132136

133137
$this->normalizer->normalize($obj, 'any');
134138
}
135139

136140
public function testIgnoredAttributes()
137141
{
138-
$this->normalizer->setIgnoredAttributes(array('foo', 'bar', 'camelCase', 'object'));
142+
$this->normalizer->setIgnoredAttributes(array('foo', 'bar', 'baz', 'camelCase', 'object'));
139143

140144
$obj = new GetSetDummy();
141145
$obj->setFoo('foo');
142146
$obj->setBar('bar');
147+
$obj->setBaz(true);
143148

144149
$this->assertEquals(
145150
array('fooBar' => 'foobar'),
@@ -157,7 +162,7 @@ public function provideCallbacks()
157162
},
158163
),
159164
'baz',
160-
array('foo' => '', 'bar' => 'baz'),
165+
array('foo' => '', 'bar' => 'baz', 'baz' => true),
161166
'Change a string',
162167
),
163168
array(
@@ -167,7 +172,7 @@ public function provideCallbacks()
167172
},
168173
),
169174
'baz',
170-
array('foo' => '', 'bar' => null),
175+
array('foo' => '', 'bar' => null, 'baz' => true),
171176
'Null an item'
172177
),
173178
array(
@@ -177,7 +182,7 @@ public function provideCallbacks()
177182
},
178183
),
179184
new \DateTime('2011-09-10 06:30:00'),
180-
array('foo' => '', 'bar' => '10-09-2011 06:30:00'),
185+
array('foo' => '', 'bar' => '10-09-2011 06:30:00', 'baz' => true),
181186
'Format a date',
182187
),
183188
array(
@@ -191,8 +196,8 @@ public function provideCallbacks()
191196
return $foos;
192197
},
193198
),
194-
array(new GetConstructorDummy('baz', ''), new GetConstructorDummy('quux', '')),
195-
array('foo' => '', 'bar' => 'bazquux'),
199+
array(new GetConstructorDummy('baz', '', false), new GetConstructorDummy('quux', '', false)),
200+
array('foo' => '', 'bar' => 'bazquux', 'baz' => true),
196201
'Collect a property',
197202
),
198203
array(
@@ -201,8 +206,8 @@ public function provideCallbacks()
201206
return count($bars);
202207
},
203208
),
204-
array(new GetConstructorDummy('baz', ''), new GetConstructorDummy('quux', '')),
205-
array('foo' => '', 'bar' => 2),
209+
array(new GetConstructorDummy('baz', '', false), new GetConstructorDummy('quux', '', false)),
210+
array('foo' => '', 'bar' => 2, 'baz' => true),
206211
'Count a property',
207212
),
208213
);
@@ -229,6 +234,7 @@ class GetSetDummy
229234
{
230235
protected $foo;
231236
private $bar;
237+
private $baz;
232238
protected $camelCase;
233239
protected $object;
234240

@@ -252,6 +258,16 @@ public function setBar($bar)
252258
$this->bar = $bar;
253259
}
254260

261+
public function isBaz()
262+
{
263+
return $this->baz;
264+
}
265+
266+
public function setBaz($baz)
267+
{
268+
$this->baz = $baz;
269+
}
270+
255271
public function getFooBar()
256272
{
257273
return $this->foo.$this->bar;
@@ -287,11 +303,13 @@ class GetConstructorDummy
287303
{
288304
protected $foo;
289305
private $bar;
306+
private $baz;
290307

291-
public function __construct($foo, $bar)
308+
public function __construct($foo, $bar, $baz)
292309
{
293310
$this->foo = $foo;
294311
$this->bar = $bar;
312+
$this->baz = $baz;
295313
}
296314

297315
public function getFoo()
@@ -304,6 +322,11 @@ public function getBar()
304322
return $this->bar;
305323
}
306324

325+
public function isBaz()
326+
{
327+
return $this->baz;
328+
}
329+
307330
public function otherMethod()
308331
{
309332
throw new \RuntimeException("Dummy::otherMethod() should not be called");

0 commit comments

Comments
 (0)