PaliSeriesesService.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace App\Services;
  3. class PaliSeriesesService
  4. {
  5. /**
  6. * 书缩写表:book => [起始段落 => ['abbr_my' => ..., 'abbr_pts' => ..., 'abbr_wp' => ...]]
  7. *
  8. * @var array<int, array<int, array{abbr_my: string, abbr_pts: string, abbr_wp: string}>>
  9. */
  10. private array $serieses;
  11. public function __construct()
  12. {
  13. $this->serieses = require resource_path('data/pali_serieses.php');
  14. }
  15. /**
  16. * 根据书号与段落号查找该段所属文本的书缩写。
  17. *
  18. * 缩写表以“段落起始号”为键,同一书内多个文本按起始段落递增排列;
  19. * 这里取不大于目标段落的最大起始号对应条目。
  20. *
  21. * @return array{abbr_my: string, abbr_pts: string, abbr_wp: string}|null
  22. */
  23. public function find(int $book, int $paragraph): ?array
  24. {
  25. $entries = $this->serieses[$book] ?? null;
  26. if (! is_array($entries)) {
  27. return null;
  28. }
  29. $result = null;
  30. foreach ($entries as $start => $abbr) {
  31. if ($start > $paragraph) {
  32. break;
  33. }
  34. $result = $abbr;
  35. }
  36. return $result;
  37. }
  38. }