PaliTextService.php 1.9 KB

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