PaliTextService.php 2.1 KB

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