SearchPaliWbwResource.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. * page_numbers.type 单字母代号到缩写/名称的映射。
  13. *
  14. * @var array<string, string>
  15. */
  16. private const TYPE_ABBR = [
  17. 'M' => 'My',
  18. 'P' => 'PTS',
  19. 'V' => 'VRI',
  20. 'T' => 'Thai',
  21. 'O' => 'Other',
  22. ];
  23. /**
  24. * Transform the resource into an array.
  25. *
  26. * @param Request $request
  27. * @return array|Arrayable|\JsonSerializable
  28. */
  29. public function toArray($request)
  30. {
  31. $data = [
  32. 'book' => $this->book,
  33. 'paragraph' => $this->paragraph,
  34. ];
  35. if (isset($this->rank)) {
  36. $data['rank'] = $this->rank;
  37. }
  38. $paliText = PaliText::where('book', $this->book)
  39. ->where('paragraph', $this->paragraph)
  40. ->first();
  41. if ($paliText) {
  42. $data['path'] = json_decode($paliText->path, true);
  43. if ($paliText->level < 100) {
  44. $data['paliTitle'] = $paliText->toc;
  45. $book = $this->book;
  46. $para = $this->paragraph;
  47. $data['link'] = config('app.url')."/library/tipitaka/{$book}-{$para}/read";
  48. } else {
  49. $data['paliTitle'] = PaliText::where('book', $this->book)
  50. ->where('paragraph', $paliText->parent)
  51. ->value('toc');
  52. $book = end($data['path'])['book'];
  53. $para = end($data['path'])['paragraph'];
  54. $data['link'] = config('app.url')."/library/tipitaka/{$book}-{$para}/read#{$this->paragraph}";
  55. }
  56. $keyWords = explode(',', $request->input('key'));
  57. $keyWordsUpper = $keyWords;
  58. foreach ($keyWords as $key => $word) {
  59. if (mb_substr($word, -3, null, 'UTF-8') === 'nti') {
  60. $keyWordsUpper[] = mb_substr($word, 0, mb_strlen($word, 'UTF-8') - 3, 'UTF-8');
  61. } elseif (mb_substr($word, -3, null, 'UTF-8') === 'ti') {
  62. $keyWordsUpper[] = mb_substr($word, 0, mb_strlen($word, 'UTF-8') - 2, 'UTF-8');
  63. }
  64. }
  65. foreach ($keyWords as $key => $word) {
  66. $keyWordsUpper[] = mb_strtoupper(mb_substr($word, 0, 1, 'UTF-8'), 'UTF-8').mb_substr($word, 1, null, 'UTF-8');
  67. }
  68. $keyReplace = [];
  69. foreach ($keyWordsUpper as $key => $word) {
  70. $keyReplace[] = "<span class='hl'>{$word}</span>";
  71. }
  72. $data['highlight'] = str_replace($keyWordsUpper, $keyReplace, $paliText->html);
  73. }
  74. $series = app(PaliSeriesesService::class)->find((int) $this->book, (int) $this->paragraph);
  75. $ref = PageNumber::where('book', $this->book)
  76. ->where('paragraph', $this->paragraph)
  77. ->orderBy('wid')
  78. ->get()
  79. ->unique('type')
  80. ->map(fn ($pageNumber) => [
  81. 'type' => self::TYPE_ABBR[$pageNumber->type] ?? $pageNumber->type,
  82. 'page' => $pageNumber->page,
  83. 'title' => match ($pageNumber->type) {
  84. 'M' => $series['abbr_my'] ?? null,
  85. 'P' => $series['abbr_pts'] ?? null,
  86. default => null,
  87. },
  88. ])
  89. ->values()
  90. ->all();
  91. $ref[] = [
  92. 'type' => 'WP',
  93. 'page' => $this->paragraph,
  94. 'title' => $series['abbr_wp'] ?? null,
  95. ];
  96. $data['ref'] = $ref;
  97. return $data;
  98. }
  99. }