AggregationDTO.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. namespace App\DTO\Search;
  3. /**
  4. * 通用聚合结果DTO
  5. * 用于处理所有结构相同的聚合桶数据
  6. */
  7. class AggregationDTO
  8. {
  9. public function __construct(
  10. public int $doc_count_error_upper_bound,
  11. public int $sum_other_doc_count,
  12. /** @var BucketDTO[] */
  13. public array $buckets,
  14. ) {}
  15. public static function fromArray(array $data): self
  16. {
  17. return new self(
  18. doc_count_error_upper_bound: $data['doc_count_error_upper_bound'],
  19. sum_other_doc_count: $data['sum_other_doc_count'],
  20. buckets: array_map(fn($bucket) => BucketDTO::fromArray($bucket), $data['buckets']),
  21. );
  22. }
  23. public function toArray(): array
  24. {
  25. return [
  26. 'doc_count_error_upper_bound' => $this->doc_count_error_upper_bound,
  27. 'sum_other_doc_count' => $this->sum_other_doc_count,
  28. 'buckets' => array_map(
  29. fn(BucketDTO $bucket) => $bucket->toArray(),
  30. $this->buckets
  31. ),
  32. ];
  33. }
  34. }