Cml\Db\MongoDB\MongoDB::conditionFactory PHP Метод

conditionFactory() публичный Метод

where 语句组装工厂
public conditionFactory ( string $column, array | integer | string $value, string $operator = '=' )
$column string 如 id user.id (这边的user为表别名如表pre_user as user 这边用user而非带前缀的原表名)
$value array | integer | string
$operator string 操作符
    public function conditionFactory($column, $value, $operator = '=')
    {
        $currentOrIndex = isset($this->sql['where']['$or']) ? count($this->sql['where']['$or']) - 1 : 0;
        if ($this->opIsAnd) {
            if (isset($this->sql['where'][$column][$operator])) {
                throw new \InvalidArgumentException('Mongodb Where Op key Is Exists[' . $column . $operator . ']');
            }
        } else {
            if ($this->bracketsIsOpen) {
                if (isset($this->sql['where']['$or'][$currentOrIndex][$column][$operator])) {
                    throw new \InvalidArgumentException('Mongodb Where Op key Is Exists[' . $column . $operator . ']');
                }
            }
        }
        switch ($operator) {
            case 'IN':
                // no break
            // no break
            case 'NOT IN':
                empty($value) && ($value = [0]);
                //这边可直接跳过不组装sql,但是为了给用户提示无条件 便于调试还是加上where field in(0)
                if ($this->opIsAnd) {
                    $this->sql['where'][$column][$operator == 'IN' ? '$in' : '$nin'] = $value;
                } else {
                    if ($this->bracketsIsOpen) {
                        $this->sql['where']['$or'][$currentOrIndex][$column][$operator == 'IN' ? '$in' : '$nin'] = $value;
                    } else {
                        $this->sql['where']['$or'][][$column] = $operator == 'IN' ? ['$in' => $value] : ['$nin' => $value];
                    }
                }
                break;
            case 'BETWEEN':
                if ($this->opIsAnd) {
                    $this->sql['where'][$column]['$gt'] = $value[0];
                    $this->sql['where'][$column]['$lt'] = $value[1];
                } else {
                    if ($this->bracketsIsOpen) {
                        $this->sql['where']['$or'][$currentOrIndex][$column]['$gt'] = $value[0];
                        $this->sql['where']['$or'][$currentOrIndex][$column]['$lt'] = $value[1];
                    } else {
                        $this->sql['where']['$or'][][$column] = ['$gt' => $value[0], '$lt' => $value[1]];
                    }
                }
                break;
            case 'NOT BETWEEN':
                if ($this->opIsAnd) {
                    $this->sql['where'][$column]['$lt'] = $value[0];
                    $this->sql['where'][$column]['$gt'] = $value[1];
                } else {
                    if ($this->bracketsIsOpen) {
                        $this->sql['where']['$or'][$currentOrIndex][$column]['$lt'] = $value[0];
                        $this->sql['where']['$or'][$currentOrIndex][$column]['$gt'] = $value[1];
                    } else {
                        $this->sql['where']['$or'][][$column] = ['$lt' => $value[0], '$gt' => $value[1]];
                    }
                }
                break;
            case 'IS NULL':
                if ($this->opIsAnd) {
                    $this->sql['where'][$column]['$in'] = [null];
                    $this->sql['where'][$column]['$exists'] = true;
                } else {
                    if ($this->bracketsIsOpen) {
                        $this->sql['where']['$or'][$currentOrIndex][$column]['$in'] = [null];
                        $this->sql['where']['$or'][$currentOrIndex][$column]['$exists'] = true;
                    } else {
                        $this->sql['where']['$or'][][$column] = ['$in' => [null], '$exists' => true];
                    }
                }
                break;
            case 'IS NOT NULL':
                if ($this->opIsAnd) {
                    $this->sql['where'][$column]['$ne'] = null;
                    $this->sql['where'][$column]['$exists'] = true;
                } else {
                    if ($this->bracketsIsOpen) {
                        $this->sql['where']['$or'][$currentOrIndex][$column]['$ne'] = null;
                        $this->sql['where']['$or'][$currentOrIndex][$column]['$exists'] = true;
                    } else {
                        $this->sql['where']['$or'][][$column] = ['$ne' => null, '$exists' => true];
                    }
                }
                break;
            case '>':
                //no break;
            //no break;
            case '<':
                if ($this->opIsAnd) {
                    $this->sql['where'][$column][$operator == '>' ? '$gt' : '$lt'] = $value;
                } else {
                    if ($this->bracketsIsOpen) {
                        $this->sql['where']['$or'][$currentOrIndex][$column][$operator == '>' ? '$gt' : '$lt'] = $value;
                    } else {
                        $this->sql['where']['$or'][][$column] = $operator == '>' ? ['$gt' => $value] : ['$lt' => $value];
                    }
                }
                break;
            case '>=':
                //no break;
            //no break;
            case '<=':
                if ($this->opIsAnd) {
                    $this->sql['where'][$column][$operator == '>=' ? '$gte' : '$lte'] = $value;
                } else {
                    if ($this->bracketsIsOpen) {
                        $this->sql['where']['$or'][$currentOrIndex][$column][$operator == '>=' ? '$gte' : '$lte'] = $value;
                    } else {
                        $this->sql['where']['$or'][][$column] = $operator == '>=' ? ['$gte' => $value] : ['$lte' => $value];
                    }
                }
                break;
            case 'NOT LIKE':
                if ($this->opIsAnd) {
                    $this->sql['where'][$column]['$not'] = new Regex($value, 'i');
                } else {
                    if ($this->bracketsIsOpen) {
                        $this->sql['where']['$or'][$currentOrIndex][$column]['$not'] = new Regex($value, 'i');
                    } else {
                        $this->sql['where']['$or'][][$column] = ['$not' => new Regex($value, 'i')];
                    }
                }
                break;
            case 'LIKE':
                //no break;
            //no break;
            case 'REGEXP':
                if ($this->opIsAnd) {
                    $this->sql['where'][$column]['$regex'] = $value;
                    $this->sql['where'][$column]['$options'] = '$i';
                } else {
                    if ($this->bracketsIsOpen) {
                        $this->sql['where']['$or'][$currentOrIndex][$column]['$regex'] = $value;
                        $this->sql['where']['$or'][$currentOrIndex][$column]['$options'] = '$i';
                    } else {
                        $this->sql['where']['$or'][][$column] = ['$regex' => $value, '$options' => '$i'];
                    }
                }
                break;
            case '!=':
                if ($this->opIsAnd) {
                    $this->sql['where'][$column]['$ne'] = $value;
                } else {
                    if ($this->bracketsIsOpen) {
                        $this->sql['where']['$or'][$currentOrIndex][$column]['$ne'] = $value;
                    } else {
                        $this->sql['where']['$or'][][$column] = ['$ne' => $value];
                    }
                }
                break;
            case '=':
                if ($this->opIsAnd) {
                    $this->sql['where'][$column] = $value;
                } else {
                    if ($this->bracketsIsOpen) {
                        $this->sql['where']['$or'][$currentOrIndex][$column] = $value;
                    } else {
                        $this->sql['where']['$or'][][$column] = $value;
                    }
                }
                break;
        }
    }