PaliTextService.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace App\Services;
  3. use App\Models\PaliText;
  4. use Illuminate\Support\Facades\Log;
  5. class PaliTextService
  6. {
  7. public function getParent(int $book, int $para) {}
  8. public function getCurrChapter(int $book, int $para)
  9. {
  10. $paragraph = PaliText::where('book', $book)
  11. ->where('paragraph', '<=', $para)
  12. ->where('level', '<', 8)
  13. ->orderBy('paragraph', 'desc')->first();
  14. if ($paragraph) {
  15. return $paragraph;
  16. } else {
  17. return null;
  18. }
  19. }
  20. public function getBookPara(int $book, int $para)
  21. {
  22. $paragraph = PaliText::where('book', $book)
  23. ->where('paragraph', '<=', $para)
  24. ->where('level', 1)
  25. ->orderBy('paragraph', 'asc')->first();
  26. if ($paragraph) {
  27. return $paragraph;
  28. } else {
  29. Log::error('not found book ', ['book' => $book, 'para' => $para]);
  30. return null;
  31. }
  32. }
  33. public function getParaCategoryTags(int $book, int $para): array
  34. {
  35. $bookPara = self::getBookPara($book, $para);
  36. if (!$bookPara) {
  37. return [];
  38. }
  39. if (isset($bookPara->uid) && $bookPara->uid) {
  40. return app(TagService::class)->getTagsName($bookPara->uid);
  41. } else {
  42. Log::error('book uid is null', ['book' => $book, 'para' => $para]);
  43. return [];
  44. }
  45. }
  46. public function getParaInfo(int $book, int $para)
  47. {
  48. return PaliText::where('book', $book)
  49. ->where('paragraph', $para)
  50. ->first();
  51. }
  52. public function getParaPathTitle(int $book, int $para)
  53. {
  54. $para = self::getParaInfo($book, $para);
  55. return array_map(function ($item) {
  56. return $item->title;
  57. }, json_decode($para->path));
  58. }
  59. }