feature #23337 [Component][Serializer][Normalizer] : Deal it with Has Method for the Normalizer/Denormalizer (jordscream)

This PR was merged into the 3.4 branch.

Discussion
----------

[Component][Serializer][Normalizer] : Deal it with Has Method for the Normalizer/Denormalizer

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | ?
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #23314
| License       | MIT
| Doc PR        | symfony/symfony-docs

Deal it with Has Method for the Normalizer/Denormalizer

Commits
-------

a15829d524 [Component][Serializer][Normalizer] : Deal it with Has Method for the Normalizer/Denormalizer
This commit is contained in:
Fabien Potencier 2017-07-06 09:04:02 +03:00
commit f7bca74cef
2 changed files with 39 additions and 1 deletions

View File

@ -87,7 +87,8 @@ class GetSetMethodNormalizer extends AbstractObjectNormalizer
!$method->isStatic() &&
(
((0 === strpos($method->name, 'get') && 3 < $methodLength) ||
(0 === strpos($method->name, 'is') && 2 < $methodLength)) &&
(0 === strpos($method->name, 'is') && 2 < $methodLength) ||
(0 === strpos($method->name, 'has') && 3 < $methodLength)) &&
0 === $method->getNumberOfRequiredParameters()
)
;
@ -133,6 +134,11 @@ class GetSetMethodNormalizer extends AbstractObjectNormalizer
if (is_callable(array($object, $isser))) {
return $object->$isser();
}
$haser = 'has'.$ucfirsted;
if (is_callable(array($object, $haser))) {
return $object->$haser();
}
}
/**

View File

@ -497,6 +497,23 @@ class GetSetMethodNormalizerTest extends TestCase
$this->assertEquals('bar', $obj->getFoo());
}
public function testHasGetterDenormalize()
{
$obj = $this->normalizer->denormalize(array('foo' => true), ObjectWithHasGetterDummy::class);
$this->assertTrue($obj->hasFoo());
}
public function testHasGetterNormalize()
{
$obj = new ObjectWithHasGetterDummy();
$obj->setFoo(true);
$this->assertEquals(
array('foo' => true),
$this->normalizer->normalize($obj, 'any')
);
}
public function testMaxDepth()
{
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
@ -807,3 +824,18 @@ class ObjectWithJustStaticSetterDummy
self::$foo = $foo;
}
}
class ObjectWithHasGetterDummy
{
private $foo;
public function setFoo($foo)
{
$this->foo = $foo;
}
public function hasFoo()
{
return $this->foo;
}
}