| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- <?php
- namespace App\Module\GameItems\Dtos;
- use App\Module\GameItems\Enums\ITEM_TYPE;
- use App\Module\GameItems\Models\Item;
- use UCore\Dto\BaseDto;
- /**
- * 物品数据传输对象
- *
- * 用于在服务层和控制器层之间传递物品数据,避免直接暴露模型
- */
- class ItemDto extends BaseDto
- {
- /**
- * 物品ID
- *
- * @var int
- */
- public int $id;
- /**
- * 物品名称
- *
- * @var string
- */
- public string $name;
- /**
- * 物品描述
- *
- * @var string|null
- */
- public ?string $description;
- /**
- * 物品分类ID
- *
- * @var int
- */
- public int $categoryId;
- /**
- * 物品分类名称
- *
- * @var string|null
- */
- public ?string $categoryName;
- /**
- * 物品类型
- *
- * @var int
- */
- public int $type;
- /**
- * 物品类型名称
- *
- * @var string
- */
- public string $typeName;
- /**
- * 是否是单独属性物品
- *
- * @var bool
- */
- public bool $isUnique;
- /**
- * 最大堆叠数量
- *
- * @var int
- */
- public int $maxStack;
- /**
- * 出售价格
- *
- * @var int
- */
- public int $sellPrice;
- /**
- * 是否可交易
- *
- * @var bool
- */
- public bool $tradable;
- /**
- * 是否可分解
- *
- * @var bool
- */
- public bool $dismantlable;
- /**
- * 默认过期时间(秒)
- *
- * @var int
- */
- public int $defaultExpireSeconds;
- /**
- * 展示属性
- *
- * @var array
- */
- public array $displayAttributes;
- /**
- * 数值属性
- *
- * @var array
- */
- public array $numericAttributes;
- /**
- * 全局过期时间
- *
- * @var string|null
- */
- public ?string $globalExpireAt;
- /**
- * 创建时间
- *
- * @var string
- */
- public string $createdAt;
- /**
- * 更新时间
- *
- * @var string
- */
- public string $updatedAt;
- /**
- * 从模型创建DTO
- *
- * @param Item $item 物品模型
- * @return self
- */
- public static function fromModel(Item $item): self
- {
- $dto = new self();
- $dto->id = $item->id;
- $dto->name = $item->name;
- $dto->description = $item->description;
- $dto->categoryId = $item->category_id;
- $dto->categoryName = $item->category->name ?? null;
- $dto->type = $item->type->value;
- $dto->typeName = ITEM_TYPE::getName($item->type->value);
- $dto->isUnique = $item->is_unique;
- $dto->maxStack = $item->max_stack;
- $dto->sellPrice = $item->sell_price;
- $dto->tradable = $item->tradable;
- $dto->dismantlable = $item->dismantlable;
- $dto->defaultExpireSeconds = $item->default_expire_seconds;
- $dto->displayAttributes = (array)$item->display_attributes;
- $dto->numericAttributes = (array)$item->numeric_attributes;
- $dto->globalExpireAt = $item->global_expire_at ? $item->global_expire_at->format('Y-m-d H:i:s') : null;
- $dto->createdAt = $item->created_at->format('Y-m-d H:i:s');
- $dto->updatedAt = $item->updated_at->format('Y-m-d H:i:s');
-
- return $dto;
- }
- /**
- * 转换为数组
- *
- * @return array
- */
- public function toArray(): array
- {
- return [
- 'id' => $this->id,
- 'name' => $this->name,
- 'description' => $this->description,
- 'category_id' => $this->categoryId,
- 'category_name' => $this->categoryName,
- 'type' => $this->type,
- 'type_name' => $this->typeName,
- 'is_unique' => $this->isUnique,
- 'max_stack' => $this->maxStack,
- 'sell_price' => $this->sellPrice,
- 'tradable' => $this->tradable,
- 'dismantlable' => $this->dismantlable,
- 'default_expire_seconds' => $this->defaultExpireSeconds,
- 'display_attributes' => $this->displayAttributes,
- 'numeric_attributes' => $this->numericAttributes,
- 'global_expire_at' => $this->globalExpireAt,
- 'created_at' => $this->createdAt,
- 'updated_at' => $this->updatedAt,
- ];
- }
- }
|