RelationResource.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace App\Http\Resources;
  3. use App\Http\Api\ChannelApi;
  4. use App\Http\Api\UserApi;
  5. use App\Models\DhammaTerm;
  6. use Illuminate\Contracts\Support\Arrayable;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Http\Resources\Json\JsonResource;
  9. class RelationResource extends JsonResource
  10. {
  11. /**
  12. * Transform the resource into an array.
  13. *
  14. * The payload must contain only scalars and arrays: it is cached by
  15. * RelationController, and Laravel 13 refuses to unserialize objects from
  16. * cache unless they are listed in cache.serializable_classes. Hence the
  17. * associative json_decode() and the ISO-8601 date strings.
  18. *
  19. * @param Request $request
  20. * @return array|Arrayable|\JsonSerializable
  21. */
  22. public function toArray($request)
  23. {
  24. $data = [
  25. 'id' => $this->id,
  26. 'name' => $this->name,
  27. 'case' => $this->case,
  28. 'from' => json_decode((string) $this->from, true),
  29. 'to' => json_decode((string) $this->to, true),
  30. 'match' => json_decode((string) $this->match, true),
  31. 'category' => $this->category,
  32. 'created_at' => $this->created_at?->toISOString(),
  33. 'updated_at' => $this->updated_at?->toISOString(),
  34. ];
  35. if (! $request->has('vocabulary')) {
  36. $data['editor'] = UserApi::getByUuid($this->editor_id);
  37. $uiLang = strtolower($request->input('ui-lang', 'zh-Hans'));
  38. $term_channel = ChannelApi::getSysChannel("_System_Grammar_Term_{$uiLang}_");
  39. if ($term_channel) {
  40. $data['category_channel'] = $term_channel;
  41. if (! empty($this->category)) {
  42. $term = DhammaTerm::where('word', $this->category)
  43. ->where('channal', $term_channel)
  44. ->first();
  45. if ($term) {
  46. $data['category_term']['channelId'] = $term_channel;
  47. $data['category_term']['word'] = $term->word;
  48. $data['category_term']['id'] = $term->guid;
  49. $data['category_term']['meaning'] = $term->meaning;
  50. }
  51. }
  52. $data['name_channel'] = $term_channel;
  53. $term_name = DhammaTerm::where('word', $this->name)
  54. ->where('channal', $term_channel)
  55. ->first();
  56. if ($term_name) {
  57. $data['name_term']['channelId'] = $term_channel;
  58. $data['name_term']['word'] = $term_name->word;
  59. $data['name_term']['id'] = $term_name->guid;
  60. $data['name_term']['meaning'] = $term_name->meaning;
  61. }
  62. }
  63. }
  64. /*
  65. */
  66. return $data;
  67. }
  68. }