ChannelResource.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace App\Http\Resources;
  3. use App\Http\Api\StudioApi;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Http\Resources\Json\JsonResource;
  6. class ChannelResource extends JsonResource
  7. {
  8. /**
  9. * Transform the resource into an array.
  10. *
  11. * 类型取自 channels 表的列定义:type / source_id 都是 varchar 而不是数字,
  12. * progress 是 double。带 ? 的键只在特定查询口径下出现。
  13. *
  14. * @param Request $request
  15. * @return array{
  16. * uid: string,
  17. * name: string,
  18. * summary: string|null,
  19. * type: string,
  20. * studio: array{id: string, nickName: string, realName: string, studioName: string}|false,
  21. * lang: string,
  22. * is_system: bool,
  23. * status: int,
  24. * created_at: string,
  25. * updated_at: string,
  26. * source_type: string|null,
  27. * source_id: string|null,
  28. * progress?: float,
  29. * role?: string
  30. * }
  31. */
  32. public function toArray($request)
  33. {
  34. $data = [
  35. 'uid' => $this->uid,
  36. 'name' => $this->name,
  37. 'summary' => $this->summary,
  38. 'type' => $this->type,
  39. 'studio' => StudioApi::getById($this->owner_uid),
  40. 'lang' => $this->lang,
  41. 'is_system' => $this->is_system,
  42. 'status' => $this->status,
  43. 'created_at' => $this->created_at,
  44. 'updated_at' => $this->updated_at,
  45. 'source_type' => $this->source_type,
  46. 'source_id' => $this->source_id,
  47. ];
  48. if (isset($this->source_type)) {
  49. $data['source_type'] = $this->source_type;
  50. }
  51. if (isset($this->source_id)) {
  52. $data['source_id'] = $this->source_id;
  53. }
  54. if (isset($this->progress)) {
  55. $data['progress'] = $this->progress;
  56. }
  57. if (isset($this->role)) {
  58. $data['role'] = $this->role;
  59. }
  60. return $data;
  61. }
  62. }