Where.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace think\db;
  13. use ArrayAccess;
  14. /**
  15. * 数组查询对象
  16. */
  17. class Where implements ArrayAccess
  18. {
  19. /**
  20. * 查询表达式
  21. * @var array
  22. */
  23. protected $where = [];
  24. /**
  25. * 是否需要把查询条件两边增加括号
  26. * @var bool
  27. */
  28. protected $enclose = false;
  29. /**
  30. * 创建一个查询表达式
  31. *
  32. * @param array $where 查询条件数组
  33. * @param bool $enclose 是否增加括号
  34. */
  35. public function __construct(array $where = [], bool $enclose = false)
  36. {
  37. $this->where = $where;
  38. $this->enclose = $enclose;
  39. }
  40. /**
  41. * 设置是否添加括号
  42. * @access public
  43. * @param bool $enclose
  44. * @return $this
  45. */
  46. public function enclose(bool $enclose = true)
  47. {
  48. $this->enclose = $enclose;
  49. return $this;
  50. }
  51. /**
  52. * 解析为Query对象可识别的查询条件数组
  53. * @access public
  54. * @return array
  55. */
  56. public function parse(): array
  57. {
  58. $where = [];
  59. foreach ($this->where as $key => $val) {
  60. if ($val instanceof Raw) {
  61. $where[] = [$key, 'exp', $val];
  62. } elseif (is_null($val)) {
  63. $where[] = [$key, 'NULL', ''];
  64. } elseif (is_array($val)) {
  65. $where[] = $this->parseItem($key, $val);
  66. } else {
  67. $where[] = [$key, '=', $val];
  68. }
  69. }
  70. return $this->enclose ? [$where] : $where;
  71. }
  72. /**
  73. * 分析查询表达式
  74. * @access protected
  75. * @param string $field 查询字段
  76. * @param array $where 查询条件
  77. * @return array
  78. */
  79. protected function parseItem(string $field, array $where = []): array
  80. {
  81. $op = $where[0];
  82. $condition = $where[1] ?? null;
  83. if (is_array($op)) {
  84. // 同一字段多条件查询
  85. array_unshift($where, $field);
  86. } elseif (is_null($condition)) {
  87. if (is_string($op) && in_array(strtoupper($op), ['NULL', 'NOTNULL', 'NOT NULL'], true)) {
  88. // null查询
  89. $where = [$field, $op, ''];
  90. } elseif (is_null($op) || '=' == $op) {
  91. $where = [$field, 'NULL', ''];
  92. } elseif ('<>' == $op) {
  93. $where = [$field, 'NOTNULL', ''];
  94. } else {
  95. // 字段相等查询
  96. $where = [$field, '=', $op];
  97. }
  98. } else {
  99. $where = [$field, $op, $condition];
  100. }
  101. return $where;
  102. }
  103. /**
  104. * 修改器 设置数据对象的值
  105. * @access public
  106. * @param string $name 名称
  107. * @param mixed $value 值
  108. * @return void
  109. */
  110. public function __set($name, $value)
  111. {
  112. $this->where[$name] = $value;
  113. }
  114. /**
  115. * 获取器 获取数据对象的值
  116. * @access public
  117. * @param string $name 名称
  118. * @return mixed
  119. */
  120. public function __get($name)
  121. {
  122. return $this->where[$name] ?? null;
  123. }
  124. /**
  125. * 检测数据对象的值
  126. * @access public
  127. * @param string $name 名称
  128. * @return bool
  129. */
  130. public function __isset($name)
  131. {
  132. return isset($this->where[$name]);
  133. }
  134. /**
  135. * 销毁数据对象的值
  136. * @access public
  137. * @param string $name 名称
  138. * @return void
  139. */
  140. public function __unset($name)
  141. {
  142. unset($this->where[$name]);
  143. }
  144. // ArrayAccess
  145. public function offsetSet($name, $value)
  146. {
  147. $this->__set($name, $value);
  148. }
  149. public function offsetExists($name)
  150. {
  151. return $this->__isset($name);
  152. }
  153. public function offsetUnset($name)
  154. {
  155. $this->__unset($name);
  156. }
  157. public function offsetGet($name)
  158. {
  159. return $this->__get($name);
  160. }
  161. }