Make sure that a condition is either 'or' or 'and'.

@param $condition
@return string
@throws QBParseException
328533
protected function validateCondition($condition)
    {
        $condition = trim(strtolower($condition));

        if ($condition !== 'and' && $condition !== 'or') {
            throw new QBParseException("Condition can only be one of: 'and', 'or'.");
        }

        return $condition;
    }
913307
private function parseCondition($condition, $parameters = [])
	{
		if (strpos($condition, ' ')) {
			$match = preg_match('~^
				(.+)\s                          ## identifier 
				(
					(?:\$\w+) |                 ## $mongoOperator
					(?:[A-Z]+(?:_[A-Z]+)*) |    ## NAMED_OPERATOR or 
					(?:[\<\>\!]?\=|\>|\<\>?)    ## logical operator
				)	
				(?:
					\s%(\w+(?:\[\])?) |         ## modifier or
					\s(.+)                      ## value
				)?$~xs', $condition, $cond);

			//['cond IN' => [...]], ['cond = %s' => 'param'], ['cond $gt' => 20]
			if (!empty($match)) {

				if (substr($cond[1], 0, 1) === $this->cmd) {
					throw new InvalidArgumentException("Field name cannot start with '{$this->cmd}'");
				}

				if ($parameters === [] && !isset($cond[4])) {
					throw new InvalidArgumentException("Missing value for item '{$cond[1]}'");
				}

				return $this->formatCondition($cond[1], trim($cond[2], $this->cmd), isset($cond[4]) ? $cond[4] : $parameters, isset($cond[3]) ? $cond[3] : NULL);
			}
		}

		if ($parameters === []) {
			throw new InvalidArgumentException("Missing value for item '{$condition}'");
		}

		if (is_array($parameters) && ($value = reset($parameters))) {

			//['$cond' => $param[]]
			if (substr($condition, 0, 1) === $this->cmd) {
				return [$condition => $this->parseDeepCondition($parameters, TRUE)];
			}
			//['cond' => ['param', ...]]
			if (substr($key = key($parameters), 0, 1) !== $this->cmd) {
				return $this->formatCondition($condition, 'IN', $parameters);
			}
			//['cond' => ['$param' => [...]]]
			if (is_array($value)) {
				return [$condition => [$key => $this->parseDeepCondition($value)]];
			}
		}

		// [cond => param]
		return [$condition => $parameters];
	}
978799
public function possible($condition) {
      $query = $this->buildStrategyCondition($condition, Possible::create());
      return $this->process($query);
    }
116704
function _condition()
    {
        $result = $this->_expression();
        if (PEAR::isError($result)) {
            return $result;
        }
        if ($this->_current_token == SPREADSHEET_EXCEL_WRITER_LT) {
            $this->_advance();
            $result2 = $this->_expression();
            if (PEAR::isError($result2)) {
                return $result2;
            }
            $result = $this->_createTree('ptgLT', $result, $result2);
        } elseif ($this->_current_token == SPREADSHEET_EXCEL_WRITER_GT) {
            $this->_advance();
            $result2 = $this->_expression();
            if (PEAR::isError($result2)) {
                return $result2;
            }
            $result = $this->_createTree('ptgGT', $result, $result2);
        } elseif ($this->_current_token == SPREADSHEET_EXCEL_WRITER_LE) {
            $this->_advance();
            $result2 = $this->_expression();
            if (PEAR::isError($result2)) {
                return $result2;
            }
            $result = $this->_createTree('ptgLE', $result, $result2);
        } elseif ($this->_current_token == SPREADSHEET_EXCEL_WRITER_GE) {
            $this->_advance();
            $result2 = $this->_expression();
            if (PEAR::isError($result2)) {
                return $result2;
            }
            $result = $this->_createTree('ptgGE', $result, $result2);
        } elseif ($this->_current_token == SPREADSHEET_EXCEL_WRITER_EQ) {
            $this->_advance();
            $result2 = $this->_expression();
            if (PEAR::isError($result2)) {
                return $result2;
            }
            $result = $this->_createTree('ptgEQ', $result, $result2);
        } elseif ($this->_current_token == SPREADSHEET_EXCEL_WRITER_NE) {
            $this->_advance();
            $result2 = $this->_expression();
            if (PEAR::isError($result2)) {
                return $result2;
            }
            $result = $this->_createTree('ptgNE', $result, $result2);
        } elseif ($this->_current_token == SPREADSHEET_EXCEL_WRITER_CONCAT) {
            $this->_advance();
            $result2 = $this->_expression();
            if (PEAR::isError($result2)) {
                return $result2;
        }
            $result = $this->_createTree('ptgConcat', $result, $result2);
        }
        return $result;
    }
1052150
protected function handleJoinConditions(array $conditions)
    {
        if (empty($conditions)) {
            return '';
        }
        $sql[] = $this->{$conditions[0]['type']}($conditions[0]);
        $count = count($conditions);
        for ($i = 1; $i < $count; $i++) {
            $sql[] = $conditions[$i]['separator'] . ' ' . $this->{$conditions[$i]['type']}($conditions[$i]);
        }
        return implode(' ', $sql);
    }
1269149
public function validateCondition($condition)
    {
        $is_boolean = true;
        if (!empty($condition)) {
            $encapsuledElements = array();
            preg_match_all(
                KeywordableTraitInterface::GET_BALANCED_BRACKETS,
                $condition,
                $encapsuledElements
            );
            if (count($encapsuledElements[0]) > 0) {
                foreach ($encapsuledElements[0] as $key => $encapsuledElement) {
                    $is_boolean = (preg_match(KeywordableTraitInterface::IS_AND_BOOLEAN, $encapsuledElements[1][$key]) ||
                        preg_match(KeywordableTraitInterface::IS_OR_BOOLEAN, $encapsuledElements[1][$key])) &&
                        $is_boolean;
                    $condition = preg_replace('/'.preg_quote($encapsuledElement).'/', '##', $condition, 1);
                    if (count($encapsuledElements[0]) > 0) {
                        $is_boolean = $is_boolean  && $this->validateCondition($condition);
                    }
                }
            } else {
                $is_boolean = (preg_match(KeywordableTraitInterface::IS_AND_BOOLEAN, $condition) ||
                    preg_match(KeywordableTraitInterface::IS_OR_BOOLEAN, $condition)) &&
                    $is_boolean;
            }
        }

        return $is_boolean;
    }
657067
public static function combine($conditions)
    {
        return Arr::join(' AND ', Arr::map(function ($condition) {
            return "(".Condition::render($condition).")";
        }, $conditions));
    }
1250094
public function orHaving(...$conditions) : self
    {
        $this->havingConditions = $this->conditionalQueryBuilder->addConditionToClause(
            $this->havingConditions, 'OR', ...$this->createConditionExpressions($conditions)
        );

        return $this;
    }
303296
public function andWhere(string ...$condition) : self
    {
        $this->whereConditions = $this->addConditionToClause(
            $this->whereConditions, 'AND', ...$condition);

        return $this;
    }
426547
public function andWithString(string $condition, ...$values): self
    {
        $this->parts[] = [
            'type' => 'AND',
            'condition' => $condition,
            'values' => $values,
        ];

        return $this;
    }
612789
public function addCondition(Condition $condition): Condition
    {
        $query = $this->connection->createQueryBuilder()
            ->insert('ngbm_rule_condition')
            ->values(
                [
                    'id' => ':id',
                    'status' => ':status',
                    'rule_id' => ':rule_id',
                    'type' => ':type',
                    'value' => ':value',
                ]
            )
            ->setValue(
                'id',
                $condition->id !== null ?
                    (int) $condition->id :
                    $this->connectionHelper->getAutoIncrementValue('ngbm_rule_condition')
            )
            ->setParameter('status', $condition->status, Type::INTEGER)
            ->setParameter('rule_id', $condition->ruleId, Type::INTEGER)
            ->setParameter('type', $condition->type, Type::STRING)
            ->setParameter('value', json_encode($condition->value), Type::STRING);

        $query->execute();

        $condition->id = $condition->id ?? (int) $this->connectionHelper->lastInsertId('ngbm_rule_condition');

        return $condition;
    }
904731
public function parseCondition($condition) {
        $condition = str_ireplace(array_keys($this->comparison),array_values($this->comparison),$condition);
        $condition = preg_replace('/\$(\w+):(\w+)\s/is','$\\1->\\2 ',$condition);
        switch(strtolower(C('TMPL_VAR_IDENTIFY'))) {
            case 'array': // 识别为数组
                $condition  =   preg_replace('/\$(\w+)\.(\w+)\s/is','$\\1["\\2"] ',$condition);
                break;
            case 'obj':  // 识别为对象
                $condition  =   preg_replace('/\$(\w+)\.(\w+)\s/is','$\\1->\\2 ',$condition);
                break;
            default:  // 自动判断数组或对象 只支持二维
                $condition  =   preg_replace('/\$(\w+)\.(\w+)\s/is','(is_array($\\1)?$\\1["\\2"]:$\\1->\\2) ',$condition);
        }
        if(false !== strpos($condition, '$Think'))
            $condition      =   preg_replace_callback('/(\$Think.*?)\s/is', array($this, 'parseThinkVar'), $condition);        
        return $condition;
    }
492938
public function buildHashCondition($condition, &$params)
    {
        $parts = [];

        foreach ($condition as $column => $value) {
            if (ArrayHelper::isTraversable($value) || $value instanceof Query) {
                // IN condition
                $parts[] = $this->buildInCondition('IN', [$column, $value], $params);
            }
            else {
                if (strpos($column, '(') === false) {
                    $column = $this->db->quoteColumnName($column);
                }

                if ($value === null) {
                    $parts[] = "$column IS NULL";
                }
                elseif ($value instanceof Expression) {
                    $parts[] = "$column=" . $value->expression;

                    foreach ($value->params as $n => $v) {
                        $params[$n] = $v;
                    }
                }
                else {
                    $phName = self::PARAM_PREFIX . count($params);
                    $parts[] = "$column=$phName";
                    $params[$phName] = $value;
                }
            }
        }

        return count($parts) === 1 ? $parts[0] : '(' . implode(') AND (', $parts) . ')';
    }
715350
public function strict($condition) {
      $query = $this->buildStrategyCondition($condition, Strict::create());
      return $this->process($query);
    }
677843
protected function resolveConditionalLogic($condition)
    {
        $renderer = $this->getActionRenderer();
        if ($renderer && is_callable([ $renderer, $condition ])) {
            return !!$renderer->{$condition}();
        } elseif (is_callable([ $this, $condition ])) {
            return !!$this->{$condition}();
        } elseif (is_callable($condition)) {
            return !!$condition();
        } elseif ($renderer) {
            return !!$renderer->renderTemplate($condition);
        } elseif ($this->view()) {
            return !!$this->renderTemplate($condition);
        }

        return !!$condition;
    }
89890
public function filter(Condition $condition = null, int &$limit = null, int &$offset = null)
    {
        $offset = (int)$offset;

        if ($condition) {
            $limit = $condition->getLimit();
            $offset = (int)$condition->getOffset();
            $conditions = $condition->getConditions();
            return function (array $item) use ($conditions) {
                foreach ($conditions as $cond) {
                    if (!\is_array($cond)) {
                        continue;
                    }

                    $cond = \array_values($cond);
                    $type = \array_shift($cond);
                    $value = $item[$cond[0]] ?? null;
                    switch ($type) {
                        case Condition::EQUALS:
                            if (\is_array($value)) {
                                if (!\in_array($cond[1], $value, false)) {
                                    return false;
                                }
                            } else {
                                /** @noinspection TypeUnsafeComparisonInspection */
                                if ($value != $cond[1]) {
                                    return false;
                                }
                            }
                            break;
                        case Condition::NOT_EQUALS:
                            /** @noinspection TypeUnsafeComparisonInspection */
                            if ($value == $cond[1]) {
                                return false;
                            }
                            break;
                        case Condition::LESS_THAN:
                            if ($value >= $cond[1]) {
                                return false;
                            }
                            break;
                        case Condition::GREATER_THAN:
                            if ($value <= $cond[1]) {
                                return false;
                            }
                            break;
                        case Condition::LESS_OR_EQUALS:
                            if ($value > $cond[1]) {
                                return false;
                            }
                            break;
                        case Condition::GREATER_OR_EQUALS:
                            if ($value < $cond[1]) {
                                return false;
                            }
                            break;
                        case Condition::BETWEEN:
                            if ($value < $cond[1] || $value > $cond[2]) {
                                return false;
                            }
                            break;
                        case Condition::LIKE:
                            if (!\mb_strstr($value, $cond[1])) {
                                return false;
                            }
                            break;
                        case Condition::IN:
                            if (!\in_array($value, $cond[1], false)) {
                                return false;
                            }
                            break;
                    }
                }

                return true;
            };
        }

        return null;
    }
282628
protected function resolveConditionalLogic($condition)
    {
        if (is_callable([ $this, $condition ])) {
            return !!$this->{$condition}();
        } elseif (is_callable($condition)) {
            return !!$condition();
        } elseif ($this->view()) {
            return !!$this->renderTemplate($condition);
        }

        return !!$condition;
    }