123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- <?php
- namespace app\common\util;
- /**
- * where语句构建器
- */
- class WhereBuilder implements \ArrayAccess
- {
- private $where = [];
- public static function builder()
- {
- return new WhereBuilder;
- }
- /**
- * 添加条件
- *
- * @param string|\think\db\Raw $column 数据库字段名
- * @param string $expr 表达式
- * @param mixed $condition 查询条件
- * @param bool $when 为false时不添加
- *
- * @return \app\common\util\WhereBuilder
- */
- public function where($column, $expr = null, $condition = null, $when = true)
- {
- if ($when) {
- $this->where[] = [$column, $expr, $condition];
- }
- return $this;
- }
- /**
- * 一对一对应的构建,条件为空时不添加
- *
- * @param string $column 数据库字段名
- * @param string $expr 表达式
- * @param mixed $condition 查询条件
- *
- * @return \app\common\util\WhereBuilder
- */
- public function corres($column, $expr, $condition)
- {
- if (!empty($condition)) {
- $this->where($column, $expr, $condition);
- }
- return $this;
- }
- /**
- * 简单eq
- *
- * @param mixed $column 数据库字段名
- * @param mixed $condition 表达式
- * @param bool $when 为true时一定会添加,其余时刻判断数据是否为空
- *
- * @return \app\common\util\WhereBuilder
- */
- public function eq($column, $condition, $when = false)
- {
- $valid = false;
- if (is_int($condition)) {
- $valid = isset($condition);
- } elseif (is_string($condition)) {
- $valid = !empty($condition) || $condition === '0';
- } else {
- $valid = !empty($condition);
- }
- $this->where($column, '=', $condition, $when || $valid);
- return $this;
- }
- /**
- * 简单not eq
- *
- * @param mixed $column 数据库字段名
- * @param mixed $condition 表达式
- * @param bool $when 为true时一定会添加,其余时刻判断数据是否为空
- *
- * @return \app\common\util\WhereBuilder
- */
- public function neq($column, $condition, $when = false)
- {
- $valid = false;
- if (is_int($condition)) {
- $valid = isset($condition);
- } elseif (is_string($condition)) {
- $valid = !empty($condition) || $condition === '0';
- } else {
- $valid = !empty($condition);
- }
- $this->where($column, '<>', $condition, $when || $valid);
- return $this;
- }
- /**
- * %like%
- *
- * @param mixed $column 数据库字段名
- * @param mixed $condition 表达式
- * @param bool $when 为true时一定会添加,其余时刻判断数据是否为空
- *
- * @return \app\common\util\WhereBuilder
- */
- public function like($column, ?string $condition = '', $when = false)
- {
- $condition = $condition ?: '';
- $this->where($column, 'like', "%{$condition}%", $when || !empty($condition));
- return $this;
- }
- public function in($column, ?array $condition = [], $exclude = [], $when = false)
- {
- $condition = $condition ?: [];
- $condition = array_diff($condition, $exclude);
- $this->where($column, 'in', $condition, $when || !empty($condition));
- return $this;
- }
- public function isnull($column, $when = false)
- {
- $this->where($column, 'null', null, $when);
- return $this;
- }
- public function notnull($column, $when = false)
- {
- $this->where($column, 'not null', null, $when);
- return $this;
- }
- public function between($column, $left, $right, $when = false)
- {
- if ($when || (self::empty($left) && self::empty($right))) {
- $this->where($column, 'between', [$left, $right]);
- return $this;
- } elseif (self::empty($left) && !self::empty($right)) {
- $this->where($column, '>=', $left);
- return $this;
- } elseif (!self::empty($left) && self::empty($right)) {
- $this->where($column, '<=', $left);
- return $this;
- } else {
- return $this;
- }
- }
- /**
- * 直接添加
- *
- * @param array $where
- *
- * @return \app\common\util\WhereBuilder
- */
- public function push($where, $when = true)
- {
- if (!$when) {
- return $this;
- }
- $this->where = array_merge($this->where, $where);
- return $this;
- }
- /**
- * 构建where
- *
- * @return array
- */
- public function build()
- {
- return $this->where;
- }
- public function offsetSet($offset, $value)
- {
- if (is_null($offset)) {
- $this->where[] = $value;
- } else {
- $this->where[$offset] = $value;
- }
- }
- public function offsetExists($offset)
- {
- return isset($this->where[$offset]);
- }
- public function offsetUnset($offset)
- {
- unset($this->where[$offset]);
- }
- public function offsetGet($offset)
- {
- return isset($this->where[$offset]) ? $this->where[$offset] : null;
- }
- private static function empty($condition)
- {
- $valid = false;
- if (is_int($condition)) {
- $valid = isset($condition);
- } elseif (is_string($condition)) {
- $valid = !empty($condition) || $condition === '0';
- } else {
- $valid = !empty($condition);
- }
- return $valid;
- }
- }
|