Skip to content

How to keep empty values in filtered array? #57

@Finesse

Description

@Finesse

What

I've found that Filter removes an empty value from the input array after you apply any filter on it:

$filter = new Filter();
$filter->all()->trim();
$result = $filter->filter([
    'one' => ' 1 ',
    'zero' => ' 0 ',
    'space' => ' ',
    'null' => null,
    'false' => false
]);
var_dump($result);
/*
[
    'one' => '1'
]
*/

In this case I don't want to remove empty values (especially the ' 0 ' value), I just want to trim them. If I wanted to remove empty values I would call:

$filter
    ->all()
    ->trim()
    ->callback(function ($value) { return $value === '' ? null : $value; })
    ->removeNull();

What is expected

All the array keys are preserved:

$filter = new Filter();
$filter->all()->trim();
$result = $filter->filter([
    'one' => ' 1 ',
    'zero' => ' 0 ',
    'space' => ' ',
    'null' => null,
    'false' => false
]);
var_dump($result);
/*
[
    'one' => '1',
    'zero' => '0',
    'space' => '',
    'null' => '', // or null
    'false' => ''
]
*/

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions