| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <?php
- namespace App\Module\Farm\Dtos;
- use App\Module\Farm\Enums\LAND_STATUS;
- use App\Module\Farm\Enums\LAND_TYPE;
- use App\Module\Farm\Models\FarmLand;
- /**
- * 土地信息数据传输对象
- */
- class LandInfoDto
- {
- /**
- * 土地ID
- *
- * @var int
- */
- public $id;
- /**
- * 用户ID
- *
- * @var int
- */
- public $userId;
- /**
- * 位置
- *
- * @var int
- */
- public $position;
- /**
- * 土地类型
- *
- * @var int
- */
- public $landType;
- /**
- * 土地类型名称
- *
- * @var string
- */
- public $landTypeName;
- /**
- * 土地状态
- *
- * @var int
- */
- public $status;
- /**
- * 土地状态名称
- *
- * @var string
- */
- public $statusName;
- /**
- * 作物信息
- *
- * @var CropInfoDto|null
- */
- public $crop;
- /**
- * 从模型创建DTO
- *
- * @param FarmLand $land
- * @param bool $withCrop 是否包含作物信息
- * @return self
- */
- public static function fromModel(FarmLand $land, bool $withCrop = true): self
- {
- $dto = new self();
- $dto->id = $land->id;
- $dto->userId = $land->user_id;
- $dto->position = $land->position;
- $dto->landType = $land->land_type;
- $dto->landTypeName = LAND_TYPE::getName($land->land_type);
- $dto->status = $land->status;
- $dto->statusName = LAND_STATUS::getName($land->status);
-
- if ($withCrop && $land->crop) {
- $dto->crop = CropInfoDto::fromModel($land->crop);
- }
-
- return $dto;
- }
- /**
- * 转换为数组
- *
- * @return array
- */
- public function toArray(): array
- {
- $data = [
- 'id' => $this->id,
- 'user_id' => $this->userId,
- 'position' => $this->position,
- 'land_type' => $this->landType,
- 'land_type_name' => $this->landTypeName,
- 'status' => $this->status,
- 'status_name' => $this->statusName,
- ];
-
- if ($this->crop) {
- $data['crop'] = $this->crop->toArray();
- }
-
- return $data;
- }
- }
|