Where.php 4.4 KB

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