PaliTextService.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. $chapter = $this->getCurrChapter($book,$para);
  17. if($chapter){
  18. return [$book,$chapter->paragraph,$chapter->paragraph+$chapter->chapter_len-1];
  19. }else{
  20. return null;
  21. }
  22. }
  23. public function getCurrChapter(int $book, int $para)
  24. {
  25. $paragraph = PaliText::where('book', $book)
  26. ->where('paragraph', '<=', $para)
  27. ->where('level', '<', 8)
  28. ->orderBy('paragraph', 'desc')->first();
  29. if ($paragraph) {
  30. return $paragraph;
  31. } else {
  32. return null;
  33. }
  34. }
  35. public function getBookPara(int $book, int $para)
  36. {
  37. $paragraph = PaliText::where('book', $book)
  38. ->where('paragraph', '<=', $para)
  39. ->where('level', 1)
  40. ->orderBy('paragraph', 'asc')->first();
  41. if (! $paragraph) {
  42. Log::warning('not found book ', ['book' => $book, 'para' => $para]);
  43. return null;
  44. }
  45. return $paragraph;
  46. }
  47. public function getParaCategoryTags(int $book, int $para): array
  48. {
  49. $bookPara = self::getBookPara($book, $para);
  50. if (! $bookPara) {
  51. return [];
  52. }
  53. if (Str::isUuid($bookPara->uid)) {
  54. return app(TagService::class)->getTagsName($bookPara->uid);
  55. } else {
  56. Log::error('book uid not uuid', [
  57. 'book' => $book,
  58. 'para' => $para,
  59. 'uid' => $bookPara->uid,
  60. ]);
  61. return [];
  62. }
  63. }
  64. public function getParaInfo(int $book, int $para)
  65. {
  66. return PaliText::where('book', $book)
  67. ->where('paragraph', $para)
  68. ->first();
  69. }
  70. public function getParaPathTitle(int $book, int $para)
  71. {
  72. $para = self::getParaInfo($book, $para);
  73. return array_map(function ($item) {
  74. return $item->title;
  75. }, json_decode($para->path));
  76. }
  77. }