TipitakaReadParaController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Services\PaliContentService;
  4. use Illuminate\Http\JsonResponse;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Str;
  7. class TipitakaReadParaController extends Controller
  8. {
  9. /**
  10. * 阅读模式段落内容列表。指定 book 段落区间和 channel
  11. */
  12. public function index(Request $request, PaliContentService $paliService): JsonResponse
  13. {
  14. $data = $request->validate([
  15. 'book' => 'required|integer',
  16. 'para' => 'required|integer',
  17. 'to' => 'integer',
  18. 'channel' => 'required|uuid',
  19. 'format' => 'string|in:html,markdown,react,text',
  20. 'view' => 'string|in:display,sentences,all', // display:整段合并 sentences:逐句
  21. ]);
  22. $from = $data['para'];
  23. $to = $data['to'] ?? $from;
  24. if ($to < $from) {
  25. return $this->error('invalid paragraph range');
  26. }
  27. $format = $data['format'] ?? 'html';
  28. $view = $data['view'] ?? 'display';
  29. $items = [];
  30. foreach (range($from, $to) as $para) {
  31. $paragraph = $paliService->readParagraph(
  32. (int) $data['book'],
  33. (int) $para,
  34. $data['channel'],
  35. $format
  36. );
  37. if (empty($paragraph['display'])) {
  38. continue;
  39. }
  40. $items[] = $this->filterView($paragraph, $view);
  41. }
  42. return $this->ok([
  43. 'items' => $items,
  44. 'pagination' => [
  45. 'page' => 1,
  46. 'pageSize' => $to - $from + 1,
  47. 'total' => count($items),
  48. ],
  49. ]);
  50. }
  51. /**
  52. * 单个段落内容。id 格式 {book}-{para}
  53. */
  54. public function show(Request $request, string $id, PaliContentService $paliService): JsonResponse
  55. {
  56. $arrId = explode('-', $id);
  57. if (count($arrId) !== 2 || ! is_numeric($arrId[0]) || ! is_numeric($arrId[1])) {
  58. return $this->error('invalid id');
  59. }
  60. $channel = $request->input('channel');
  61. if (! Str::isUuid($channel)) {
  62. return $this->error('invalid channel');
  63. }
  64. $book = (int) $arrId[0];
  65. $para = (int) $arrId[1];
  66. $paragraph = $paliService->readParagraph(
  67. $book,
  68. $para,
  69. $channel,
  70. $request->input('format', 'html')
  71. );
  72. if (empty($paragraph['display'])) {
  73. return $this->error('no data');
  74. }
  75. return $this->ok($this->filterView($paragraph, $request->input('view', 'display')));
  76. }
  77. /**
  78. * 按 view 裁剪输出。display 只要段落 html,sentences 只要句子列表,all 两者都要。
  79. *
  80. * @param array{para: int, display: string, sentences: array} $paragraph
  81. */
  82. protected function filterView(array $paragraph, string $view): array
  83. {
  84. return match ($view) {
  85. 'sentences' => ['para' => $paragraph['para'], 'sentences' => $paragraph['sentences']],
  86. 'all' => $paragraph,
  87. default => ['para' => $paragraph['para'], 'display' => $paragraph['display']],
  88. };
  89. }
  90. }