PetStatusTempDto.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace App\Module\Game\Dtos;
  3. use UCore\Dto\BaseDto;
  4. /**
  5. * 宠物状态临时数据DTO
  6. *
  7. * 用于存储和传输宠物的变更目标信息
  8. * 继承自BaseDto,只记录变更的目标(用户id+宠物id)
  9. * 适用于PetCreatedEvent和PetUpdateEvent事件的数据存储
  10. */
  11. class PetStatusTempDto extends BaseDto
  12. {
  13. /**
  14. * 宠物唯一ID
  15. *
  16. * @var int
  17. */
  18. public int $id = 0;
  19. /**
  20. * 更新时间戳
  21. *
  22. * @var int
  23. */
  24. public int $updatedAt = 0;
  25. /**
  26. * 从缓存中恢复对象数组
  27. *
  28. * 重写父类方法以正确处理嵌套的PetLifeSkillDto对象数组
  29. *
  30. * @param mixed $cachedData 缓存的数据
  31. * @return array DTO对象数组
  32. */
  33. public static function fromCache($cachedData): array
  34. {
  35. if (is_null($cachedData)) {
  36. return [];
  37. }
  38. if (!is_array($cachedData)) {
  39. return [];
  40. }
  41. $result = [];
  42. foreach ($cachedData as $key => $item) {
  43. if ($item instanceof static) {
  44. // 如果已经是正确的对象类型,检查并修复lifeSkills
  45. $result[$key] = $item;
  46. } elseif (is_array($item)) {
  47. // 从数组创建DTO对象
  48. $dto = new static();
  49. // 复制基本属性
  50. $dto->id = $item['id'] ?? 0;
  51. $result[$key] = $dto;
  52. }
  53. }
  54. return $result;
  55. }
  56. }