Skip to content
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

[Feature] Mapping to camel case #198

Closed
wants to merge 5 commits into from

Conversation

reinfi
Copy link

@reinfi reinfi commented Jan 4, 2023

UseCase

I have a API which I consume and I want to map this to objects.

With PHP8.1 you have the wonderful support of readonly properties.

public readonly string $categoryId;

This works actual perfect with the json mapper and reduces the amount of boilerplate code to near zero.

My problem is, that the API I consume, often uses snake_case names and because of that I have to add setter functions to the model class. This leads to errors because you can not reset the property with a setter if it is a readonly property. There for I have to make it private and add a getter. All the boilerplate code is back.

Suggestion

So my suggestions is to add a new flag to support converting property keys to camelCase if they are not found.

I am not really confident with the naming so I am open for a better naming.

If you are fine with this I would add it to the readme to explain the usage.

@cweiske
Copy link
Owner

cweiske commented Jan 8, 2023

See #183:

I would accept a patch that, when $undefinedPropertyHandler returns a string, interprets that string as a property name and then sets the value on it.
The snake case patch here will not be merged.

@reinfi
Copy link
Author

reinfi commented Jan 8, 2023

ah, have not searches all closed pull-requests.

I will change it to your suggestion next week.

@cweiske cweiske closed this in cd83947 Jan 21, 2023
@cweiske
Copy link
Owner

cweiske commented Jan 21, 2023

Thank your for that patch! I've squashed + merged it.

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

Successfully merging this pull request may close these issues.

2 participants