WhereIn.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace UCore\Helper\Model;
  3. /**
  4. * where in 条件交集封装
  5. *
  6. * 用于处理 where in 查询条件的交集操作
  7. */
  8. class WhereIn
  9. {
  10. /**
  11. * 构造函数
  12. *
  13. * @param array $ids 初始ID数组
  14. * @param int $no 无效ID值
  15. */
  16. public function __construct(public array $ids = [], private int $no = -999999)
  17. {
  18. }
  19. /**
  20. * 追加数据
  21. *
  22. * @param array $ids 要追加的ID数组
  23. * @return $this
  24. */
  25. public function addIds(array $ids): self
  26. {
  27. if ($ids) {
  28. if ($this->ids) {
  29. $this->ids = array_intersect($ids, $this->ids);
  30. if (!$this->ids) {
  31. $this->ids = [$this->no];
  32. }
  33. } else {
  34. $this->ids = $ids;
  35. }
  36. } else {
  37. $this->ids = [$this->no];
  38. }
  39. return $this;
  40. }
  41. /**
  42. * 检查是否有有效数据
  43. *
  44. * @return bool
  45. */
  46. public function notEmpty(): bool
  47. {
  48. return !empty($this->ids);
  49. }
  50. /**
  51. * 获取数据
  52. *
  53. * @return array
  54. */
  55. public function getData(): array
  56. {
  57. return $this->ids;
  58. }
  59. }