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

This commit is contained in:
Jordan Samouh 2017-06-30 10:33:54 -04:00
parent ac107ba6b2
commit a15829d524
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;
}
}