ConsumeGroupDto.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace App\Module\Game\Dtos;
  3. /**
  4. * 消耗组DTO
  5. */
  6. class ConsumeGroupDto
  7. {
  8. /**
  9. * 消耗组ID
  10. *
  11. * @var int
  12. */
  13. public int $id;
  14. /**
  15. * 消耗组名称
  16. *
  17. * @var string
  18. */
  19. public string $name;
  20. /**
  21. * 消耗组编码
  22. *
  23. * @var string
  24. */
  25. public string $code;
  26. /**
  27. * 消耗组描述
  28. *
  29. * @var string|null
  30. */
  31. public ?string $description;
  32. /**
  33. * 消耗项列表
  34. *
  35. * @var ConsumeItemDto[]
  36. */
  37. public array $items = [];
  38. /**
  39. * 从模型创建DTO
  40. *
  41. * @param \App\Module\Game\Models\GameConsumeGroup $model
  42. * @param bool $withItems 是否包含消耗项
  43. * @return self
  44. */
  45. public static function fromModel($model, bool $withItems = false): self
  46. {
  47. $dto = new self();
  48. $dto->id = $model->id;
  49. $dto->name = $model->name;
  50. $dto->code = $model->code;
  51. $dto->description = $model->description;
  52. if ($withItems && $model->relationLoaded('consumeItems')) {
  53. foreach ($model->consumeItems as $item) {
  54. $dto->items[] = ConsumeItemDto::fromModel($item);
  55. }
  56. }
  57. return $dto;
  58. }
  59. }