PaliSeriesesService.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. /**
  39. * wikipali 缩写 + 段号的引用,如 `dī.ni.1.168`(缩写本身以点结尾)。查不到缩写时为 null。
  40. */
  41. public function citation(int $book, int $paragraph): ?string
  42. {
  43. $abbr = $this->find($book, $paragraph)['abbr_wp'] ?? null;
  44. return $abbr === null ? null : $abbr.$paragraph;
  45. }
  46. }