UserPhoneDto.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace App\Module\User\Dto;
  3. use App\Module\User\Enums\PHONE_STATUS;
  4. use App\Module\User\Enums\Type;
  5. use App\Module\User\Models\UserPhone;
  6. /**
  7. * 用户手机信息数据传输对象
  8. *
  9. * 用于在服务层返回用户手机信息,避免直接暴露模型对象
  10. */
  11. class UserPhoneDto
  12. {
  13. /**
  14. * @var int ID
  15. */
  16. public int $id;
  17. /**
  18. * @var int 用户ID
  19. */
  20. public int $userId;
  21. /**
  22. * @var PHONE_STATUS 状态
  23. */
  24. public PHONE_STATUS $status;
  25. /**
  26. * @var Type 类型
  27. */
  28. public Type $type;
  29. /**
  30. * @var string 手机号
  31. */
  32. public string $phone;
  33. /**
  34. * @var string 创建时间
  35. */
  36. public string $createdAt;
  37. /**
  38. * @var string 更新时间
  39. */
  40. public string $updatedAt;
  41. /**
  42. * 从模型创建DTO
  43. *
  44. * @param UserPhone $model 用户手机信息模型
  45. * @return self
  46. */
  47. public static function fromModel(UserPhone $model): self
  48. {
  49. $dto = new self();
  50. $dto->id = $model->id;
  51. $dto->userId = $model->user_id;
  52. $dto->status = $model->status;
  53. $dto->type = $model->type;
  54. $dto->phone = $model->phone;
  55. $dto->createdAt = $model->created_at ? $model->created_at->toDateTimeString() : '';
  56. $dto->updatedAt = $model->updated_at ? $model->updated_at->toDateTimeString() : '';
  57. return $dto;
  58. }
  59. /**
  60. * 转换为数组
  61. *
  62. * @return array
  63. */
  64. public function toArray(): array
  65. {
  66. return [
  67. 'id' => $this->id,
  68. 'user_id' => $this->userId,
  69. 'status' => $this->status->value,
  70. 'status_name' => $this->status->name,
  71. 'type' => $this->type->value,
  72. 'type_name' => $this->type->name,
  73. 'phone' => $this->phone,
  74. 'created_at' => $this->createdAt,
  75. 'updated_at' => $this->updatedAt,
  76. ];
  77. }
  78. }