SearchPaliWbwResource.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace App\Http\Resources;
  3. use App\Models\PageNumber;
  4. use App\Models\PaliText;
  5. use App\Services\PaliSeriesesService;
  6. use Illuminate\Contracts\Support\Arrayable;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Http\Resources\Json\JsonResource;
  9. class SearchPaliWbwResource 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. $data = [
  20. 'book' => $this->book,
  21. 'paragraph' => $this->paragraph,
  22. ];
  23. if (isset($this->rank)) {
  24. $data['rank'] = $this->rank;
  25. }
  26. $paliText = PaliText::where('book', $this->book)
  27. ->where('paragraph', $this->paragraph)
  28. ->first();
  29. if ($paliText) {
  30. $data['path'] = json_decode($paliText->path, true);
  31. if ($paliText->level < 100) {
  32. $data['paliTitle'] = $paliText->toc;
  33. $book = $this->book;
  34. $para = $this->paragraph;
  35. $data['link'] = config('app.url')."/library/tipitaka/{$book}-{$para}/read";
  36. } else {
  37. $data['paliTitle'] = PaliText::where('book', $this->book)
  38. ->where('paragraph', $paliText->parent)
  39. ->value('toc');
  40. $book = end($data['path'])['book'];
  41. $para = end($data['path'])['paragraph'];
  42. $data['link'] = config('app.url')."/library/tipitaka/{$book}-{$para}/read#{$this->paragraph}";
  43. }
  44. $keyWords = explode(',', $request->input('key'));
  45. $keyWordsUpper = $keyWords;
  46. foreach ($keyWords as $key => $word) {
  47. if (mb_substr($word, -3, null, 'UTF-8') === 'nti') {
  48. $keyWordsUpper[] = mb_substr($word, 0, mb_strlen($word, 'UTF-8') - 3, 'UTF-8');
  49. } elseif (mb_substr($word, -3, null, 'UTF-8') === 'ti') {
  50. $keyWordsUpper[] = mb_substr($word, 0, mb_strlen($word, 'UTF-8') - 2, 'UTF-8');
  51. }
  52. }
  53. foreach ($keyWords as $key => $word) {
  54. $keyWordsUpper[] = mb_strtoupper(mb_substr($word, 0, 1, 'UTF-8'), 'UTF-8').mb_substr($word, 1, null, 'UTF-8');
  55. }
  56. $keyReplace = [];
  57. foreach ($keyWordsUpper as $key => $word) {
  58. $keyReplace[] = "<span class='hl'>{$word}</span>";
  59. }
  60. $data['highlight'] = str_replace($keyWordsUpper, $keyReplace, $paliText->html);
  61. }
  62. $series = app(PaliSeriesesService::class)->find((int) $this->book, (int) $this->paragraph);
  63. $ref = PageNumber::where('book', $this->book)
  64. ->where('paragraph', $this->paragraph)
  65. ->orderBy('wid')
  66. ->get()
  67. ->unique('type')
  68. ->map(fn ($pageNumber) => [
  69. 'type' => $pageNumber->type,
  70. 'page' => $pageNumber->page,
  71. 'title' => match ($pageNumber->type) {
  72. 'M' => $series['abbr_my'] ?? null,
  73. 'P' => $series['abbr_pts'] ?? null,
  74. default => null,
  75. },
  76. ])
  77. ->values()
  78. ->all();
  79. $ref[] = [
  80. 'type' => 'wp',
  81. 'page' => $this->paragraph,
  82. 'title' => $series['abbr_wp'] ?? null,
  83. ];
  84. $data['ref'] = $ref;
  85. return $data;
  86. }
  87. }