代码拉取完成,页面将自动刷新
<?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 madong\query\enum\ParamFormat;
use madong\query\enum\QueryMode;
/**
* QuerySelect 配置类
* 用于灵活配置 selectInput 方法的返回格式和行为
*/
class QuerySelectConfig
{
/**
* 返回格式类型
* - array: 返回数组格式 [where, format, limit, field, order, page]
* - object: 返回对象格式(支持更多自定义字段)
* - compact: 返回紧凑格式,仅包含必要字段
*/
private string $returnFormat = 'array';
/**
* 参数格式限定
* - filters: 标准 filters 格式
* - uppercase: 大写前缀格式
* - array: 三元素数组格式
* - where: 直接 where 条件格式
* - auto: 自动检测格式
*/
private string $paramFormat = ParamFormat::AUTO->value;
/**
* 查询模式
* - standard: 标准模式
* - uppercase: 大写前缀模式
* - auto: 自动检测模式
*/
private string $queryMode = QueryMode::AUTO->value;
/**
* 跳过的字段(不在 where 条件中包含这些字段)
* @var array
*/
private array $skipFields = [];
/**
* 允许的字段白名单
* @var array|null
*/
private ?array $allowedFields = null;
/**
* 排序字段白名单
* @var array|null
*/
private ?array $sortFields = null;
/**
* 默认排序
* @var array
*/
private array $defaultSort = [];
/**
* 默认每页数量
*/
private int $defaultPageSize = 10;
/**
* 最大每页数量
*/
private int $maxPageSize = 100;
/**
* 默认页码
*/
private int $defaultPage = 1;
/**
* 关键词搜索字段
* @var array
*/
private array $keywordFields = [];
/**
* 是否启用关键词搜索
*/
private bool $enableKeywordSearch = false;
/**
* 关键词参数名
*/
private string $keywordParam = 'keyword';
/**
* 是否启用范围查询(between)
*/
private bool $enableRangeQuery = true;
/**
* 是否启用模糊匹配(like)
*/
private bool $enableLikeQuery = true;
/**
* 是否自动转换日期时间格式
*/
private bool $autoConvertDateTime = true;
/**
* 是否跳过空值
*/
private bool $skipEmptyValues = true;
/**
* 自定义返回字段
* @var array
*/
private array $customFields = [];
/**
* 是否启用缓存
*/
private bool $enableCache = false;
/**
* 缓存时间(秒)
*/
private int $cacheTtl = 3600;
/**
* 是否启用调试模式
*/
private bool $debugMode = false;
/**
* 过滤器格式
* - array: 数组格式 [[字段, 操作符, 值], ...]
* - keyvalue: 键值对格式 [字段 => 'op:value', ...]
*/
private string $filterFormat = 'keyvalue';
/**
* 默认操作符
* 用于没有明确指定操作符时的默认值,如 'eq', 'gt', 'lt', 'like' 等
*/
private string $defaultOperator = 'eq';
/**
* 创建配置实例
*
* @param array $config
* @return self
*/
public static function make(array $config = []): self
{
return new self($config);
}
/**
* 构造函数
*
* @param array $config
*/
public function __construct(array $config = [])
{
foreach ($config as $key => $value) {
// 将下划线命名转换为驼峰命名:custom_fields -> CustomFields
$method = 'set' . str_replace('_', '', ucwords($key, '_'));
if (method_exists($this, $method)) {
$this->$method($value);
}
}
}
// Getters
public function getReturnFormat(): string
{
return $this->returnFormat;
}
public function getParamFormat(): string
{
return $this->paramFormat;
}
public function getQueryMode(): string
{
return $this->queryMode;
}
public function getSkipFields(): array
{
return $this->skipFields;
}
public function getAllowedFields(): ?array
{
return $this->allowedFields;
}
public function getSortFields(): ?array
{
return $this->sortFields;
}
public function getDefaultSort(): array
{
return $this->defaultSort;
}
public function getDefaultPageSize(): int
{
return $this->defaultPageSize;
}
public function getMaxPageSize(): int
{
return $this->maxPageSize;
}
public function getDefaultPage(): int
{
return $this->defaultPage;
}
public function getKeywordFields(): array
{
return $this->keywordFields;
}
public function isEnableKeywordSearch(): bool
{
return $this->enableKeywordSearch;
}
public function getKeywordParam(): string
{
return $this->keywordParam;
}
public function isEnableRangeQuery(): bool
{
return $this->enableRangeQuery;
}
public function isEnableLikeQuery(): bool
{
return $this->enableLikeQuery;
}
public function isAutoConvertDateTime(): bool
{
return $this->autoConvertDateTime;
}
public function isSkipEmptyValues(): bool
{
return $this->skipEmptyValues;
}
public function getCustomFields(): array
{
return $this->customFields;
}
public function isEnableCache(): bool
{
return $this->enableCache;
}
public function getCacheTtl(): int
{
return $this->cacheTtl;
}
public function isDebugMode(): bool
{
return $this->debugMode;
}
public function getFilterFormat(): ?string
{
return $this->filterFormat;
}
public function getDefaultOperator(): string
{
return $this->defaultOperator;
}
// Setters
public function setReturnFormat(string $format): self
{
$this->returnFormat = in_array($format, ['array', 'object', 'compact']) ? $format : 'array';
return $this;
}
public function setParamFormat(string $format): self
{
if (ParamFormat::isValid($format)) {
$this->paramFormat = $format;
}
return $this;
}
public function setQueryMode(string $mode): self
{
if (QueryMode::isValid($mode)) {
$this->queryMode = $mode;
}
return $this;
}
public function setSkipFields(array $fields): self
{
$this->skipFields = $fields;
return $this;
}
public function setAllowedFields(?array $fields): self
{
$this->allowedFields = $fields;
return $this;
}
public function setSortFields(?array $fields): self
{
$this->sortFields = $fields;
return $this;
}
public function setDefaultSort(array $sort): self
{
$this->defaultSort = $sort;
return $this;
}
public function setDefaultPageSize(int $size): self
{
$this->defaultPageSize = max(1, $size);
return $this;
}
public function setMaxPageSize(int $size): self
{
$this->maxPageSize = max(1, $size);
return $this;
}
public function setDefaultPage(int $page): self
{
$this->defaultPage = max(1, $page);
return $this;
}
public function setKeywordFields(array $fields): self
{
$this->keywordFields = $fields;
return $this;
}
public function setEnableKeywordSearch(bool $enable): self
{
$this->enableKeywordSearch = $enable;
return $this;
}
public function setKeywordParam(string $param): self
{
$this->keywordParam = $param;
return $this;
}
public function setEnableRangeQuery(bool $enable): self
{
$this->enableRangeQuery = $enable;
return $this;
}
public function setEnableLikeQuery(bool $enable): self
{
$this->enableLikeQuery = $enable;
return $this;
}
public function setAutoConvertDateTime(bool $auto): self
{
$this->autoConvertDateTime = $auto;
return $this;
}
public function setSkipEmptyValues(bool $skip): self
{
$this->skipEmptyValues = $skip;
return $this;
}
public function setCustomFields(array $fields): self
{
$this->customFields = $fields;
return $this;
}
public function setEnableCache(bool $enable): self
{
$this->enableCache = $enable;
return $this;
}
public function setCacheTtl(int $ttl): self
{
$this->cacheTtl = max(0, $ttl);
return $this;
}
public function setDebugMode(bool $debug): self
{
$this->debugMode = $debug;
return $this;
}
public function setFilterFormat(string $format): self
{
$this->filterFormat = in_array($format, ['array', 'keyvalue']) ? $format : 'array';
return $this;
}
public function setDefaultOperator(string $operator): self
{
$this->defaultOperator = $operator;
return $this;
}
/**
* 添加跳过的字段
*
* @param string|array $fields
* @return self
*/
public function addSkipFields($fields): self
{
$fields = is_array($fields) ? $fields : [$fields];
$this->skipFields = array_merge($this->skipFields, $fields);
return $this;
}
/**
* 添加允许的字段
*
* @param string|array $fields
* @return self
*/
public function addAllowedFields($fields): self
{
$fields = is_array($fields) ? $fields : [$fields];
$this->allowedFields = array_merge($this->allowedFields ?? [], $fields);
return $this;
}
/**
* 添加自定义字段
*
* @param string $key
* @param mixed $value
* @return self
*/
public function addCustomField(string $key, $value): self
{
$this->customFields[$key] = $value;
return $this;
}
/**
* 转换为数组
*
* @return array
*/
public function toArray(): array
{
return [
'return_format' => $this->returnFormat,
'param_format' => $this->paramFormat,
'query_mode' => $this->queryMode,
'skip_fields' => $this->skipFields,
'allowed_fields' => $this->allowedFields,
'sort_fields' => $this->sortFields,
'default_sort' => $this->defaultSort,
'default_page_size' => $this->defaultPageSize,
'max_page_size' => $this->maxPageSize,
'default_page' => $this->defaultPage,
'keyword_fields' => $this->keywordFields,
'enable_keyword_search' => $this->enableKeywordSearch,
'keyword_param' => $this->keywordParam,
'enable_range_query' => $this->enableRangeQuery,
'enable_like_query' => $this->enableLikeQuery,
'auto_convert_datetime' => $this->autoConvertDateTime,
'skip_empty_values' => $this->skipEmptyValues,
'custom_fields' => $this->customFields,
'enable_cache' => $this->enableCache,
'cache_ttl' => $this->cacheTtl,
'debug_mode' => $this->debugMode,
'filter_format' => $this->filterFormat,
'default_operator' => $this->defaultOperator,
];
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。