HouseChangeTempDto.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace App\Module\Game\Dtos;
  3. use UCore\Dto\BaseDto;
  4. /**
  5. * 房屋变更临时数据传输对象
  6. *
  7. * 用于存储和传输房屋变更的临时数据
  8. */
  9. class HouseChangeTempDto extends BaseDto
  10. {
  11. /**
  12. * 旧等级
  13. *
  14. * @var int
  15. */
  16. public int $oldLevel;
  17. /**
  18. * 新等级
  19. *
  20. * @var int
  21. */
  22. public int $newLevel;
  23. /**
  24. * 是否为升级(true为升级,false为降级)
  25. *
  26. * @var bool
  27. */
  28. public bool $isUpgrade;
  29. /**
  30. * 产出加成
  31. *
  32. * @var float
  33. */
  34. public float $outputBonus;
  35. /**
  36. * 特殊土地上限
  37. *
  38. * @var int
  39. */
  40. public int $specialLandLimit;
  41. /**
  42. * 更新时间戳
  43. *
  44. * @var int
  45. */
  46. public int $updatedAt;
  47. /**
  48. * 从缓存数据创建DTO对象
  49. *
  50. * @param mixed $cachedData 缓存数据
  51. * @return array 包含DTO对象的数组
  52. */
  53. public static function fromCache($cachedData): array
  54. {
  55. // 为了保持与原有逻辑兼容,我们将创建一个新的方法来处理单个对象的情况
  56. $singleDto = self::fromCacheSingle($cachedData);
  57. if ($singleDto === null) {
  58. return [];
  59. }
  60. return [0 => $singleDto]; // 使用0作为默认键
  61. }
  62. /**
  63. * 从缓存数据创建单个DTO对象
  64. *
  65. * 这是一个辅助方法,保留原有的逻辑,但不覆盖父类的fromCache方法
  66. *
  67. * @param mixed $cachedData 缓存数据
  68. * @return HouseChangeTempDto|null 包含DTO对象或null
  69. */
  70. public static function fromCacheSingle($cachedData): ?HouseChangeTempDto
  71. {
  72. if (empty($cachedData)) {
  73. return null;
  74. }
  75. if ($cachedData instanceof self) {
  76. return $cachedData;
  77. }
  78. if (!is_array($cachedData)) {
  79. return null;
  80. }
  81. $dto = new self();
  82. $dto->oldLevel = $cachedData['old_level'] ?? 0;
  83. $dto->newLevel = $cachedData['new_level'] ?? 0;
  84. $dto->isUpgrade = $cachedData['is_upgrade'] ?? true;
  85. $dto->outputBonus = $cachedData['output_bonus'] ?? 0.0;
  86. $dto->specialLandLimit = $cachedData['special_land_limit'] ?? 0;
  87. $dto->updatedAt = $cachedData['updated_at'] ?? time();
  88. return $dto;
  89. }
  90. /**
  91. * 转换为数组
  92. *
  93. * @return array
  94. */
  95. public function toArray(): array
  96. {
  97. return [
  98. 'old_level' => $this->oldLevel,
  99. 'new_level' => $this->newLevel,
  100. 'is_upgrade' => $this->isUpgrade,
  101. 'output_bonus' => $this->outputBonus,
  102. 'special_land_limit' => $this->specialLandLimit,
  103. 'updated_at' => $this->updatedAt,
  104. ];
  105. }
  106. }