instead an addIfTrue or addWhen method could be used, to adding the item to a collection if it a condition is met.
example of method:
/**
* Add an item to the collection if it is not empty.
*
* @param mixed $item
* @return $this
*/
public function addIfTrue($item)
{
if (!empty($item)) {
$this->items[] = $item;
}
return $this;
}
I was looking at the when method on the collection, but if a lot of variables from the outside scope is needed in the callback function, it does involve writing a lot more code.
In many cases, I find my self having to declare variables, only for doing a simple validation before adding it to a collection.
instead an addIfTrue or addWhen method could be used, to adding the item to a collection if it a condition is met. example of method:
example of usage:
I was looking at the when method on the collection, but if a lot of variables from the outside scope is needed in the callback function, it does involve writing a lot more code.