| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- <?php
- namespace App\Module\Mex\Dto;
- use App\Module\Mex\Enums\MexConfigGroup;
- use App\Module\Mex\Enums\MexConfigType;
- use App\Module\Mex\Models\MexConfig;
- use UCore\Dto\BaseDto;
- /**
- * Mex配置DTO
- */
- class MexConfigDto extends BaseDto
- {
- public int $id;
- public string $key;
- public string $name;
- public string $description;
- public MexConfigGroup $group;
- public MexConfigType $type;
- public string $value;
- public ?string $defaultValue;
- public ?array $options;
- public ?string $validationRules;
- public bool $isEnabled;
- public bool $isReadonly;
- public int $sortOrder;
- public ?string $remark;
- public string $createdAt;
- public string $updatedAt;
- // 计算属性
- public string $groupLabel;
- public string $typeLabel;
- public string $statusLabel;
- public string $displayValue;
- public mixed $typedValue;
- /**
- * 从模型创建DTO
- *
- * @param MexConfig $config
- * @return static
- */
- public static function fromModel(MexConfig $config): static
- {
- $dto = new static();
-
- $dto->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);
- }
- }
|