protected function toSqlLevel(array $tree) : string
    {
        if (count($tree) == 0) {
            return '';
        }
        $sql = '(';
        reset($tree);
        if (is_numeric(key($tree)) === true) {
            foreach ($tree as $elem) {
                $sql .= $elem . ' AND ';
            }
            $sql = substr($sql, 0, -5);
        } elseif (key($tree) === 'OR') {
            foreach ($tree as $value) {
                if (is_array($value) === true) {
                    $sql .= $this->toSqlLevel($value);
                } else {
                    $sql .= $value;
                }
                $sql .= ' OR ';
            }
            $sql = substr($sql, 0, -4);
        } elseif (key($tree) === 'AND') {
            foreach ($tree as $value) {
                if (is_array($value) === true) {
                    $sql .= $this->toSqlLevel($value);
                } else {
                    $sql .= $value;
                }
                $sql .= ' AND ';
            }
            $sql = substr($sql, 0, -5);
        } else {
            foreach ($tree as $key => $value) {
                $sql .= $key . ' = ' . $value . ' AND ';
            }
            $sql = substr($sql, 0, -5);
        }
        $sql .= ')';

        return $sql;
    }
protected function toSqlLevel(array $tree) : string
    {
        if (count($tree) == 0) {
            return '';
        }
        $sql = '(';
        reset($tree);
        if (is_numeric(key($tree)) === true) {
            foreach ($tree as $elem) {
                $sql .= $elem . ' AND ';
            }
            $sql = substr($sql, 0, -5);
        } elseif (key($tree) === 'OR') {
            foreach ($tree as $value) {
                if (is_array($value) === true) {
                    $sql .= $this->toSqlLevel($value);
                } else {
                    $sql .= $value;
                }
                $sql .= ' OR ';
            }
            $sql = substr($sql, 0, -4);
        } elseif (key($tree) === 'AND') {
            foreach ($tree as $value) {
                if (is_array($value) === true) {
                    $sql .= $this->toSqlLevel($value);
                } else {
                    $sql .= $value;
                }
                $sql .= ' AND ';
            }
            $sql = substr($sql, 0, -5);
        } else {
            foreach ($tree as $key => $value) {
                $sql .= $key . ' = ' . $value . ' AND ';
            }
            $sql = substr($sql, 0, -5);
        }
        $sql .= ')';

        return $sql;
    }
protected function toSqlLevel ( array $ tree ) : string { if ( count ( $ tree ) == 0 ) { return '' ; } $ sql = '(' ; reset ( $ tree ) ; if ( is_numeric ( key ( $ tree ) ) === true ) { foreach ( $ tree as $ elem ) { $ sql .= $ elem . ' AND ' ; } $ sql = substr ( $ sql , 0 , - 5 ) ; } elseif ( key ( $ tree ) === 'OR' ) { foreach ( $ tree as $ value ) { if ( is_array ( $ value ) === true ) { $ sql .= $ this -> toSqlLevel ( $ value ) ; } else { $ sql .= $ value ; } $ sql .= ' OR ' ; } $ sql = substr ( $ sql , 0 , - 4 ) ; } elseif ( key ( $ tree ) === 'AND' ) { foreach ( $ tree as $ value ) { if ( is_array ( $ value ) === true ) { $ sql .= $ this -> toSqlLevel ( $ value ) ; } else { $ sql .= $ value ; } $ sql .= ' AND ' ; } $ sql = substr ( $ sql , 0 , - 5 ) ; } else { foreach ( $ tree as $ key => $ value ) { $ sql .= $ key . ' = ' . $ value . ' AND ' ; } $ sql = substr ( $ sql , 0 , - 5 ) ; } $ sql .= ')' ; return $ sql ; }
Extract the corresponding SQL code, depth level by level.

@param  array  $tree one tree level
@return string generated SQL code for this level
Extract the corresponding SQL code depth level by level .