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;
    }