src/QueryBuilder.php
/**
     * The basic smart method to build queries.
     * 
     * It will automatically identify the correct filter/query tool to use, as 
     * well the correct query part to be used in, depending on the content and
     * type of the key, value, and comparison parameters.
     * 
     * $comparison parameter will define what filter/query tool to use.
     * 
     * @param string|string[] $key the key(s) to check
     * @param string|string[] $value the value(s) to check against
     * @param string $compare the comparison operator or elastic query/filter tool
     *                        prefix it with ! to negate or with ? to convert to should
     * @param boolean $filter false to add as a query, true as a filter
     * @param array|map $params additional parameters the query/filter can use
     * @return static|QueryBuilder
     */
The basic smart method to build queries.

It will automatically identify the correct filter/query tool to use, as
well the correct query part to be used in, depending on the content and
type of the key, value, and comparison parameters.

$comparison parameter will define what filter/query tool to use.

@param string|string[] $key the key(s) to check
@param string|string[] $value the value(s) to check against
@param string $compare the comparison operator or elastic query/filter tool
prefix it with . to negate or with . to convert to should
@param boolean $filter false to add as a query, true as a filter
@param array|map $params additional parameters the query/filter can use
@return static|QueryBuilder
The basic smart method to build queries . It will automatically identify the correct filter / query tool to use as well the correct query part to be used in depending on the content and type of the key value and comparison parameters . $comparison parameter will define what filter / query tool to use . @param string|string [] $key the key ( s ) to check @param string|string [] $value the value ( s ) to check against @param string $compare the comparison operator or elastic query / filter tool prefix it with . to negate or with . to convert to should @param boolean $filter false to add as a query true as a filter @param array|map $params additional parameters the query / filter can use @return static|QueryBuilder
public function where($key, $value, $compare = "=", $filter = null, $params = []) {
        $compare = trim($compare);
        $bool = substr($compare, 0, 1) == "!" ? "must_not" : (substr($compare, 0, 1) == "?" ? "should" : "must");
        if ($bool !== "must") {
            $compare = substr($compare, 1);
        }
        $tool = $suffix = $prefix = null;
        $_filter = $filter;
        switch (strtolower(str_replace("_", " ", $compare))) {
            case '=':
            case 'equals':
            case 'term':
            default:
                $tool = "term";
                break;
            case '>':
            case 'gt':
                $tool = "range";
                $operator = "gt";
                break;
            case '<':
            case 'lt':
                $tool = "range";
                $operator = "lt";
                break;
            case '><':
            case '<>':
            case 'between':
                $tool = "range";
                $operator = ["gt", "lt"];
                break;
            case '>=<=':
            case '<=>=':
            case 'between from to':
                $tool = "range";
                $operator = ["gte", "lte"];
                break;
            case '>=<':
            case '<>=':
            case 'between from':
                $tool = "range";
                $operator = ["gte", "lt"];
                break;
            case '><=':
            case '<=>':
            case 'between to':
                $tool = "range";
                $operator = ["gt", "lte"];
                break;
            case '>=':
            case 'gte':
                $tool = "range";
                $operator = "gte";
                break;
            case '<=':
            case 'lte':
                $tool = "range";
                $operator = "lte";
                break;
            case '*=':
            case 'suffix':
            case 'suffixed':
            case 'ends with':
            case 'ends':
                $tool = "wildcard";
                $prefix = "*";
                $_filter = false;
                break;
            case '=*':
            case 'starts':
            case 'starts with':
            case 'prefix':
            case 'prefixed':
                $tool = "prefix";
                break;
            case '*=*':
            case 'like':
            case 'wildcard':
                $tool = "wildcard";
                $prefix = '*';
                $suffix = '*';
                $_filter = false;
                break;
            case '**':
            case 'r':
            case 'regexp':
            case 'regex':
            case 'rx':
                $tool = "regexp";
                break;
            case '*':
            case 'match':
                $tool = "match";
                $_filter = false;
                break;
        }
        if ($suffix || $prefix) {
            if (is_array($value)) {
                foreach ($value as $index => $singleValue) {
                    if (is_string($singleValue)) {
                        $value[$index] = $prefix . $singleValue . $suffix;
                    }
                }
            } else {
                $value = ($prefix? : "") . $value . ($suffix? : "");
            }
        }
        switch ($tool) {
            case 'match':
                return $this->match($key, $value, $bool, $this->shouldBeFilter($filter, $_filter), $params);
            case 'regexp':
                return $this->regexp($key, $value, $bool, $this->shouldBeFilter($filter, $_filter), $params);
            case 'term':
                return $this->term($key, $value, $bool, $this->shouldBeFilter($filter, $_filter), $params);
            case 'range':
                return $this->range($key, $operator, $value, $bool, $this->shouldBeFilter($filter, $_filter), $params);
            case 'wildcard':
                return $this->wildcard($key, $value, $bool, $this->shouldBeFilter($filter, $_filter), $params);
            case 'prefix':
                return $this->prefix($key, $value, $bool, $this->shouldBeFilter($filter, $_filter), $params);
        }
    }
