Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@JMS\Exclude only if a property is empty

I am using JMS\Serializer in my project and I want to ignore one property only if the array in it is empty.

I tried something like :

@JMS\Exclude(if="count('$this->required') === 0")
or 
@JMS\Exclude(if="empty('required')")

but got a syntax error.

Can anyone help me on this?

thank.

like image 399
Mathieu Delisle Avatar asked Feb 04 '23 13:02

Mathieu Delisle


1 Answers

What you need was implemented recently and it is in release-1.7 so you might as well wait for it. It is called @SkipWhenEmpty

@SkipWhenEmpty This annotation can be defined on a property to indicate that the property should not be serialized if the result will be "empty".

This is the bug related it.

like image 186
BentCoder Avatar answered Feb 08 '23 15:02

BentCoder