WhereBuilder.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. namespace app\common\util;
  3. /**
  4. * where语句构建器
  5. */
  6. class WhereBuilder implements \ArrayAccess
  7. {
  8. private $where = [];
  9. public static function builder()
  10. {
  11. return new WhereBuilder;
  12. }
  13. /**
  14. * 添加条件
  15. *
  16. * @param string|\think\db\Raw $column 数据库字段名
  17. * @param string $expr 表达式
  18. * @param mixed $condition 查询条件
  19. * @param bool $when 为false时不添加
  20. *
  21. * @return \app\common\util\WhereBuilder
  22. */
  23. public function where($column, $expr = null, $condition = null, $when = true)
  24. {
  25. if ($when) {
  26. $this->where[] = [$column, $expr, $condition];
  27. }
  28. return $this;
  29. }
  30. /**
  31. * 一对一对应的构建,条件为空时不添加
  32. *
  33. * @param string $column 数据库字段名
  34. * @param string $expr 表达式
  35. * @param mixed $condition 查询条件
  36. *
  37. * @return \app\common\util\WhereBuilder
  38. */
  39. public function corres($column, $expr, $condition)
  40. {
  41. if (!empty($condition)) {
  42. $this->where($column, $expr, $condition);
  43. }
  44. return $this;
  45. }
  46. /**
  47. * 简单eq
  48. *
  49. * @param mixed $column 数据库字段名
  50. * @param mixed $condition 表达式
  51. * @param bool $when 为true时一定会添加,其余时刻判断数据是否为空
  52. *
  53. * @return \app\common\util\WhereBuilder
  54. */
  55. public function eq($column, $condition, $when = false)
  56. {
  57. $valid = false;
  58. if (is_int($condition)) {
  59. $valid = isset($condition);
  60. } elseif (is_string($condition)) {
  61. $valid = !empty($condition) || $condition === '0';
  62. } else {
  63. $valid = !empty($condition);
  64. }
  65. $this->where($column, '=', $condition, $when || $valid);
  66. return $this;
  67. }
  68. /**
  69. * 简单not eq
  70. *
  71. * @param mixed $column 数据库字段名
  72. * @param mixed $condition 表达式
  73. * @param bool $when 为true时一定会添加,其余时刻判断数据是否为空
  74. *
  75. * @return \app\common\util\WhereBuilder
  76. */
  77. public function neq($column, $condition, $when = false)
  78. {
  79. $valid = false;
  80. if (is_int($condition)) {
  81. $valid = isset($condition);
  82. } elseif (is_string($condition)) {
  83. $valid = !empty($condition) || $condition === '0';
  84. } else {
  85. $valid = !empty($condition);
  86. }
  87. $this->where($column, '<>', $condition, $when || $valid);
  88. return $this;
  89. }
  90. /**
  91. * %like%
  92. *
  93. * @param mixed $column 数据库字段名
  94. * @param mixed $condition 表达式
  95. * @param bool $when 为true时一定会添加,其余时刻判断数据是否为空
  96. *
  97. * @return \app\common\util\WhereBuilder
  98. */
  99. public function like($column, ?string $condition = '', $when = false)
  100. {
  101. $condition = $condition ?: '';
  102. $this->where($column, 'like', "%{$condition}%", $when || !empty($condition));
  103. return $this;
  104. }
  105. public function in($column, ?array $condition = [], $exclude = [], $when = false)
  106. {
  107. $condition = $condition ?: [];
  108. $condition = array_diff($condition, $exclude);
  109. $this->where($column, 'in', $condition, $when || !empty($condition));
  110. return $this;
  111. }
  112. public function isnull($column, $when = false)
  113. {
  114. $this->where($column, 'null', null, $when);
  115. return $this;
  116. }
  117. public function notnull($column, $when = false)
  118. {
  119. $this->where($column, 'not null', null, $when);
  120. return $this;
  121. }
  122. public function between($column, $left, $right, $when = false)
  123. {
  124. if ($when || (self::empty($left) && self::empty($right))) {
  125. $this->where($column, 'between', [$left, $right]);
  126. return $this;
  127. } elseif (self::empty($left) && !self::empty($right)) {
  128. $this->where($column, '>=', $left);
  129. return $this;
  130. } elseif (!self::empty($left) && self::empty($right)) {
  131. $this->where($column, '<=', $left);
  132. return $this;
  133. } else {
  134. return $this;
  135. }
  136. }
  137. /**
  138. * 直接添加
  139. *
  140. * @param array $where
  141. *
  142. * @return \app\common\util\WhereBuilder
  143. */
  144. public function push($where, $when = true)
  145. {
  146. if (!$when) {
  147. return $this;
  148. }
  149. $this->where = array_merge($this->where, $where);
  150. return $this;
  151. }
  152. /**
  153. * 构建where
  154. *
  155. * @return array
  156. */
  157. public function build()
  158. {
  159. return $this->where;
  160. }
  161. public function offsetSet($offset, $value)
  162. {
  163. if (is_null($offset)) {
  164. $this->where[] = $value;
  165. } else {
  166. $this->where[$offset] = $value;
  167. }
  168. }
  169. public function offsetExists($offset)
  170. {
  171. return isset($this->where[$offset]);
  172. }
  173. public function offsetUnset($offset)
  174. {
  175. unset($this->where[$offset]);
  176. }
  177. public function offsetGet($offset)
  178. {
  179. return isset($this->where[$offset]) ? $this->where[$offset] : null;
  180. }
  181. private static function empty($condition)
  182. {
  183. $valid = false;
  184. if (is_int($condition)) {
  185. $valid = isset($condition);
  186. } elseif (is_string($condition)) {
  187. $valid = !empty($condition) || $condition === '0';
  188. } else {
  189. $valid = !empty($condition);
  190. }
  191. return $valid;
  192. }
  193. }