| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- <?php
- namespace App\Http\Resources;
- use App\Http\Api\ChannelApi;
- use App\Models\PaliText;
- use App\Models\Sentence;
- use Illuminate\Contracts\Support\Arrayable;
- use Illuminate\Http\Request;
- use Illuminate\Http\Resources\Json\JsonResource;
- use Illuminate\Support\Facades\Cache;
- class SearchResource extends JsonResource
- {
- /**
- * Transform the resource into an array.
- *
- * @param Request $request
- * @return array|Arrayable|\JsonSerializable
- */
- public function toArray($request)
- {
- $data = [
- 'book' => $this->book,
- 'paragraph' => $this->paragraph,
- ];
- if (isset($this->rank)) {
- $data['rank'] = $this->rank;
- }
- $paliText = PaliText::where('book', $this->book)
- ->where('paragraph', $this->paragraph)
- ->first();
- if (isset($this->highlight)) {
- $data['highlight'] = $this->highlight;
- } elseif (isset($this->content)) {
- $data['content'] = $this->content;
- } else {
- $channelId = Cache::remember('_System_Pali_VRI_', config('mint.cache.expire'), function () {
- return ChannelApi::getSysChannel('_System_Pali_VRI_');
- });
- $paraContent = Sentence::where('book_id', $this->book)
- ->where('paragraph', $this->paragraph)
- ->where('channel_uid', $channelId)
- ->orderBy('word_start')
- ->select('content')
- ->get();
- $html = '';
- foreach ($paraContent as $key => $value) {
- $html .= $value->content;
- }
- $data['content'] = $html;
- }
- if ($paliText) {
- $data['path'] = json_decode($paliText->path);
- if ($paliText->level < 100) {
- $data['paliTitle'] = $paliText->toc;
- } else {
- $data['paliTitle'] = PaliText::where('book', $this->book)
- ->where('paragraph', $paliText->parent)
- ->value('toc');
- }
- }
- return $data;
- }
- }
|