-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] PropertyNormalizer: a new normalizer that maps an object's properties to an array #9708
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
Conversation
* | ||
* @param string $class | ||
* | ||
* @return boolean |
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.
upper case 👶
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.
OK I did a good search on the docs, couldn't find anything about that. Then I searched the issue & PRs and I see it's been a debated subject ;)
I'll comply, even though I don't think it's a good thing. I'm sure Mr. Boole would be ok with us being more consistent ;)
…object's properties to an array
|
||
$attributeValue = $property->getValue($object); | ||
|
||
if (array_key_exists($property->name, $this->callbacks)) { |
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.
isset()
will be enough as the callback can't be null
anyway.
@lsmith77 Can you have a look at this one? |
👍 for this feature |
$reflectionObject = new \ReflectionObject($object); | ||
$attributes = array(); | ||
|
||
foreach ($reflectionObject->getProperties() as $property) { |
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.
You could just use foreach ($object as $property => $name) {
no? Probably would run faster than reflection.
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.
Ah no I see you override the visibility below sorry.. I thought the point of this class was to use public properties only. Nevermind then.
Looks fine to me except for the outdated CHANGELOG which should now be 2.6 |
Thank you @mnapoli. |
…maps an object's properties to an array (mnapoli) This PR was merged into the 2.6-dev branch. Discussion ---------- [Serializer] PropertyNormalizer: a new normalizer that maps an object's properties to an array | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | if PR is deemed mergeable, I'll write the docs This PR adds a new Normalizer for the Serializer component: **`PropertyNormalizer`**. Currently the only normalizer is `GetSetMethodNormalizer`, which calls getters and setters. This new serializer uses the properties values directly. This is especially useful if you write a webservice and take/return very simple DTO (Data Transfer Objects) which role is only to act like a "named" `stdClass`. Every property is public (the class doesn't contain any logic), and mapping that to an array is pretty easy. This normalizer takes into account public, but also *private* and *protected* properties. FYI I've based most of the code of `GetSetMethodNormalizer`. Commits ------- 78ceed1 [Serializer] Added PropertyNormalizer, a new normalizer that maps an object's properties to an array
@mnapoli Can you submit a PR on the docs for this new feature? Thanks again. |
Yay managed to do the PR for the docs: symfony/symfony-docs#4308! Thanks for the merge! |
…zer (mnapoli, WouterJ) This PR was merged into the 2.6 branch. Discussion ---------- Finish #4308: Documentation for the new PropertyNormalizer Finishes #4308 | Q | A | --- | --- | Doc fix? | no | New docs? | yes (symfony/symfony#9708) | Applies to | 2.6+ | Fixed tickets | #4272 Commits ------- 3b96301 Applied comments 629c8ac Added line returns… 6868a19 Updated the rendering of the list 4815c4a Introduction about normalizers 421aa41 ref #4272 Documentation for the new PropertyNormalizer introduced in symfony/symfony#9708
I usually find dangerous using in require libraries such as |
This PR adds a new Normalizer for the Serializer component:
PropertyNormalizer
.Currently the only normalizer is
GetSetMethodNormalizer
, which calls getters and setters. This new serializer uses the properties values directly.This is especially useful if you write a webservice and take/return very simple DTO (Data Transfer Objects) which role is only to act like a "named"
stdClass
. Every property is public (the class doesn't contain any logic), and mapping that to an array is pretty easy.This normalizer takes into account public, but also private and protected properties.
FYI I've based most of the code of
GetSetMethodNormalizer
.