GameConfigGroup::class, 'type' => GameConfigType::class, 'is_enabled' => 'boolean', 'is_readonly' => 'boolean', 'sort_order' => 'integer', 'options' => 'array', ]; /** * 获取配置的实际值(根据类型转换) */ public function getTypedValue() { if (!$this->is_enabled) { return $this->type->castValue($this->default_value); } return $this->type->castValue($this->value); } /** * 设置配置值(根据类型转换) */ public function setTypedValue($value): bool { if (!$this->type->validateValue($value)) { return false; } // 对于JSON类型,如果传入的是数组,转换为JSON字符串 if ($this->type === GameConfigType::JSON && is_array($value)) { $value = json_encode($value, JSON_UNESCAPED_UNICODE); } $this->value = $value; return true; } /** * 获取配置的显示值 */ public function getDisplayValue(): string { $value = $this->getTypedValue(); return match($this->type) { GameConfigType::BOOLEAN => $value ? '是' : '否', GameConfigType::JSON => is_array($value) ? json_encode($value, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) : (string)$value, default => (string)$value, }; } /** * 获取配置的默认显示值 */ public function getDefaultDisplayValue(): string { $value = $this->type->castValue($this->default_value); return match($this->type) { GameConfigType::BOOLEAN => $value ? '是' : '否', GameConfigType::JSON => is_array($value) ? json_encode($value, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) : (string)$value, default => (string)$value, }; } /** * 重置为默认值 */ public function resetToDefault(): void { $this->value = $this->default_value; } /** * 检查配置是否有效 */ public function isValid(): bool { return $this->is_enabled && !empty($this->key) && !empty($this->name); } /** * 获取配置的完整描述 */ public function getFullDescription(): string { $desc = $this->description; if ($this->remark) { $desc .= "\n备注:" . $this->remark; } return $desc; } }