SentSimResource.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace App\Http\Resources;
  3. use App\Http\Controllers\CorpusController;
  4. use App\Models\PaliSentence;
  5. use Illuminate\Contracts\Support\Arrayable;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Http\Resources\Json\JsonResource;
  8. use Illuminate\Support\Str;
  9. class SentSimResource extends JsonResource
  10. {
  11. /**
  12. * Transform the resource into an array.
  13. *
  14. * @param Request $request
  15. * @return array|Arrayable|\JsonSerializable
  16. */
  17. public function toArray($request)
  18. {
  19. // 获取实际句子信息
  20. $sent = PaliSentence::find($this->sent2);
  21. if (! $sent) {
  22. return [];
  23. }
  24. // 过滤掉空值或非 uuid 的 channel,避免空 channels 参数导致 500
  25. $channels = [];
  26. foreach (explode(',', (string) $request->input('channels', '')) as $channel) {
  27. if (Str::isUuid($channel)) {
  28. $channels[] = $channel;
  29. }
  30. }
  31. $mode = $request->input('mode', 'read');
  32. $sentId = $sent->book.'-'.$sent->paragraph.'-'.$sent->word_begin.'-'.$sent->word_end;
  33. $Sent = new CorpusController;
  34. $data['sent'] = $Sent->getSentTpl($sentId, $channels, $mode);
  35. $data['sim'] = $this->sim;
  36. return $data;
  37. }
  38. }