GlossaryEntryResource.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. namespace App\Http\Resources\V3;
  3. use App\Models\DhammaTerm;
  4. use Illuminate\Http\Request;
  5. /**
  6. * 词汇表的一条(`dhamma_terms` 表里系统 channel 的术语)。只给客户端本地查词要用的字段。
  7. *
  8. * `id`(术语 guid)只在语法术语表(grammar)里给:v6 悬停 / 语法手册凭它取 `/v3/terms/{id}`。
  9. * 社区译名表(community)没有任何客户端用 id,而上万个 uuid 是整张表里最大、最压不动的一块,所以不给。
  10. *
  11. * @mixin DhammaTerm
  12. */
  13. class GlossaryEntryResource extends BaseResource
  14. {
  15. /**
  16. * @return array{
  17. * id?: string,
  18. * word: string,
  19. * tag: string|null,
  20. * meaning: string,
  21. * other_meaning: string|null
  22. * }
  23. */
  24. public function toArray(Request $request): array
  25. {
  26. $tag = trim((string) $this->tag);
  27. $other = trim((string) $this->other_meaning);
  28. return [
  29. 'id' => $this->when($request->route('kind') === 'grammar', fn () => (string) $this->guid),
  30. 'word' => (string) $this->word,
  31. 'tag' => $tag === '' ? null : $tag,
  32. 'meaning' => (string) $this->meaning,
  33. 'other_meaning' => $other === '' ? null : $other,
  34. ];
  35. }
  36. }