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
This commit is contained in:
Fabien Potencier 2014-03-03 16:53:45 +01:00
commit 4a1639acc7
3 changed files with 48 additions and 18 deletions

View File

@ -1,6 +1,11 @@
CHANGELOG
=========
2.5.0
-----
* added support for `is.*` getters in `GetSetMethodNormalizer`
2.4.0
-----

View File

@ -88,7 +88,7 @@ class GetSetMethodNormalizer extends SerializerAwareNormalizer implements Normal
$attributes = array();
foreach ($reflectionMethods as $method) {
if ($this->isGetMethod($method)) {
$attributeName = lcfirst(substr($method->name, 3));
$attributeName = lcfirst(substr($method->name, 0 === strpos($method->name, 'is') ? 2 : 3));
if (in_array($attributeName, $this->ignoredAttributes)) {
continue;
@ -214,17 +214,19 @@ class GetSetMethodNormalizer extends SerializerAwareNormalizer implements Normal
}
/**
* Checks if a method's name is get.* and can be called without parameters.
* Checks if a method's name is get.* or is.*, and can be called without parameters.
*
* @param \ReflectionMethod $method the method to check
*
* @return Boolean whether the method is a getter.
* @return Boolean whether the method is a getter or boolean getter.
*/
private function isGetMethod(\ReflectionMethod $method)
{
$methodLength = strlen($method->name);
return (
0 === strpos($method->name, 'get') &&
3 < strlen($method->name) &&
((0 === strpos($method->name, 'get') && 3 < $methodLength) ||
(0 === strpos($method->name, 'is') && 2 < $methodLength)) &&
0 === $method->getNumberOfRequiredParameters()
);
}

View File

@ -30,6 +30,7 @@ class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase
$object = new \stdClass();
$obj->setFoo('foo');
$obj->setBar('bar');
$obj->setBaz(true);
$obj->setCamelCase('camelcase');
$obj->setObject($object);
@ -44,6 +45,7 @@ class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase
array(
'foo' => 'foo',
'bar' => 'bar',
'baz' => true,
'fooBar' => 'foobar',
'camelCase' => 'camelcase',
'object' => 'string_object',
@ -55,12 +57,13 @@ class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase
public function testDenormalize()
{
$obj = $this->normalizer->denormalize(
array('foo' => 'foo', 'bar' => 'bar', 'fooBar' => 'foobar'),
array('foo' => 'foo', 'bar' => 'bar', 'baz' => true, 'fooBar' => 'foobar'),
__NAMESPACE__.'\GetSetDummy',
'any'
);
$this->assertEquals('foo', $obj->getFoo());
$this->assertEquals('bar', $obj->getBar());
$this->assertTrue($obj->isBaz());
}
public function testDenormalizeOnCamelCaseFormat()
@ -99,10 +102,11 @@ class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase
public function testConstructorDenormalize()
{
$obj = $this->normalizer->denormalize(
array('foo' => 'foo', 'bar' => 'bar', 'fooBar' => 'foobar'),
array('foo' => 'foo', 'bar' => 'bar', 'baz' => true, 'fooBar' => 'foobar'),
__NAMESPACE__.'\GetConstructorDummy', 'any');
$this->assertEquals('foo', $obj->getFoo());
$this->assertEquals('bar', $obj->getBar());
$this->assertTrue($obj->isBaz());
}
/**
@ -112,7 +116,7 @@ class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase
{
$this->normalizer->setCallbacks($callbacks);
$obj = new GetConstructorDummy('', $value);
$obj = new GetConstructorDummy('', $value, true);
$this->assertEquals(
$result,
@ -128,18 +132,19 @@ class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase
{
$this->normalizer->setCallbacks(array('bar' => null));
$obj = new GetConstructorDummy('baz', 'quux');
$obj = new GetConstructorDummy('baz', 'quux', true);
$this->normalizer->normalize($obj, 'any');
}
public function testIgnoredAttributes()
{
$this->normalizer->setIgnoredAttributes(array('foo', 'bar', 'camelCase', 'object'));
$this->normalizer->setIgnoredAttributes(array('foo', 'bar', 'baz', 'camelCase', 'object'));
$obj = new GetSetDummy();
$obj->setFoo('foo');
$obj->setBar('bar');
$obj->setBaz(true);
$this->assertEquals(
array('fooBar' => 'foobar'),
@ -157,7 +162,7 @@ class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase
},
),
'baz',
array('foo' => '', 'bar' => 'baz'),
array('foo' => '', 'bar' => 'baz', 'baz' => true),
'Change a string',
),
array(
@ -167,7 +172,7 @@ class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase
},
),
'baz',
array('foo' => '', 'bar' => null),
array('foo' => '', 'bar' => null, 'baz' => true),
'Null an item'
),
array(
@ -177,7 +182,7 @@ class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase
},
),
new \DateTime('2011-09-10 06:30:00'),
array('foo' => '', 'bar' => '10-09-2011 06:30:00'),
array('foo' => '', 'bar' => '10-09-2011 06:30:00', 'baz' => true),
'Format a date',
),
array(
@ -191,8 +196,8 @@ class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase
return $foos;
},
),
array(new GetConstructorDummy('baz', ''), new GetConstructorDummy('quux', '')),
array('foo' => '', 'bar' => 'bazquux'),
array(new GetConstructorDummy('baz', '', false), new GetConstructorDummy('quux', '', false)),
array('foo' => '', 'bar' => 'bazquux', 'baz' => true),
'Collect a property',
),
array(
@ -201,8 +206,8 @@ class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase
return count($bars);
},
),
array(new GetConstructorDummy('baz', ''), new GetConstructorDummy('quux', '')),
array('foo' => '', 'bar' => 2),
array(new GetConstructorDummy('baz', '', false), new GetConstructorDummy('quux', '', false)),
array('foo' => '', 'bar' => 2, 'baz' => true),
'Count a property',
),
);
@ -229,6 +234,7 @@ class GetSetDummy
{
protected $foo;
private $bar;
private $baz;
protected $camelCase;
protected $object;
@ -252,6 +258,16 @@ class GetSetDummy
$this->bar = $bar;
}
public function isBaz()
{
return $this->baz;
}
public function setBaz($baz)
{
$this->baz = $baz;
}
public function getFooBar()
{
return $this->foo.$this->bar;
@ -287,11 +303,13 @@ class GetConstructorDummy
{
protected $foo;
private $bar;
private $baz;
public function __construct($foo, $bar)
public function __construct($foo, $bar, $baz)
{
$this->foo = $foo;
$this->bar = $bar;
$this->baz = $baz;
}
public function getFoo()
@ -304,6 +322,11 @@ class GetConstructorDummy
return $this->bar;
}
public function isBaz()
{
return $this->baz;
}
public function otherMethod()
{
throw new \RuntimeException("Dummy::otherMethod() should not be called");