LandInfoDto.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace App\Module\Farm\Dtos;
  3. use App\Module\Farm\Enums\LAND_STATUS;
  4. use App\Module\Farm\Enums\LAND_TYPE;
  5. use App\Module\Farm\Models\FarmLand;
  6. /**
  7. * 土地信息数据传输对象
  8. */
  9. class LandInfoDto
  10. {
  11. /**
  12. * 土地ID
  13. *
  14. * @var int
  15. */
  16. public $id;
  17. /**
  18. * 用户ID
  19. *
  20. * @var int
  21. */
  22. public $userId;
  23. /**
  24. * 位置
  25. *
  26. * @var int
  27. */
  28. public $position;
  29. /**
  30. * 土地类型
  31. *
  32. * @var int
  33. */
  34. public $landType;
  35. /**
  36. * 土地类型名称
  37. *
  38. * @var string
  39. */
  40. public $landTypeName;
  41. /**
  42. * 土地状态
  43. *
  44. * @var int
  45. */
  46. public $status;
  47. /**
  48. * 土地状态名称
  49. *
  50. * @var string
  51. */
  52. public $statusName;
  53. /**
  54. * 作物信息
  55. *
  56. * @var CropInfoDto|null
  57. */
  58. public $crop;
  59. /**
  60. * 从模型创建DTO
  61. *
  62. * @param FarmLand $land
  63. * @param bool $withCrop 是否包含作物信息
  64. * @return self
  65. */
  66. public static function fromModel(FarmLand $land, bool $withCrop = true): self
  67. {
  68. $dto = new self();
  69. $dto->id = $land->id;
  70. $dto->userId = $land->user_id;
  71. $dto->position = $land->position;
  72. $dto->landType = $land->land_type;
  73. $dto->landTypeName = LAND_TYPE::getName($land->land_type);
  74. $dto->status = $land->status;
  75. $dto->statusName = LAND_STATUS::getName($land->status);
  76. if ($withCrop && $land->crop) {
  77. $dto->crop = CropInfoDto::fromModel($land->crop);
  78. }
  79. return $dto;
  80. }
  81. /**
  82. * 转换为数组
  83. *
  84. * @return array
  85. */
  86. public function toArray(): array
  87. {
  88. $data = [
  89. 'id' => $this->id,
  90. 'user_id' => $this->userId,
  91. 'position' => $this->position,
  92. 'land_type' => $this->landType,
  93. 'land_type_name' => $this->landTypeName,
  94. 'status' => $this->status,
  95. 'status_name' => $this->statusName,
  96. ];
  97. if ($this->crop) {
  98. $data['crop'] = $this->crop->toArray();
  99. }
  100. return $data;
  101. }
  102. }