Locker\Data\Analytics\Analytics::setFilter PHP Method

setFilter() private method

Formats include: key => value - where key equals value key => array(foo, bar) - where key equals foo AND bar key => array(array(foo,bar)) - where key equals foo OR bar key => array(array(foo,bar), hello) - where key equals foo OR bar AND hello If there are no $in criteria (we can tell based on whether values = array) then we only need to pass to mongo query as a single array as $and is implied. However, if multiple criteria, we need to include $and hence the dual approach here.
private setFilter ( array $options ) : [String
$options array
return [String
    private function setFilter(array $options)
    {
        $use_and = false;
        $filter = [];
        $set_in = [];
        $set_inbetween = [];
        if ($options) {
            foreach ($options as $key => $value) {
                // Multiple elements so require '$and'.
                if (is_array($value)) {
                    $use_and = true;
                }
            }
            if (!$use_and) {
                foreach ($options as $key => $value) {
                    $filter[$key] = $value;
                }
            } else {
                foreach ($options as $key => $value) {
                    $in_statement = [];
                    if (is_array($value) && count($value) > 0) {
                        // Uses between ('<>') or in filter.
                        if ($value[0] == '<>') {
                            $set_inbetween[$key] = ['$gte' => (int) $value[1], '$lte' => (int) $value[2]];
                        } else {
                            foreach ($value as $v) {
                                $in_statement[] = $v;
                            }
                            $set_in[] = [$key => ['$in' => $in_statement]];
                        }
                    } else {
                        $set_in[] = [$key => $value];
                    }
                }
                // Use Mongo $and for this type of statement.
                if (!empty($set_in)) {
                    $filter = ['$and' => $set_in];
                }
                // Merges `and` and `between` if available.
                if (!empty($filter) && !empty($set_inbetween)) {
                    $filter = array_merge($filter, $set_inbetween);
                } elseif (!empty($set_inbetween)) {
                    $filter = $set_inbetween;
                }
            }
        }
        return $filter;
    }