WealthRankItemDto.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace App\Module\Farm\Dtos;
  3. /**
  4. * 财富排行榜项目数据传输对象
  5. */
  6. class WealthRankItemDto
  7. {
  8. /**
  9. * 排名
  10. * @var int
  11. */
  12. public int $rank = 0;
  13. /**
  14. * 财富值(钻石余额)
  15. * @var int
  16. */
  17. public int $wealth = 0;
  18. /**
  19. * 房屋等级(用于同财富值时的排序)
  20. * @var int
  21. */
  22. public int $houseLevel = 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 $wealth 财富值
  43. * @param int $houseLevel 房屋等级
  44. * @param int $userId 用户ID
  45. * @param string $nickname 用户昵称
  46. * @param int $reason 赛季
  47. */
  48. public function __construct(int $rank = 0, int $wealth = 0, int $houseLevel = 0, int $userId = 0, string $nickname = '', int $reason = 1)
  49. {
  50. $this->rank = $rank;
  51. $this->wealth = $wealth;
  52. $this->houseLevel = $houseLevel;
  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. // dd($data);
  67. return new static(
  68. rank: $rank,
  69. wealth: $data['balance'] ?? 0,
  70. houseLevel: $data['house_level'] ?? 0,
  71. userId: $data['user_id'] ?? 0,
  72. nickname: $data['nickname'] ?? '',
  73. reason: 1
  74. );
  75. }
  76. }