ItemChangeTempDto.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 bool|null
  50. */
  51. public ?bool $oldFrozenStatus;
  52. /**
  53. * 新冻结状态
  54. *
  55. * @var bool|null
  56. */
  57. public ?bool $newFrozenStatus;
  58. /**
  59. * 更新时间戳
  60. *
  61. * @var int
  62. */
  63. public int $updatedAt;
  64. /**
  65. * 从数组创建DTO对象
  66. *
  67. * @param array $data 物品变更数据数组
  68. * @return self
  69. */
  70. public static function fromArray(array $data): self
  71. {
  72. $dto = new self();
  73. $dto->itemId = $data['item_id'];
  74. $dto->instanceId = $data['instance_id'];
  75. $dto->oldQuantity = $data['old_quantity'];
  76. $dto->newQuantity = $data['new_quantity'];
  77. $dto->changeAmount = $data['change_amount'];
  78. $dto->userItemId = $data['user_item_id'];
  79. $dto->oldFrozenStatus = $data['old_frozen_status'] ?? null;
  80. $dto->newFrozenStatus = $data['new_frozen_status'] ?? null;
  81. $dto->updatedAt = $data['updated_at'];
  82. return $dto;
  83. }
  84. /**
  85. * 转换为数组
  86. *
  87. * @return array
  88. */
  89. public function toArray(): array
  90. {
  91. return [
  92. 'item_id' => $this->itemId,
  93. 'instance_id' => $this->instanceId,
  94. 'old_quantity' => $this->oldQuantity,
  95. 'new_quantity' => $this->newQuantity,
  96. 'change_amount' => $this->changeAmount,
  97. 'user_item_id' => $this->userItemId,
  98. 'old_frozen_status' => $this->oldFrozenStatus,
  99. 'new_frozen_status' => $this->newFrozenStatus,
  100. 'updated_at' => $this->updatedAt,
  101. ];
  102. }
  103. }