You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 satır
5.3KB

  1. <?php
  2. namespace App\Kintone;
  3. use Closure;
  4. use Illuminate\Support\Carbon;
  5. use Illuminate\Support\Collection;
  6. class KintoneRecordQuery
  7. {
  8. private Collection $query;
  9. private string $order = "";
  10. private string $appName;
  11. public function __construct(string $appName)
  12. {
  13. $this->query = collect();
  14. $this->appName = $appName;
  15. }
  16. public function toQuery(): string
  17. {
  18. $ret = "";
  19. foreach ($this->query as $ele) {
  20. $ret .= $ele;
  21. $ret .= " ";
  22. }
  23. $ret .= $this->order;
  24. logger(sprintf("QUERY[%s]:%s", $this->appName, $ret));
  25. return $ret;
  26. }
  27. public function where(string|Closure $column, string|int|null $condition = null, KintoneRecordQueryOperator $operator = KintoneRecordQueryOperator::EQ)
  28. {
  29. return $this->whereQuery("and", $operator->value, $column, $condition);
  30. }
  31. public function notWhere(string|Closure $column, string|int|null $condition = null, KintoneRecordQueryOperator $operator = KintoneRecordQueryOperator::EQ)
  32. {
  33. return $this->whereQuery("and", $operator->value, $column, $condition);
  34. }
  35. public function orWhere(string|Closure $column, string|int|null $condition = null, KintoneRecordQueryOperator $operator = KintoneRecordQueryOperator::EQ)
  36. {
  37. $this->whereQuery("or", $operator->value, $column, $condition);
  38. return $this;
  39. }
  40. public function notOrWhere(string|Closure $column, string|int|null $condition = null, KintoneRecordQueryOperator $operator = KintoneRecordQueryOperator::EQ)
  41. {
  42. return $this->whereQuery("or", $operator->value, $column, $condition);
  43. }
  44. public function whereIn(string|Closure $column, array $condition)
  45. {
  46. return $this->whereQuery("and", "in", $column, $condition);
  47. }
  48. public function whereNotIn(string|Closure $column, array $condition)
  49. {
  50. return $this->whereQuery("and", "not in", $column, $condition);
  51. }
  52. public function orWhereIn(string|Closure $column, array $condition)
  53. {
  54. return $this->whereQuery("or", "not in", $column, $condition);
  55. }
  56. public function whereDate(string $column, Carbon $date, KintoneRecordQueryOperator $operator = KintoneRecordQueryOperator::EQ)
  57. {
  58. return $this->whereQuery("and", $operator->value, $column, $date->format('Y-m-d'));
  59. }
  60. public function whereDateTime(string $column, Carbon $date, KintoneRecordQueryOperator $operator = KintoneRecordQueryOperator::EQ)
  61. {
  62. return $this->whereQuery("and", $operator->value, $column, $date->toIso8601ZuluString());
  63. }
  64. public function orWhereDate(string $column, Carbon $date, KintoneRecordQueryOperator $operator = KintoneRecordQueryOperator::EQ)
  65. {
  66. return $this->whereQuery("or", $operator->value, $column, $date->format('Y-m-d'));
  67. }
  68. public function orWhereDateTime(string $column, Carbon $date, KintoneRecordQueryOperator $operator = KintoneRecordQueryOperator::EQ)
  69. {
  70. return $this->whereQuery("or", $operator->value, $column, $date->toIso8601ZuluString());
  71. }
  72. public function orderByAsc(string $column)
  73. {
  74. return $this->orderBy($column, "asc");
  75. }
  76. public function orderByDesc(string $column)
  77. {
  78. return $this->orderBy($column, "desc");
  79. }
  80. private function orderBy(string $column, string $order)
  81. {
  82. $this->order = sprintf("order by %s %s", $column, $order);
  83. return $this;
  84. }
  85. private function whereQuery(
  86. string $andOr,
  87. string $operator,
  88. string|Closure $column,
  89. string|int|array|null $condition
  90. ) {
  91. if (!$this->query->isEmpty()) {
  92. $this->query->push($andOr);
  93. }
  94. if (is_string($column)) {
  95. if (is_string($condition)) {
  96. $this->query->push(sprintf(
  97. '%s %s "%s"',
  98. $column,
  99. $operator,
  100. $condition,
  101. ));
  102. return $this;
  103. }
  104. if (is_int($condition)) {
  105. $this->query->push(sprintf(
  106. '%s %s %d',
  107. $column,
  108. $operator,
  109. $condition,
  110. ));
  111. return $this;
  112. }
  113. if (is_array($condition)) {
  114. $in = "";
  115. foreach ($condition as $ele) {
  116. if (is_string($ele)) {
  117. if ($in !== "") {
  118. $in .= ", ";
  119. }
  120. $in .= sprintf('"%s"', $ele);
  121. }
  122. if (is_integer($ele)) {
  123. if ($in !== "") {
  124. $in .= ", ";
  125. }
  126. $in .= sprintf('%d', $ele);
  127. }
  128. }
  129. $this->query->push(sprintf(
  130. '%s %s (%s)',
  131. $column,
  132. $operator,
  133. $in,
  134. ));
  135. return $this;
  136. }
  137. }
  138. if ($column instanceof Closure) {
  139. $nestedCondition = new self($this->appName);
  140. $column($nestedCondition);
  141. $this->query->push(sprintf(
  142. '(%s)',
  143. $nestedCondition->toQuery()
  144. ));
  145. return $this;
  146. }
  147. }
  148. }