Marcelgwerder\ApiHandler\Parser::parseFilter PHP Метод

parseFilter() защищенный Метод

Parse the remaining filter params
protected parseFilter ( array $filterParams ) : void
$filterParams array
Результат void
    protected function parseFilter($filterParams)
    {
        $supportedPostfixes = ['st' => '<', 'gt' => '>', 'min' => '>=', 'max' => '<=', 'lk' => 'LIKE', 'not-lk' => 'NOT LIKE', 'in' => 'IN', 'not-in' => 'NOT IN', 'not' => '!='];
        $supportedPrefixesStr = implode('|', $supportedPostfixes);
        $supportedPostfixesStr = implode('|', array_keys($supportedPostfixes));
        foreach ($filterParams as $filterParamKey => $filterParamValue) {
            $keyMatches = [];
            //Matches every parameter with an optional prefix and/or postfix
            //e.g. not-title-lk, title-lk, not-title, title
            $keyRegex = '/^(?:(' . $supportedPrefixesStr . ')-)?(.*?)(?:-(' . $supportedPostfixesStr . ')|$)/';
            preg_match($keyRegex, $filterParamKey, $keyMatches);
            if (!isset($keyMatches[3])) {
                if (strtolower(trim($filterParamValue)) == 'null') {
                    $comparator = 'NULL';
                } else {
                    $comparator = '=';
                }
            } else {
                if (strtolower(trim($filterParamValue)) == 'null') {
                    $comparator = 'NOT NULL';
                } else {
                    $comparator = $supportedPostfixes[$keyMatches[3]];
                }
            }
            $column = $keyMatches[2];
            if ($comparator == 'IN') {
                $values = explode(',', $filterParamValue);
                $this->query->whereIn($column, $values);
            } else {
                if ($comparator == 'NOT IN') {
                    $values = explode(',', $filterParamValue);
                    $this->query->whereNotIn($column, $values);
                } else {
                    $values = explode('|', $filterParamValue);
                    if (count($values) > 1) {
                        $this->query->where(function ($query) use($column, $comparator, $values) {
                            foreach ($values as $value) {
                                if ($comparator == 'LIKE' || $comparator == 'NOT LIKE') {
                                    $value = preg_replace('/(^\\*|\\*$)/', '%', $value);
                                }
                                //Link the filters with AND of there is a "not" and with OR if there's none
                                if ($comparator == '!=' || $comparator == 'NOT LIKE') {
                                    $query->where($column, $comparator, $value);
                                } else {
                                    $query->orWhere($column, $comparator, $value);
                                }
                            }
                        });
                    } else {
                        $value = $values[0];
                        if ($comparator == 'LIKE' || $comparator == 'NOT LIKE') {
                            $value = preg_replace('/(^\\*|\\*$)/', '%', $value);
                        }
                        if ($comparator == 'NULL' || $comparator == 'NOT NULL') {
                            $this->query->whereNull($column, 'and', $comparator == 'NOT NULL');
                        } else {
                            $this->query->where($column, $comparator, $value);
                        }
                    }
                }
            }
        }
    }