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

Filtering out elements where value is null in Transformer #458

Open
kickthemooon opened this issue Aug 2, 2018 · 4 comments
Open

Filtering out elements where value is null in Transformer #458

kickthemooon opened this issue Aug 2, 2018 · 4 comments
Labels

Comments

@kickthemooon
Copy link

kickthemooon commented Aug 2, 2018

Example:

return [
  'name' => null
]

This element is not filtered out. Is there some way to tell Fractal to filter out null elements or does it have to be done manually?

@kickthemooon kickthemooon changed the title Filtering elements where value is null in Transformer Filtering out elements where value is null in Transformer Aug 2, 2018
@willishq
Copy link
Member

It'll have to be done manually, this is not really in the scope of Fractal.

If a value is meant to be in the response but there is no value for it, the property should still be there but not have a value in my opinion, otherwise you're putting responsibility on your consumer to check if the keys are there in the response, which shouldn't happen.

Imagine every property on the object needing the API consumer to check if it exists?

@johannesschobel
Copy link

i totally agree with @willishq .. it is better to have a "stable interface / structure" for the return types of a transformer..

however, if you want to filter out some properties that are explicitly set to null you can use the array_filter() method.. please note that this function may also remove falsy values, like false or ''..

I think, this issue can be closed @kickthemooon

@hubertnnn
Copy link

I think both ways are valid.
I prefer keeping the key, but I can see why some people might want to remove null values from response (eg. to reduce network usage or hide some fields).
But for that the best way is to create a custom serializer (or modify existing one).
The default should still be keeping the keys.

Also maybe having a way to say that something does not exist (as opposed to being empty) might be a solution.
So adding a new keyword like this:

protected $defaultIncludes = [ 'a',  'b',  'c'];

public function includeA($item)
{
        return $this->primitive(1);
}

public function includeB($item)
{
        return $this->null();
}

public function includeC($item)
{
        return $this->skip();
}

To return

{
  "a": 1,
  "b": null
}

@stale
Copy link

stale bot commented Apr 16, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 4 weeks if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Apr 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants