DisasterInfoDto.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace App\Module\Farm\Dtos;
  3. use App\Module\Farm\Enums\DISASTER_TYPE;
  4. /**
  5. * 灾害信息数据传输对象
  6. */
  7. class DisasterInfoDto
  8. {
  9. /**
  10. * 灾害类型
  11. *
  12. * @var int
  13. */
  14. public $type;
  15. /**
  16. * 灾害类型名称
  17. *
  18. * @var string
  19. */
  20. public $typeName;
  21. /**
  22. * 生成时间
  23. *
  24. * @var string
  25. */
  26. public $generatedTs;
  27. /**
  28. * 状态
  29. *
  30. * @var string
  31. */
  32. public $status;
  33. /**
  34. * 清理时间
  35. *
  36. * @var string|null
  37. */
  38. public $clearedAt;
  39. /**
  40. * 从数组创建DTO
  41. *
  42. * @param array $disasterInfo
  43. * @return self
  44. */
  45. public static function fromArray(array $disasterInfo): self
  46. {
  47. $dto = new self();
  48. $dto->type = $disasterInfo['type'] ?? 0;
  49. $dto->typeName = DISASTER_TYPE::getName($dto->type);
  50. $dto->generatedAt = $disasterInfo['generated_ts'] ?? '';
  51. $dto->status = $disasterInfo['status'] ?? 'active';
  52. $dto->clearedAt = $disasterInfo['cleared_at'] ?? null;
  53. return $dto;
  54. }
  55. /**
  56. * 转换为数组
  57. *
  58. * @return array
  59. */
  60. public function toArray(): array
  61. {
  62. return [
  63. 'type' => $this->type,
  64. 'type_name' => $this->typeName,
  65. 'generated_ts' => $this->generatedAt,
  66. 'status' => $this->status,
  67. 'cleared_at' => $this->clearedAt,
  68. ];
  69. }
  70. }