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

Add possibility to get data without nulls in map transformation #87

Closed
MuminovicAnel opened this issue Sep 25, 2024 · 3 comments
Closed

Comments

@MuminovicAnel
Copy link

Hello!

Your package is really great and convenient for the use case of our team. :)

I wanted to know if there as a possiblity to add the option to get data without null properties from the DTO ?

Because I need to overwrite the toJson or toArray methods like this in every DTO.

public function toJson($options = 0): string
{
        $dataWithoutNulls = array_filter($this->buildDataForExport(), fn($value) => !is_null($value));
        return json_encode($dataWithoutNulls, $options);
}

Thanks!

@WendellAdriel WendellAdriel pinned this issue Sep 25, 2024
@WendellAdriel
Copy link
Owner

Hey @MuminovicAnel.
I think this is a use case that should be handled in the userland, not in the library itself.

IMO the best way of dealing with this would be to create a BaseDTO class that extends from the SimpleDTO or ValidatedDTO one and on it you can add two new methods: toNonNullableJson and toNonNullableArray then in your DTOs, extend from your own BaseDTO class instead.

A second approach is to create a Trait and add these two methods in the Trait and in the DTOs you need that you can add the trait to it.

@MuminovicAnel
Copy link
Author

Hey @WendellAdriel

Thanks for the suggestion, I will go with the trait, that was a good idea. It doesn't add too much code and it lets the user use it or not.

Have a nice day!

@WendellAdriel
Copy link
Owner

Yeah, the best part of the Trait approach is that you can use it only when needed. 🥳
Have a great day over there as well!

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

No branches or pull requests

2 participants