Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b6fa50b

Browse files
committed
ticket_686 : Deal it with Has Method for the Normalizer/Denormalizer
1 parent 7787343 commit b6fa50b

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ private function isGetMethod(\ReflectionMethod $method)
8787
!$method->isStatic() &&
8888
(
8989
((0 === strpos($method->name, 'get') && 3 < $methodLength) ||
90-
(0 === strpos($method->name, 'is') && 2 < $methodLength)) &&
90+
(0 === strpos($method->name, 'is') && 2 < $methodLength) ||
91+
(0 === strpos($method->name, 'has') && 3 < $methodLength)) &&
9192
0 === $method->getNumberOfRequiredParameters()
9293
)
9394
;
@@ -133,6 +134,11 @@ protected function getAttributeValue($object, $attribute, $format = null, array
133134
if (is_callable(array($object, $isser))) {
134135
return $object->$isser();
135136
}
137+
138+
$haser = 'has'.$ucfirsted;
139+
if (is_callable(array($object, $haser))) {
140+
return $object->$haser();
141+
}
136142
}
137143

138144
/**

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,23 @@ public function testPrivateSetter()
497497
$this->assertEquals('bar', $obj->getFoo());
498498
}
499499

500+
public function testHasGetterDenormalize()
501+
{
502+
$obj = $this->normalizer->denormalize(array('foo' => true), __NAMESPACE__.'\ObjectWithHasGetterDummy');
503+
$this->assertEquals(true, $obj->hasFoo());
504+
}
505+
506+
public function testHasGetterNormalize()
507+
{
508+
$obj = new ObjectWithHasGetterDummy();
509+
$obj->setFoo(true);
510+
511+
$this->assertEquals(
512+
array('foo' => true),
513+
$this->normalizer->normalize($obj, 'any')
514+
);
515+
}
516+
500517
public function testMaxDepth()
501518
{
502519
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
@@ -807,3 +824,18 @@ public static function setFoo($foo)
807824
self::$foo = $foo;
808825
}
809826
}
827+
828+
class ObjectWithHasGetterDummy
829+
{
830+
private $foo = null;
831+
832+
public function setFoo($foo)
833+
{
834+
$this->foo = $foo;
835+
}
836+
837+
public function hasFoo()
838+
{
839+
return $this->foo;
840+
}
841+
}

0 commit comments

Comments
 (0)