public function where ( $ key , $ value , $ compare = " = " , $ filter = null , $ params = [ ] ) { $ compare = trim ( $ compare ) ; $ bool = substr ( $ compare , 0 , 1 ) == " ! " ? " must_not " : ( substr ( $ compare , 0 , 1 ) == " ? " ? " should " : " must " ) ; if ( $ bool !== " must " ) { $ compare = substr ( $ compare , 1 ) ; } $ tool = $ suffix = $ prefix = null ; $ _filter = $ filter ; switch ( strtolower ( str_replace ( " _ " , " " , $ compare ) ) ) { case '=' : case 'equals' : case 'term' : default : $ tool = " term " ; break ; case '>' : case 'gt' : $ tool = " range " ; $ operator = " gt " ; break ; case '<' : case 'lt' : $ tool = " range " ; $ operator = " lt " ; break ; case '><' : case '<>' : case 'between' : $ tool = " range " ; $ operator = [ " gt " , " lt " ] ; break ; case '>=<=' : case '<=>=' : case 'between from to' : $ tool = " range " ; $ operator = [ " gte " , " lte " ] ; break ; case '>=<' : case '<>=' : case 'between from' : $ tool = " range " ; $ operator = [ " gte " , " lt " ] ; break ; case '><=' : case '<=>' : case 'between to' : $ tool = " range " ; $ operator = [ " gt " , " lte " ] ; break ; case '>=' : case 'gte' : $ tool = " range " ; $ operator = " gte " ; break ; case '<=' : case 'lte' : $ tool = " range " ; $ operator = " lte " ; break ; case '*=' : case 'suffix' : case 'suffixed' : case 'ends with' : case 'ends' : $ tool = " wildcard " ; $ prefix = " * " ; $ _filter = false ; break ; case '=*' : case 'starts' : case 'starts with' : case 'prefix' : case 'prefixed' : $ tool = " prefix " ; break ; case '*=*' : case 'like' : case 'wildcard' : $ tool = " wildcard " ; $ prefix = '*' ; $ suffix = '*' ; $ _filter = false ; break ; case '**' : case 'r' : case 'regexp' : case 'regex' : case 'rx' : $ tool = " regexp " ; break ; case '*' : case 'match' : $ tool = " match " ; $ _filter = false ; break ; } if ( $ suffix || $ prefix ) { if ( is_array ( $ value ) ) { foreach ( $ value as $ index => $ singleValue ) { if ( is_string ( $ singleValue ) ) { $ value [ $ index ] = $ prefix . $ singleValue . $ suffix ; } } } else { $ value = ( $ prefix ? : " " ) . $ value . ( $ suffix ? : " " ) ; } } switch ( $ tool ) { case 'match' : return $ this -> match ( $ key , $ value , $ bool , $ this -> shouldBeFilter ( $ filter , $ _filter ) , $ params ) ; case 'regexp' : return $ this -> regexp ( $ key , $ value , $ bool , $ this -> shouldBeFilter ( $ filter , $ _filter ) , $ params ) ; case 'term' : return $ this -> term ( $ key , $ value , $ bool , $ this -> shouldBeFilter ( $ filter , $ _filter ) , $ params ) ; case 'range' : return $ this -> range ( $ key , $ operator , $ value , $ bool , $ this -> shouldBeFilter ( $ filter , $ _filter ) , $ params ) ; case 'wildcard' : return $ this -> wildcard ( $ key , $ value , $ bool , $ this -> shouldBeFilter ( $ filter , $ _filter ) , $ params ) ; case 'prefix' : return $ this -> prefix ( $ key , $ value , $ bool , $ this -> shouldBeFilter ( $ filter , $ _filter ) , $ params ) ; } }
The basic smart method to build queries.
The basic smart method to build queries .
//identify the bool part must|should|must_not //get the correct compare value //$suffix and $prefix will be used in regex, wildcard, prefix, and suffix //queries. //$_filter is the real identifier for the filter //identify the tool, operator, and part //add prefix/suffix to each element in array values //call the real query builder method