SearchResource.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace App\Http\Resources;
  3. use Illuminate\Http\Resources\Json\JsonResource;
  4. use App\Models\PaliText;
  5. use App\Models\Sentence;
  6. use App\Http\Api\ChannelApi;
  7. use Illuminate\Support\Facades\Cache;
  8. class SearchResource extends JsonResource
  9. {
  10. /**
  11. * Transform the resource into an array.
  12. *
  13. * @param \Illuminate\Http\Request $request
  14. * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
  15. */
  16. public function toArray($request)
  17. {
  18. $data = [
  19. "book" => $this->book,
  20. "paragraph" => $this->paragraph,
  21. ];
  22. if (isset($this->rank)) {
  23. $data["rank"] = $this->rank;
  24. }
  25. $paliText = PaliText::where('book', $this->book)
  26. ->where('paragraph', $this->paragraph)
  27. ->first();
  28. if (isset($this->highlight)) {
  29. $data["highlight"] = $this->highlight;
  30. } else if (isset($this->content)) {
  31. $data["content"] = $this->content;
  32. } else {
  33. $channelId = Cache::remember('_System_Pali_VRI_', config('mint.cache.expire'), function () {
  34. return ChannelApi::getSysChannel('_System_Pali_VRI_');
  35. });
  36. $paraContent = Sentence::where('book_id', $this->book)
  37. ->where('paragraph', $this->paragraph)
  38. ->where('channel_uid', $channelId)
  39. ->orderBy('word_start')
  40. ->select('content')
  41. ->get();
  42. $html = '';
  43. foreach ($paraContent as $key => $value) {
  44. $html .= $value->content;
  45. }
  46. $data["content"] = $html;
  47. }
  48. if ($paliText) {
  49. $data['path'] = json_decode($paliText->path);
  50. if ($paliText->level < 100) {
  51. $data["paliTitle"] = $paliText->toc;
  52. } else {
  53. $data["paliTitle"] = PaliText::where('book', $this->book)
  54. ->where('paragraph', $paliText->parent)
  55. ->value('toc');
  56. }
  57. }
  58. return $data;
  59. }
  60. }