UserOInfoDto.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace App\Module\User\Dto;
  3. use App\Module\User\Models\UserInfo;
  4. use App\Module\User\Unit\UserOInfo;
  5. /**
  6. * 用户订单信息数据传输对象
  7. *
  8. * 用于在服务层返回用户订单信息,避免直接暴露模型对象
  9. */
  10. class UserOInfoDto
  11. {
  12. /**
  13. * @var int 用户ID
  14. */
  15. public int $userId;
  16. /**
  17. * @var string|null 头像
  18. */
  19. public ?string $avatar;
  20. /**
  21. * @var int 状态
  22. */
  23. public int $status;
  24. /**
  25. * @var string|null 昵称
  26. */
  27. public ?string $nickName;
  28. /**
  29. * @var string|null 手机号
  30. */
  31. public ?string $phone;
  32. /**
  33. * @var int|null 商户ID
  34. */
  35. public ?int $merchantId;
  36. /**
  37. * 从 UserOInfo 对象创建 DTO
  38. *
  39. * @param UserOInfo $userOInfo 用户订单信息对象
  40. * @return self
  41. */
  42. public static function fromUserOInfo(UserOInfo $userOInfo): self
  43. {
  44. $dto = new self();
  45. $dto->userId = $userOInfo->user_id;
  46. $dto->avatar = $userOInfo->avatar;
  47. $dto->status = $userOInfo->status;
  48. $dto->nickName = $userOInfo->nick_name;
  49. $dto->phone = $userOInfo->phone ?? null;
  50. $dto->merchantId = isset($userOInfo->merchant_id) ? $userOInfo->merchant_id : null;
  51. return $dto;
  52. }
  53. /**
  54. * 从 UserInfo 模型创建 DTO
  55. *
  56. * @param UserInfo $userInfo 用户详细信息模型
  57. * @return self
  58. */
  59. public static function fromUserInfo(UserInfo $userInfo): self
  60. {
  61. $dto = new self();
  62. $dto->userId = $userInfo->user_id;
  63. $dto->avatar = $userInfo->avatar;
  64. $dto->status = $userInfo->status->value;
  65. $dto->nickName = $userInfo->nickname;
  66. // 如果模型已加载关联
  67. if ($userInfo->relationLoaded('user_phone') && $userInfo->user_phone) {
  68. $dto->phone = $userInfo->user_phone->phone;
  69. }
  70. // 如果模型已加载关联
  71. if ($userInfo->relationLoaded('merchant') && $userInfo->merchant) {
  72. $dto->merchantId = $userInfo->merchant->id;
  73. }
  74. return $dto;
  75. }
  76. /**
  77. * 创建已注销用户的 DTO
  78. *
  79. * @param int $userId 用户ID
  80. * @return self
  81. */
  82. public static function createDeleted(int $userId): self
  83. {
  84. $dto = new self();
  85. $dto->userId = $userId;
  86. $dto->avatar = '';
  87. $dto->status = 0;
  88. $dto->nickName = '已注销';
  89. $dto->phone = null;
  90. $dto->merchantId = null;
  91. return $dto;
  92. }
  93. /**
  94. * 转换为数组
  95. *
  96. * @return array
  97. */
  98. public function toArray(): array
  99. {
  100. $result = [
  101. 'user_id' => $this->userId,
  102. 'avatar' => $this->avatar,
  103. 'status' => $this->status,
  104. 'nick_name' => $this->nickName,
  105. ];
  106. if ($this->phone !== null) {
  107. $result['phone'] = $this->phone;
  108. }
  109. if ($this->merchantId !== null) {
  110. $result['merchant_id'] = $this->merchantId;
  111. }
  112. return $result;
  113. }
  114. }