ItemChangeTempDto.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace App\Module\Game\Dtos;
  3. /**
  4. * 物品变更临时数据DTO
  5. *
  6. * 用于存储和传输物品变更的临时数据
  7. */
  8. class ItemChangeTempDto
  9. {
  10. /**
  11. * 物品ID
  12. *
  13. * @var int
  14. */
  15. public int $itemId;
  16. /**
  17. * 物品实例ID(单独属性物品)
  18. *
  19. * @var int|null
  20. */
  21. public ?int $instanceId;
  22. /**
  23. * 旧数量
  24. *
  25. * @var int
  26. */
  27. public int $oldQuantity;
  28. /**
  29. * 新数量
  30. *
  31. * @var int
  32. */
  33. public int $newQuantity;
  34. /**
  35. * 变化量
  36. *
  37. * @var int
  38. */
  39. public int $changeAmount;
  40. /**
  41. * 用户物品记录ID
  42. *
  43. * @var int
  44. */
  45. public int $userItemId;
  46. /**
  47. * 更新时间戳
  48. *
  49. * @var int
  50. */
  51. public int $updatedAt;
  52. /**
  53. * 从数组创建DTO对象
  54. *
  55. * @param array $data 物品变更数据数组
  56. * @return self
  57. */
  58. public static function fromArray(array $data): self
  59. {
  60. $dto = new self();
  61. $dto->itemId = $data['item_id'];
  62. $dto->instanceId = $data['instance_id'];
  63. $dto->oldQuantity = $data['old_quantity'];
  64. $dto->newQuantity = $data['new_quantity'];
  65. $dto->changeAmount = $data['change_amount'];
  66. $dto->userItemId = $data['user_item_id'];
  67. $dto->updatedAt = $data['updated_at'];
  68. return $dto;
  69. }
  70. /**
  71. * 转换为数组
  72. *
  73. * @return array
  74. */
  75. public function toArray(): array
  76. {
  77. return [
  78. 'item_id' => $this->itemId,
  79. 'instance_id' => $this->instanceId,
  80. 'old_quantity' => $this->oldQuantity,
  81. 'new_quantity' => $this->newQuantity,
  82. 'change_amount' => $this->changeAmount,
  83. 'user_item_id' => $this->userItemId,
  84. 'updated_at' => $this->updatedAt,
  85. ];
  86. }
  87. }