use DateTime;
use SL::Helper::DateTime;
use List::MoreUtils qw(uniq);
+use SL::MoreCommon qw(listify);
use Data::Dumper;
my %filters = (
sub parse_filter {
my ($filter, %params) = @_;
- my $hint_objects = $params{with_objects} || [];
- my $auto_objects = [];
+ my $objects = $params{with_objects} || [];
- my ($flattened, $objects) = flatten($filter, $auto_objects, '', %params);
+ my ($flattened, $auto_objects) = flatten($filter, '', %params);
- if ($params{class}) {
- $objects = $hint_objects;
+ if (!$params{class}) {
+ _add_uniq($objects, $_) for @$auto_objects;
}
my $query = _parse_filter($flattened, $objects, %params);
}
sub flatten {
- my ($filter, $with_objects, $prefix, %params) = @_;
+ my ($filter, $prefix, %params) = @_;
- return (undef, $with_objects) unless 'HASH' eq ref $filter;
- $with_objects ||= [];
+ return (undef, []) unless 'HASH' eq ref $filter;
+ my $with_objects = [];
my @result;
while (my ($key, $value) = each %$filter) {
next if !defined $value || $value eq ''; # 0 is fine
if ('HASH' eq ref $value) {
- my ($query, $more_objects) = flatten($value, $with_objects, _prefix($prefix, $key));
- push @result, @$query if $query;
- push @$with_objects, _prefix($prefix, $key), ($more_objects ? @$more_objects : ());
+ my ($query, $more_objects) = flatten($value, _prefix($prefix, $key));
+ push @result, @$query if $query;
+ _add_uniq($with_objects, $_) for _prefix($prefix, $key), @$more_objects;
} else {
push @result, _prefix($prefix, $key) => $value;
}
if ($manager->can('filter')) {
($key, $value, my $obj) = $manager->filter($last_token, $value, $obj_prefix);
- _add_uniq($with_objects, $obj);
+ _add_uniq($with_objects, $obj) if $obj;
} else {
- _add_uniq($with_objects, $obj_path);
+ _add_uniq($with_objects, $obj_path) if $obj_path;
}
return ($key, $value);
my ($array, $what) = @_;
$array //= [];
- $array = [ uniq @$array, $what ];
+ @$array = (uniq @$array, listify($what));
}
sub _collapse_indirect_filters {