Files
tcpserver-flow/app/flow/config/MorningWashConfig.php
2026-03-08 22:58:56 +08:00

89 lines
2.7 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace app\flow\config;
/**
* 晨洗配置类
*/
class MorningWashConfig extends AbstractConfig
{
public MorningMode $mode {
get => $this->mode;
set => $this->mode = $value;
}
public int $storageThreshold {
get => $this->storageThreshold;
set => $this->storageThreshold = $value;
}
public string $morningStartTime {
get => $this->morningStartTime;
set => $this->morningStartTime = $value;
}
/**
* 扩展字段:存储除已知字段(mode / storage_threshold / morning_start_time)之外的原始字段
* 以及业务侧自定义扩展字段(如 specific_types 等)
*/
public array $expand {
get => $this->expand;
}
public function __construct(
MorningMode $mode = MorningMode::DailyFirst,
int $storageThreshold = 4,
string $morningStartTime = '00:00:00',
array $expand = []
) {
$this->mode = $mode;
$this->storageThreshold = $storageThreshold;
$this->morningStartTime = $morningStartTime;
$this->expand = $expand;
}
/**
* 从数组创建
* 已知字段直接映射到具体属性,其余字段(含原始字段)存入 expand
*/
public static function create(array $config = []): self
{
// mode 支持字符串(兼容旧配置文件)
$mode = $config['mode'] ?? MorningMode::DailyFirst;
if (is_string($mode)) {
$mode = MorningMode::from_name($mode) ?? MorningMode::DailyFirst;
}
$known = ['mode', 'storage_threshold', 'morning_start_time'];
$expand = array_diff_key($config, array_flip($known));
// 把原始 config 整体也保留在 expand['_raw'] 里,便于下游无损访问
$expand['_raw'] = $config;
$instance = new self(
$mode,
$config['storage_threshold'] ?? 4,
$config['morning_start_time'] ?? '00:00:00',
$expand
);
$instance->log("加载[morning_wash]晨洗配置,来源: {}", [empty($config) ? "默认配置" : "自定义配置"]);
return $instance;
}
/**
* 获取扩展字段值(如 specific_types
*/
public function getExpand(string $key, mixed $default = null): mixed
{
return $this->expand[$key] ?? $default;
}
public function toArray(): array
{
return array_merge([
'mode' => $this->mode->name,
'storage_threshold' => $this->storageThreshold,
'morning_start_time' => $this->morningStartTime,
], array_diff_key($this->expand, ['_raw' => null]));
}
}