SearchResource.php 2.1 KB

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