New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Serializer] added support for is.* methods in GetSetMethodNormalizer #10314
Closed
Closed
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,7 +88,7 @@ public function normalize($object, $format = null, array $context = array()) | |
$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; | ||
|
@@ -211,17 +211,19 @@ private function supports($class) | |
} | ||
|
||
/** | ||
* 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) | ||
{ | ||
return ( | ||
0 === strpos($method->name, 'get') && | ||
3 < strlen($method->name) && | ||
((0 === strpos($method->name, 'get') && | ||
3 < strlen($method->name)) || | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move |
||
(0 === strpos($method->name, 'is') && | ||
2 < strlen($method->name))) && | ||
0 === $method->getNumberOfRequiredParameters() | ||
); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,22 +26,24 @@ public function testNormalize() | |
$obj = new GetSetDummy(); | ||
$obj->setFoo('foo'); | ||
$obj->setBar('bar'); | ||
$obj->setBaz(true); | ||
$obj->setCamelCase('camelcase'); | ||
$this->assertEquals( | ||
array('foo' => 'foo', 'bar' => 'bar', 'fooBar' => 'foobar', 'camelCase' => 'camelcase'), | ||
array('foo' => 'foo', 'bar' => 'bar', 'baz' => true, 'fooBar' => 'foobar', 'camelCase' => 'camelcase'), | ||
$this->normalizer->normalize($obj, 'any') | ||
); | ||
} | ||
|
||
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->assertEquals(true, $obj->isBaz()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
} | ||
|
||
public function testDenormalizeOnCamelCaseFormat() | ||
|
@@ -80,10 +82,11 @@ public function attributeProvider() | |
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->assertEquals(true, $obj->isBaz()); | ||
} | ||
|
||
/** | ||
|
@@ -93,7 +96,7 @@ public function testCallbacks($callbacks, $value, $result, $message) | |
{ | ||
$this->normalizer->setCallbacks($callbacks); | ||
|
||
$obj = new GetConstructorDummy('', $value); | ||
$obj = new GetConstructorDummy('', $value, true); | ||
|
||
$this->assertEquals( | ||
$result, | ||
|
@@ -109,18 +112,19 @@ public function testUncallableCallbacks() | |
{ | ||
$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')); | ||
$this->normalizer->setIgnoredAttributes(array('foo', 'bar', 'baz', 'camelCase')); | ||
|
||
$obj = new GetSetDummy(); | ||
$obj->setFoo('foo'); | ||
$obj->setBar('bar'); | ||
$obj->setBaz(true); | ||
|
||
$this->assertEquals( | ||
array('fooBar' => 'foobar'), | ||
|
@@ -138,7 +142,7 @@ public function provideCallbacks() | |
}, | ||
), | ||
'baz', | ||
array('foo' => '', 'bar' => 'baz'), | ||
array('foo' => '', 'bar' => 'baz', 'baz' => true), | ||
'Change a string', | ||
), | ||
array( | ||
|
@@ -148,7 +152,7 @@ public function provideCallbacks() | |
}, | ||
), | ||
'baz', | ||
array('foo' => '', 'bar' => null), | ||
array('foo' => '', 'bar' => null, 'baz' => true), | ||
'Null an item' | ||
), | ||
array( | ||
|
@@ -158,7 +162,7 @@ public function provideCallbacks() | |
}, | ||
), | ||
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( | ||
|
@@ -172,8 +176,8 @@ public function provideCallbacks() | |
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( | ||
|
@@ -182,8 +186,8 @@ public function provideCallbacks() | |
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', | ||
), | ||
); | ||
|
@@ -194,6 +198,7 @@ class GetSetDummy | |
{ | ||
protected $foo; | ||
private $bar; | ||
private $baz; | ||
protected $camelCase; | ||
|
||
public function getFoo() | ||
|
@@ -216,6 +221,16 @@ public function setBar($bar) | |
$this->bar = $bar; | ||
} | ||
|
||
public function isBaz() | ||
{ | ||
return $this->baz; | ||
} | ||
|
||
public function setBaz($baz) | ||
{ | ||
$this->baz = $baz; | ||
} | ||
|
||
public function getFooBar() | ||
{ | ||
return $this->foo.$this->bar; | ||
|
@@ -241,11 +256,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() | ||
|
@@ -258,6 +275,11 @@ public function getBar() | |
return $this->bar; | ||
} | ||
|
||
public function isBaz() | ||
{ | ||
return $this->baz; | ||
} | ||
|
||
public function otherMethod() | ||
{ | ||
throw new \RuntimeException("Dummy::otherMethod() should not be called"); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be revereted. This file is auto-generated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thus, this is a new feature, so it won't go in 2.4.3 (and you cannot tell that 2.4.3 was released yesterday)