userId = $userOInfo->user_id; $dto->avatar = $userOInfo->avatar; $dto->status = $userOInfo->status; $dto->nickName = $userOInfo->nick_name; $dto->phone = $userOInfo->phone ?? null; $dto->merchantId = isset($userOInfo->merchant_id) ? $userOInfo->merchant_id : null; return $dto; } /** * 从 UserInfo 模型创建 DTO * * @param UserInfo $userInfo 用户详细信息模型 * @return self */ public static function fromUserInfo(UserInfo $userInfo): self { $dto = new self(); $dto->userId = $userInfo->user_id; $dto->avatar = $userInfo->avatar; $dto->status = $userInfo->status->value; $dto->nickName = $userInfo->nickname; // 如果模型已加载关联 if ($userInfo->relationLoaded('user_phone') && $userInfo->user_phone) { $dto->phone = $userInfo->user_phone->phone; } // 如果模型已加载关联 if ($userInfo->relationLoaded('merchant') && $userInfo->merchant) { $dto->merchantId = $userInfo->merchant->id; } return $dto; } /** * 创建已注销用户的 DTO * * @param int $userId 用户ID * @return self */ public static function createDeleted(int $userId): self { $dto = new self(); $dto->userId = $userId; $dto->avatar = ''; $dto->status = 0; $dto->nickName = '已注销'; $dto->phone = null; $dto->merchantId = null; return $dto; } /** * 转换为数组 * * @return array */ public function toArray(): array { $result = [ 'user_id' => $this->userId, 'avatar' => $this->avatar, 'status' => $this->status, 'nick_name' => $this->nickName, ]; if ($this->phone !== null) { $result['phone'] = $this->phone; } if ($this->merchantId !== null) { $result['merchant_id'] = $this->merchantId; } return $result; } }