TipitakaChapterChannelResource.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace App\Http\Resources\V3;
  3. use App\Services\V3\TipitakaChapterChannelService;
  4. use Illuminate\Http\Request;
  5. /**
  6. * 某一章里的一个 channel 及其完成度。
  7. *
  8. * 底层是 progress_chapters 一行(join channels)。`views` / `studio` / `progress_line`
  9. * 由 {@see TipitakaChapterChannelService} 整页批量取好挂在模型上,这里不查库。
  10. *
  11. * 类型核对自 information_schema:progress_chapters.uid / channel_id 是 uuid,
  12. * progress 是 double,book / para 是 integer;channels.type / lang 是 varchar。
  13. */
  14. class TipitakaChapterChannelResource extends BaseResource
  15. {
  16. /**
  17. * @param Request $request
  18. * @return array{
  19. * id: string,
  20. * book: int,
  21. * para: int,
  22. * progress: float,
  23. * progress_line: array<int, int>|null,
  24. * views: int,
  25. * updated_at: string,
  26. * channel: array{id: string, name: string, type: string, lang: string},
  27. * studio: array{id: string, nickName: string, realName: string, avatar?: string}|null
  28. * }
  29. */
  30. public function toArray($request): array
  31. {
  32. return [
  33. 'id' => (string) $this->uid,
  34. 'book' => (int) $this->book,
  35. 'para' => (int) $this->para,
  36. 'progress' => (float) $this->progress,
  37. 'progress_line' => is_array($this->progress_line) ? array_values($this->progress_line) : null,
  38. 'views' => (int) $this->views,
  39. 'updated_at' => $this->updated_at?->toJSON(),
  40. 'channel' => [
  41. 'id' => (string) $this->channel_id,
  42. 'name' => (string) $this->channel_name,
  43. 'type' => (string) $this->channel_type,
  44. 'lang' => (string) $this->channel_lang,
  45. ],
  46. 'studio' => $this->studio,
  47. ];
  48. }
  49. }