| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- <?php
- namespace App\Module\User\Dto;
- use App\Module\User\Enums\PHONE_STATUS;
- use App\Module\User\Enums\Type;
- use App\Module\User\Models\UserPhone;
- /**
- * 用户手机信息数据传输对象
- *
- * 用于在服务层返回用户手机信息,避免直接暴露模型对象
- */
- class UserPhoneDto
- {
- /**
- * @var int ID
- */
- public int $id;
- /**
- * @var int 用户ID
- */
- public int $userId;
- /**
- * @var PHONE_STATUS 状态
- */
- public PHONE_STATUS $status;
- /**
- * @var Type 类型
- */
- public Type $type;
- /**
- * @var string 手机号
- */
- public string $phone;
- /**
- * @var string 创建时间
- */
- public string $createdAt;
- /**
- * @var string 更新时间
- */
- public string $updatedAt;
- /**
- * 从模型创建DTO
- *
- * @param UserPhone $model 用户手机信息模型
- * @return self
- */
- public static function fromModel(UserPhone $model): self
- {
- $dto = new self();
- $dto->id = $model->id;
- $dto->userId = $model->user_id;
- $dto->status = $model->status;
- $dto->type = $model->type;
- $dto->phone = $model->phone;
- $dto->createdAt = $model->created_at ? $model->created_at->toDateTimeString() : '';
- $dto->updatedAt = $model->updated_at ? $model->updated_at->toDateTimeString() : '';
- return $dto;
- }
- /**
- * 转换为数组
- *
- * @return array
- */
- public function toArray(): array
- {
- return [
- 'id' => $this->id,
- 'user_id' => $this->userId,
- 'status' => $this->status->value,
- 'status_name' => $this->status->name,
- 'type' => $this->type->value,
- 'type_name' => $this->type->name,
- 'phone' => $this->phone,
- 'created_at' => $this->createdAt,
- 'updated_at' => $this->updatedAt,
- ];
- }
- }
|