| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- <?php
- namespace App\Module\Game\Dtos;
- use UCore\Dto\BaseDto;
- /**
- * 宠物状态临时数据DTO
- *
- * 用于存储和传输宠物的变更目标信息
- * 继承自BaseDto,只记录变更的目标(用户id+宠物id)
- * 适用于PetCreatedEvent和PetUpdateEvent事件的数据存储
- */
- class PetStatusTempDto extends BaseDto
- {
- /**
- * 宠物唯一ID
- *
- * @var int
- */
- public int $id = 0;
- /**
- * 更新时间戳
- *
- * @var int
- */
- public int $updatedAt = 0;
- /**
- * 从缓存中恢复对象数组
- *
- * 重写父类方法以正确处理嵌套的PetLifeSkillDto对象数组
- *
- * @param mixed $cachedData 缓存的数据
- * @return array DTO对象数组
- */
- public static function fromCache($cachedData): array
- {
- if (is_null($cachedData)) {
- return [];
- }
- if (!is_array($cachedData)) {
- return [];
- }
- $result = [];
- foreach ($cachedData as $key => $item) {
- if ($item instanceof static) {
- // 如果已经是正确的对象类型,检查并修复lifeSkills
- $result[$key] = $item;
- } elseif (is_array($item)) {
- // 从数组创建DTO对象
- $dto = new static();
- // 复制基本属性
- $dto->id = $item['id'] ?? 0;
- $result[$key] = $dto;
- }
- }
- return $result;
- }
- }
|