|
- <?php
-
- namespace App\Kintone;
-
- use Closure;
- use Illuminate\Support\Carbon;
- use Illuminate\Support\Collection;
-
- class KintoneRecordQuery
- {
-
- private Collection $query;
- private string $order = "";
- private string $appName;
-
-
- public function __construct(string $appName)
- {
- $this->query = collect();
- $this->appName = $appName;
- }
-
- public function toQuery(): string
- {
- $ret = "";
- foreach ($this->query as $ele) {
- $ret .= $ele;
- $ret .= " ";
- }
- $ret .= $this->order;
- logger(sprintf("QUERY[%s]:%s", $this->appName, $ret));
- return $ret;
- }
-
- /**
- * @param string|\Closure(static $query): static $column
- */
- public function where(string|Closure $column, string|int|null $condition = null, KintoneRecordQueryOperator $operator = KintoneRecordQueryOperator::EQ)
- {
- return $this->whereQuery("and", $operator->value, $column, $condition);
- }
- /**
- * @param string|\Closure(static $query): static $column
- */
- public function notWhere(string|Closure $column, string|int|null $condition = null, KintoneRecordQueryOperator $operator = KintoneRecordQueryOperator::NEQ)
- {
- return $this->whereQuery("and", $operator->value, $column, $condition);
- }
- /**
- * @param string|\Closure(static $query): static $column
- */
- public function orWhere(string|Closure $column, string|int|null $condition = null, KintoneRecordQueryOperator $operator = KintoneRecordQueryOperator::EQ)
- {
- $this->whereQuery("or", $operator->value, $column, $condition);
- return $this;
- }
- /**
- * @param string|\Closure(static $query): static $column
- */
- public function notOrWhere(string|Closure $column, string|int|null $condition = null, KintoneRecordQueryOperator $operator = KintoneRecordQueryOperator::NEQ)
- {
- return $this->whereQuery("or", $operator->value, $column, $condition);
- }
- /**
- * @param string|\Closure(static $query): static $column
- */
- public function whereIn(string|Closure $column, array $condition)
- {
- return $this->whereQuery("and", "in", $column, $condition);
- }
- /**
- * @param string|\Closure(static $query): static $column
- */
- public function whereNotIn(string|Closure $column, array $condition)
- {
- return $this->whereQuery("and", "not in", $column, $condition);
- }
- /**
- * @param string|\Closure(static $query): static $column
- */
- public function orWhereIn(string|Closure $column, array $condition)
- {
- return $this->whereQuery("or", "not in", $column, $condition);
- }
- public function whereDate(string $column, Carbon $date, KintoneRecordQueryOperator $operator = KintoneRecordQueryOperator::EQ, bool $allowNull = false)
- {
- if (!$allowNull) {
- $this->whereNotNull($column);
- }
- return $this->whereQuery("and", $operator->value, $column, $date->format('Y-m-d'));
- }
- public function whereDateTime(string $column, Carbon $date, KintoneRecordQueryOperator $operator = KintoneRecordQueryOperator::EQ, bool $allowNull = false)
- {
- if (!$allowNull) {
- $this->whereNotNull($column);
- }
- return $this->whereQuery("and", $operator->value, $column, $date->toIso8601ZuluString());
- }
- public function orWhereDate(string $column, Carbon $date, KintoneRecordQueryOperator $operator = KintoneRecordQueryOperator::EQ, bool $allowNull = false)
- {
- if (!$allowNull) {
- $this->whereNotNull($column);
- }
- return $this->whereQuery("or", $operator->value, $column, $date->format('Y-m-d'));
- }
- public function orWhereDateTime(string $column, Carbon $date, KintoneRecordQueryOperator $operator = KintoneRecordQueryOperator::EQ, bool $allowNull = false)
- {
- if (!$allowNull) {
- $this->whereNotNull($column);
- }
- return $this->whereQuery("or", $operator->value, $column, $date->toIso8601ZuluString());
- }
- public function whereNull(string $column)
- {
- return $this->where($column, "");
- }
- public function owWhereNull(string $column)
- {
- return $this->orWhere($column, "");
- }
- public function whereNotNull(string $column)
- {
- return $this->notWhere($column, "");
- }
- public function owWhereNotNull(string $column)
- {
- return $this->notOrWhere($column, "");
- }
-
- public function orderByAsc(string $column)
- {
- return $this->orderBy($column, "asc");
- }
- public function orderByDesc(string $column)
- {
- return $this->orderBy($column, "desc");
- }
- private function orderBy(string $column, string $order)
- {
- $this->order = sprintf("order by %s %s", $column, $order);
- return $this;
- }
-
- /**
- * @param string|\Closure(static $query): static $column
- */
- private function whereQuery(
- string $andOr,
- string $operator,
- string|Closure $column,
- string|int|array|null $condition
- ) {
- if (!$this->query->isEmpty()) {
- $this->query->push($andOr);
- }
-
- if (is_string($column)) {
- if (is_string($condition)) {
- $this->query->push(sprintf(
- '%s %s "%s"',
- $column,
- $operator,
- $condition,
- ));
- return $this;
- }
- if (is_int($condition)) {
- $this->query->push(sprintf(
- '%s %s %d',
- $column,
- $operator,
- $condition,
- ));
- return $this;
- }
- if (is_array($condition)) {
- $in = "";
- foreach ($condition as $ele) {
- if (is_string($ele)) {
- if ($in !== "") {
- $in .= ", ";
- }
- $in .= sprintf('"%s"', $ele);
- }
- if (is_integer($ele)) {
- if ($in !== "") {
- $in .= ", ";
- }
- $in .= sprintf('%d', $ele);
- }
- }
- $this->query->push(sprintf(
- '%s %s (%s)',
- $column,
- $operator,
- $in,
- ));
- return $this;
- }
- }
-
-
- if ($column instanceof Closure) {
- $nestedCondition = new self($this->appName);
- $column($nestedCondition);
- $this->query->push(sprintf(
- '(%s)',
- $nestedCondition->toQuery()
- ));
- return $this;
- }
-
- return $this;
- }
- }
|