MexConfigDto.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace App\Module\Mex\Dto;
  3. use App\Module\Mex\Enums\MexConfigGroup;
  4. use App\Module\Mex\Enums\MexConfigType;
  5. use App\Module\Mex\Models\MexConfig;
  6. use UCore\Dto\BaseDto;
  7. /**
  8. * Mex配置DTO
  9. */
  10. class MexConfigDto extends BaseDto
  11. {
  12. public int $id;
  13. public string $key;
  14. public string $name;
  15. public string $description;
  16. public MexConfigGroup $group;
  17. public MexConfigType $type;
  18. public string $value;
  19. public ?string $defaultValue;
  20. public ?array $options;
  21. public ?string $validationRules;
  22. public bool $isEnabled;
  23. public bool $isReadonly;
  24. public int $sortOrder;
  25. public ?string $remark;
  26. public string $createdAt;
  27. public string $updatedAt;
  28. // 计算属性
  29. public string $groupLabel;
  30. public string $typeLabel;
  31. public string $statusLabel;
  32. public string $displayValue;
  33. public mixed $typedValue;
  34. /**
  35. * 从模型创建DTO
  36. *
  37. * @param MexConfig $config
  38. * @return static
  39. */
  40. public static function fromModel(MexConfig $config): static
  41. {
  42. $dto = new static();
  43. $dto->id = $config->id;
  44. $dto->key = $config->key;
  45. $dto->name = $config->name;
  46. $dto->description = $config->description;
  47. $dto->group = $config->group;
  48. $dto->type = $config->type;
  49. $dto->value = $config->value;
  50. $dto->defaultValue = $config->default_value;
  51. $dto->options = $config->options;
  52. $dto->validationRules = $config->validation_rules;
  53. $dto->isEnabled = $config->is_enabled;
  54. $dto->isReadonly = $config->is_readonly;
  55. $dto->sortOrder = $config->sort_order;
  56. $dto->remark = $config->remark;
  57. $dto->createdAt = $config->created_at->format('Y-m-d H:i:s');
  58. $dto->updatedAt = $config->updated_at->format('Y-m-d H:i:s');
  59. // 计算属性
  60. $dto->groupLabel = $config->group_label;
  61. $dto->typeLabel = $config->type_label;
  62. $dto->statusLabel = $config->status_label;
  63. $dto->displayValue = $config->getDisplayValue();
  64. $dto->typedValue = $config->getTypedValue();
  65. return $dto;
  66. }
  67. /**
  68. * 转换为数组(用于API响应)
  69. *
  70. * @return array
  71. */
  72. public function toArray(): array
  73. {
  74. return [
  75. 'id' => $this->id,
  76. 'key' => $this->key,
  77. 'name' => $this->name,
  78. 'description' => $this->description,
  79. 'group' => $this->group->value,
  80. 'group_label' => $this->groupLabel,
  81. 'type' => $this->type->value,
  82. 'type_label' => $this->typeLabel,
  83. 'value' => $this->value,
  84. 'default_value' => $this->defaultValue,
  85. 'options' => $this->options,
  86. 'validation_rules' => $this->validationRules,
  87. 'is_enabled' => $this->isEnabled,
  88. 'is_readonly' => $this->isReadonly,
  89. 'sort_order' => $this->sortOrder,
  90. 'remark' => $this->remark,
  91. 'status_label' => $this->statusLabel,
  92. 'display_value' => $this->displayValue,
  93. 'typed_value' => $this->typedValue,
  94. 'created_at' => $this->createdAt,
  95. 'updated_at' => $this->updatedAt,
  96. ];
  97. }
  98. /**
  99. * 转换为简化数组(用于配置列表)
  100. *
  101. * @return array
  102. */
  103. public function toSimpleArray(): array
  104. {
  105. return [
  106. 'key' => $this->key,
  107. 'name' => $this->name,
  108. 'value' => $this->typedValue,
  109. 'type' => $this->type->value,
  110. 'group' => $this->group->value,
  111. 'is_enabled' => $this->isEnabled,
  112. ];
  113. }
  114. /**
  115. * 检查配置是否可编辑
  116. *
  117. * @return bool
  118. */
  119. public function isEditable(): bool
  120. {
  121. return !$this->isReadonly;
  122. }
  123. /**
  124. * 检查配置是否有效
  125. *
  126. * @return bool
  127. */
  128. public function isValid(): bool
  129. {
  130. return $this->type->validateValue($this->value);
  131. }
  132. /**
  133. * 获取配置的帮助信息
  134. *
  135. * @return array
  136. */
  137. public function getHelpInfo(): array
  138. {
  139. return [
  140. 'type_help' => $this->getTypeHelp(),
  141. 'validation_help' => $this->getValidationHelp(),
  142. 'options_help' => $this->getOptionsHelp(),
  143. ];
  144. }
  145. /**
  146. * 获取类型帮助信息
  147. *
  148. * @return string
  149. */
  150. private function getTypeHelp(): string
  151. {
  152. return match ($this->type) {
  153. MexConfigType::BOOLEAN => '布尔值:true/false 或 1/0',
  154. MexConfigType::INTEGER => '整数:如 123',
  155. MexConfigType::DECIMAL => '小数:如 123.45',
  156. MexConfigType::STRING => '字符串:任意文本',
  157. MexConfigType::JSON => 'JSON格式:如 {"key": "value"}',
  158. MexConfigType::ARRAY => '数组格式:如 ["item1", "item2"]',
  159. };
  160. }
  161. /**
  162. * 获取验证帮助信息
  163. *
  164. * @return string
  165. */
  166. private function getValidationHelp(): string
  167. {
  168. if (empty($this->validationRules)) {
  169. return '无特殊验证规则';
  170. }
  171. return $this->validationRules;
  172. }
  173. /**
  174. * 获取选项帮助信息
  175. *
  176. * @return string
  177. */
  178. private function getOptionsHelp(): string
  179. {
  180. if (empty($this->options)) {
  181. return '无预设选项';
  182. }
  183. if (isset($this->options['enum'])) {
  184. return '可选值:' . implode(', ', $this->options['enum']);
  185. }
  186. if (isset($this->options['range'])) {
  187. return '取值范围:' . $this->options['range']['min'] . ' - ' . $this->options['range']['max'];
  188. }
  189. return '参考选项:' . json_encode($this->options, JSON_UNESCAPED_UNICODE);
  190. }
  191. }