<?php/***+------------------* madong*+------------------* Copyright (c) https://gitee.com/motion-code All rights reserved.*+------------------* Author: Mr. April (405784684@qq.com)*+------------------* Official Website: http://www.madong.tech*/namespace madong\query;use Illuminate\Database\Eloquent\Builder;/*** 查询条件对象* 封装单个查询条件,支持多种操作符*/class QueryCondition{/*** @param string $field 字段名* @param string $operator 操作符* @param mixed $value 值*/public function __construct(public readonly string $field,public readonly string $operator,public readonly mixed $value) {}/*** 应用到查询构建器** @param Builder $query* @return Builder*/public function apply(Builder $query): Builder{return match ($this->operator) {'eq' => $query->where($this->field, '=', $this->value),'ne' => $query->where($this->field, '!=', $this->value),'gt' => $query->where($this->field, '>', $this->value),'gte' => $query->where($this->field, '>=', $this->value),'lt' => $query->where($this->field, '<', $this->value),'lte' => $query->where($this->field, '<=', $this->value),'like' => $query->where($this->field, 'like', "%{$this->value}%"),'ilike' => $query->where($this->field, 'ilike', "%{$this->value}%"),'in' => $query->whereIn($this->field, is_array($this->value) ? $this->value : explode(',', $this->value)),'not_in' => $query->whereNotIn($this->field, is_array($this->value) ? $this->value : explode(',', $this->value)),'between' => $query->whereBetween($this->field, is_array($this->value) ? $this->value : explode(',', $this->value)),'not_between' => $query->whereNotBetween($this->field, is_array($this->value) ? $this->value : explode(',', $this->value)),'null' => $query->whereNull($this->field),'not_null' => $query->whereNotNull($this->field),default => $query,};}/*** 静态工厂方法:等于*/public static function eq(string $field, mixed $value): self{return new self($field, 'eq', $value);}/*** 静态工厂方法:不等于*/public static function ne(string $field, mixed $value): self{return new self($field, 'ne', $value);}/*** 静态工厂方法:大于*/public static function gt(string $field, mixed $value): self{return new self($field, 'gt', $value);}/*** 静态工厂方法:大于等于*/public static function gte(string $field, mixed $value): self{return new self($field, 'gte', $value);}/*** 静态工厂方法:小于*/public static function lt(string $field, mixed $value): self{return new self($field, 'lt', $value);}/*** 静态工厂方法:小于等于*/public static function lte(string $field, mixed $value): self{return new self($field, 'lte', $value);}/*** 静态工厂方法:模糊匹配*/public static function like(string $field, mixed $value): self{return new self($field, 'like', $value);}/*** 静态工厂方法:包含*/public static function in(string $field, mixed $value): self{return new self($field, 'in', $value);}/*** 静态工厂方法:范围*/public static function between(string $field, mixed $value): self{return new self($field, 'between', $value);}/*** 静态工厂方法:为空*/public static function null(string $field): self{return new self($field, 'null', null);}/*** 静态工厂方法:不为空*/public static function notNull(string $field): self{return new self($field, 'not_null', null);}/*** 转换为数组*/public function toArray(): array{return ['field' => $this->field,'operator' => $this->operator,'value' => $this->value,];}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。