HouseRankItemDto.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace App\Module\Farm\Dtos;
  3. /**
  4. * 房屋排行榜项目数据传输对象
  5. */
  6. class HouseRankItemDto
  7. {
  8. /**
  9. * 排名
  10. * @var int
  11. */
  12. public int $rank = 0;
  13. /**
  14. * 房屋等级
  15. * @var int
  16. */
  17. public int $level = 0;
  18. /**
  19. * 财富值(钻石余额)
  20. * @var int
  21. */
  22. public int $wealth = 0;
  23. /**
  24. * 用户ID
  25. * @var int
  26. */
  27. public int $userId = 0;
  28. /**
  29. * 用户昵称
  30. * @var string
  31. */
  32. public string $nickname = '';
  33. /**
  34. * 第几届/赛季
  35. * @var int
  36. */
  37. public int $reason = 1;
  38. /**
  39. * 构造函数
  40. *
  41. * @param int $rank 排名
  42. * @param int $level 房屋等级
  43. * @param int $wealth 财富值
  44. * @param int $userId 用户ID
  45. * @param string $nickname 用户昵称
  46. * @param int $reason 赛季
  47. */
  48. public function __construct(int $rank = 0, int $level = 0, int $wealth = 0, int $userId = 0, string $nickname = '', int $reason = 1)
  49. {
  50. $this->rank = $rank;
  51. $this->level = $level;
  52. $this->wealth = $wealth;
  53. $this->userId = $userId;
  54. $this->nickname = $nickname;
  55. $this->reason = $reason;
  56. }
  57. /**
  58. * 从数据库查询结果创建DTO
  59. *
  60. * @param array $data 数据库查询结果
  61. * @param int $rank 排名
  62. * @return static
  63. */
  64. public static function fromArray(array $data, int $rank): static
  65. {
  66. return new static(
  67. rank: $rank,
  68. level: $data['house_level'] ?? 0,
  69. wealth: $data['balance'] ?? 0,
  70. userId: $data['user_id'] ?? 0,
  71. nickname: $data['nickname'] ?? '',
  72. reason: 1
  73. );
  74. }
  75. }