id = $config->id; $dto->key = $config->key; $dto->name = $config->name; $dto->description = $config->description; $dto->group = $config->group; $dto->type = $config->type; $dto->value = $config->value; $dto->defaultValue = $config->default_value; $dto->options = $config->options; $dto->validationRules = $config->validation_rules; $dto->isEnabled = $config->is_enabled; $dto->isReadonly = $config->is_readonly; $dto->sortOrder = $config->sort_order; $dto->remark = $config->remark; $dto->createdAt = $config->created_at->format('Y-m-d H:i:s'); $dto->updatedAt = $config->updated_at->format('Y-m-d H:i:s'); // 计算属性 $dto->groupLabel = $config->group_label; $dto->typeLabel = $config->type_label; $dto->statusLabel = $config->status_label; $dto->displayValue = $config->getDisplayValue(); $dto->typedValue = $config->getTypedValue(); return $dto; } /** * 转换为数组(用于API响应) * * @return array */ public function toArray(): array { return [ 'id' => $this->id, 'key' => $this->key, 'name' => $this->name, 'description' => $this->description, 'group' => $this->group->value, 'group_label' => $this->groupLabel, 'type' => $this->type->value, 'type_label' => $this->typeLabel, 'value' => $this->value, 'default_value' => $this->defaultValue, 'options' => $this->options, 'validation_rules' => $this->validationRules, 'is_enabled' => $this->isEnabled, 'is_readonly' => $this->isReadonly, 'sort_order' => $this->sortOrder, 'remark' => $this->remark, 'status_label' => $this->statusLabel, 'display_value' => $this->displayValue, 'typed_value' => $this->typedValue, 'created_at' => $this->createdAt, 'updated_at' => $this->updatedAt, ]; } /** * 转换为简化数组(用于配置列表) * * @return array */ public function toSimpleArray(): array { return [ 'key' => $this->key, 'name' => $this->name, 'value' => $this->typedValue, 'type' => $this->type->value, 'group' => $this->group->value, 'is_enabled' => $this->isEnabled, ]; } /** * 检查配置是否可编辑 * * @return bool */ public function isEditable(): bool { return !$this->isReadonly; } /** * 检查配置是否有效 * * @return bool */ public function isValid(): bool { return $this->type->validateValue($this->value); } /** * 获取配置的帮助信息 * * @return array */ public function getHelpInfo(): array { return [ 'type_help' => $this->getTypeHelp(), 'validation_help' => $this->getValidationHelp(), 'options_help' => $this->getOptionsHelp(), ]; } /** * 获取类型帮助信息 * * @return string */ private function getTypeHelp(): string { return match ($this->type) { MexConfigType::BOOLEAN => '布尔值:true/false 或 1/0', MexConfigType::INTEGER => '整数:如 123', MexConfigType::DECIMAL => '小数:如 123.45', MexConfigType::STRING => '字符串:任意文本', MexConfigType::JSON => 'JSON格式:如 {"key": "value"}', MexConfigType::ARRAY => '数组格式:如 ["item1", "item2"]', }; } /** * 获取验证帮助信息 * * @return string */ private function getValidationHelp(): string { if (empty($this->validationRules)) { return '无特殊验证规则'; } return $this->validationRules; } /** * 获取选项帮助信息 * * @return string */ private function getOptionsHelp(): string { if (empty($this->options)) { return '无预设选项'; } if (isset($this->options['enum'])) { return '可选值:' . implode(', ', $this->options['enum']); } if (isset($this->options['range'])) { return '取值范围:' . $this->options['range']['min'] . ' - ' . $this->options['range']['max']; } return '参考选项:' . json_encode($this->options, JSON_UNESCAPED_UNICODE); } }