Skip to content

[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

Merged
merged 1 commit into from
Sep 24, 2014

Conversation

mnapoli
Copy link
Contributor

@mnapoli mnapoli commented Dec 5, 2013

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.

*
* @param string $class
*
* @return boolean
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

upper case 👶

Copy link
Contributor Author

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 ;)


$attributeValue = $property->getValue($object);

if (array_key_exists($property->name, $this->callbacks)) {
Copy link
Contributor

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.

@fabpot
Copy link
Member

fabpot commented Dec 29, 2013

@lsmith77 Can you have a look at this one?

@DavidBadura
Copy link
Contributor

👍 for this feature

$reflectionObject = new \ReflectionObject($object);
$attributes = array();

foreach ($reflectionObject->getProperties() as $property) {
Copy link
Member

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.

Copy link
Member

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.

@Seldaek
Copy link
Member

Seldaek commented Sep 24, 2014

Looks fine to me except for the outdated CHANGELOG which should now be 2.6

@fabpot
Copy link
Member

fabpot commented Sep 24, 2014

Thank you @mnapoli.

@fabpot fabpot merged commit 78ceed1 into symfony:master Sep 24, 2014
fabpot added a commit that referenced this pull request Sep 24, 2014
…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
@fabpot
Copy link
Member

fabpot commented Sep 24, 2014

@mnapoli Can you submit a PR on the docs for this new feature? Thanks again.

@mnapoli
Copy link
Contributor Author

mnapoli commented Oct 9, 2014

Yay managed to do the PR for the docs: symfony/symfony-docs#4308! Thanks for the merge!

mnapoli added a commit to mnapoli/symfony-docs that referenced this pull request Nov 1, 2014
weaverryan added a commit to symfony/symfony-docs that referenced this pull request Mar 14, 2015
…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
@pounard
Copy link
Contributor

pounard commented Aug 7, 2019

I usually find dangerous using in require libraries such as nikic/php-parser, whereas it proves itself very useful, it also got me some trouble at least once or twice, because it's rather common to end up with dev dependencies that uses it, with different version constraints, which might cause some packages to become uninstallable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants