PaliTextService.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace App\Services;
  3. use App\Models\PaliText;
  4. use Illuminate\Support\Facades\Log;
  5. use Illuminate\Support\Str;
  6. class PaliTextService
  7. {
  8. public function getParent(int $book, int $para)
  9. {
  10. $parent = PaliText::where('book', $book)
  11. ->where('paragraph', $para)->value('parent');
  12. return $parent ? PaliText::where('book', $book)
  13. ->where('paragraph', $parent)->first() : null;
  14. }
  15. public function chapterRange(int $book, int $para)
  16. {
  17. $chapter = $this->getCurrChapter($book, $para);
  18. if ($chapter) {
  19. return [$book, $chapter->paragraph, $chapter->paragraph + $chapter->chapter_len - 1];
  20. } else {
  21. return null;
  22. }
  23. }
  24. public function getCurrChapter(int $book, int $para)
  25. {
  26. $paragraph = PaliText::where('book', $book)
  27. ->where('paragraph', '<=', $para)
  28. ->where('level', '<', 8)
  29. ->orderBy('paragraph', 'desc')->first();
  30. if ($paragraph) {
  31. return $paragraph;
  32. } else {
  33. return null;
  34. }
  35. }
  36. public function getBookPara(int $book, int $para)
  37. {
  38. $paragraph = PaliText::where('book', $book)
  39. ->where('paragraph', '<=', $para)
  40. ->where('level', 1)
  41. ->orderBy('paragraph', 'asc')->first();
  42. if (! $paragraph) {
  43. Log::warning('not found book ', ['book' => $book, 'para' => $para]);
  44. return null;
  45. }
  46. return $paragraph;
  47. }
  48. public function getParaCategoryTags(int $book, int $para): array
  49. {
  50. $bookPara = self::getBookPara($book, $para);
  51. if (! $bookPara) {
  52. return [];
  53. }
  54. if (Str::isUuid($bookPara->uid)) {
  55. return app(TagService::class)->getTagsName($bookPara->uid);
  56. } else {
  57. Log::error('book uid not uuid', [
  58. 'book' => $book,
  59. 'para' => $para,
  60. 'uid' => $bookPara->uid,
  61. ]);
  62. return [];
  63. }
  64. }
  65. public function getParaInfo(int $book, int $para)
  66. {
  67. return PaliText::where('book', $book)
  68. ->where('paragraph', $para)
  69. ->first();
  70. }
  71. public function getParaPathTitle(int $book, int $para)
  72. {
  73. $para = self::getParaInfo($book, $para);
  74. return array_map(function ($item) {
  75. return $item->title;
  76. }, json_decode($para->path));
  77. }
  78. }