ItemDto.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. namespace App\Module\GameItems\Dtos;
  3. use App\Module\GameItems\Enums\ITEM_TYPE;
  4. use App\Module\GameItems\Models\Item;
  5. use UCore\Dto\BaseDto;
  6. /**
  7. * 物品数据传输对象
  8. *
  9. * 用于在服务层和控制器层之间传递物品数据,避免直接暴露模型
  10. */
  11. class ItemDto extends BaseDto
  12. {
  13. /**
  14. * 物品ID
  15. *
  16. * @var int
  17. */
  18. public int $id;
  19. /**
  20. * 物品名称
  21. *
  22. * @var string
  23. */
  24. public string $name;
  25. /**
  26. * 物品描述
  27. *
  28. * @var string|null
  29. */
  30. public ?string $description;
  31. /**
  32. * 物品分类ID
  33. *
  34. * @var int
  35. */
  36. public int $categoryId;
  37. /**
  38. * 物品分类名称
  39. *
  40. * @var string|null
  41. */
  42. public ?string $categoryName;
  43. /**
  44. * 物品类型
  45. *
  46. * @var int
  47. */
  48. public int $type;
  49. /**
  50. * 物品类型名称
  51. *
  52. * @var string
  53. */
  54. public string $typeName;
  55. /**
  56. * 是否是单独属性物品
  57. *
  58. * @var bool
  59. */
  60. public bool $isUnique;
  61. /**
  62. * 最大堆叠数量
  63. *
  64. * @var int
  65. */
  66. public int $maxStack;
  67. /**
  68. * 出售价格
  69. *
  70. * @var int
  71. */
  72. public int $sellPrice;
  73. /**
  74. * 是否可交易
  75. *
  76. * @var bool
  77. */
  78. public bool $tradable;
  79. /**
  80. * 是否可分解
  81. *
  82. * @var bool
  83. */
  84. public bool $dismantlable;
  85. /**
  86. * 默认过期时间(秒)
  87. *
  88. * @var int
  89. */
  90. public int $defaultExpireSeconds;
  91. /**
  92. * 展示属性
  93. *
  94. * @var array
  95. */
  96. public array $displayAttributes;
  97. /**
  98. * 数值属性
  99. *
  100. * @var array
  101. */
  102. public array $numericAttributes;
  103. /**
  104. * 全局过期时间
  105. *
  106. * @var string|null
  107. */
  108. public ?string $globalExpireAt;
  109. /**
  110. * 创建时间
  111. *
  112. * @var string
  113. */
  114. public string $createdAt;
  115. /**
  116. * 更新时间
  117. *
  118. * @var string
  119. */
  120. public string $updatedAt;
  121. /**
  122. * 从模型创建DTO
  123. *
  124. * @param Item $item 物品模型
  125. * @return self
  126. */
  127. public static function fromModel(Item $item): self
  128. {
  129. $dto = new self();
  130. $dto->id = $item->id;
  131. $dto->name = $item->name;
  132. $dto->description = $item->description;
  133. $dto->categoryId = $item->category_id;
  134. $dto->categoryName = $item->category->name ?? null;
  135. $dto->type = $item->type->value;
  136. $dto->typeName = ITEM_TYPE::getName($item->type->value);
  137. $dto->isUnique = $item->is_unique;
  138. $dto->maxStack = $item->max_stack;
  139. $dto->sellPrice = $item->sell_price;
  140. $dto->tradable = $item->tradable;
  141. $dto->dismantlable = $item->dismantlable;
  142. $dto->defaultExpireSeconds = $item->default_expire_seconds;
  143. $dto->displayAttributes = (array)$item->display_attributes;
  144. $dto->numericAttributes = (array)$item->numeric_attributes;
  145. $dto->globalExpireAt = $item->global_expire_at ? $item->global_expire_at->format('Y-m-d H:i:s') : null;
  146. $dto->createdAt = $item->created_at->format('Y-m-d H:i:s');
  147. $dto->updatedAt = $item->updated_at->format('Y-m-d H:i:s');
  148. return $dto;
  149. }
  150. /**
  151. * 转换为数组
  152. *
  153. * @return array
  154. */
  155. public function toArray(): array
  156. {
  157. return [
  158. 'id' => $this->id,
  159. 'name' => $this->name,
  160. 'description' => $this->description,
  161. 'category_id' => $this->categoryId,
  162. 'category_name' => $this->categoryName,
  163. 'type' => $this->type,
  164. 'type_name' => $this->typeName,
  165. 'is_unique' => $this->isUnique,
  166. 'max_stack' => $this->maxStack,
  167. 'sell_price' => $this->sellPrice,
  168. 'tradable' => $this->tradable,
  169. 'dismantlable' => $this->dismantlable,
  170. 'default_expire_seconds' => $this->defaultExpireSeconds,
  171. 'display_attributes' => $this->displayAttributes,
  172. 'numeric_attributes' => $this->numericAttributes,
  173. 'global_expire_at' => $this->globalExpireAt,
  174. 'created_at' => $this->createdAt,
  175. 'updated_at' => $this->updatedAt,
  176. ];
  177. }
  178. }