HitItemDTO.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace App\DTO\Search;
  3. class HitItemDTO
  4. {
  5. public function __construct(
  6. public string $id,
  7. public float $score,
  8. public string $content,
  9. public string $title,
  10. public string $path,
  11. public array $category,
  12. ) {}
  13. public static function fromArray(array $data): self
  14. {
  15. $source = $data['_source'];
  16. return new self(
  17. id: $source['id'],
  18. score: $data['_score'],
  19. title: $source['title']['pali'] ?? '',
  20. content: $source['content']['pali'] ?? '',
  21. path: $source['path'] ?? '',
  22. category: $source['category'] ?? [],
  23. );
  24. }
  25. /**
  26. * 提取 para 引用ID(核心逻辑🔥)
  27. */
  28. public function getParaId(): ?string
  29. {
  30. if (preg_match('/pali_para_(\d+)_(\d+)/', $this->id, $matches)) {
  31. return "{$matches[1]}-{$matches[2]}";
  32. }
  33. return null;
  34. }
  35. public function getParaLink(): ?string
  36. {
  37. $id = $this->getParaId();
  38. if (!$id) return null;
  39. return "{{para|id={$id}|title={$id}|style=reference}}";
  40. }
  41. }