ChannelLinksResource.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace App\Http\Resources\V3;
  3. use App\Models\Channel;
  4. use App\Services\V3\ChannelLinkService;
  5. use Illuminate\Http\Request;
  6. /**
  7. * 一个 channel 的关系设置(`channel_links` 表)。载荷来自 {@see ChannelLinkService::get()}。
  8. */
  9. class ChannelLinksResource extends BaseResource
  10. {
  11. /**
  12. * @return array{
  13. * channel_id: string,
  14. * derived_from: array{id: string, name: string, type: string, lang: string, status: int}|null,
  15. * wbw: array{id: string, name: string, type: string, lang: string, status: int}|null,
  16. * fallback: list<array{id: string, name: string, type: string, lang: string, status: int}>,
  17. * term_source: list<array{id: string, name: string, type: string, lang: string, status: int}>,
  18. * can_update: bool
  19. * }
  20. */
  21. public function toArray(Request $request): array
  22. {
  23. return [
  24. 'channel_id' => $this->resource['channel_id'],
  25. 'derived_from' => $this->ref($this->resource['derived_from']),
  26. 'wbw' => $this->ref($this->resource['wbw']),
  27. 'fallback' => array_map(fn (Channel $c) => $this->ref($c), $this->resource['fallback']),
  28. 'term_source' => array_map(fn (Channel $c) => $this->ref($c), $this->resource['term_source']),
  29. 'can_update' => $this->resource['can_update'],
  30. ];
  31. }
  32. /**
  33. * @return array{id: string, name: string, type: string, lang: string, status: int}|null
  34. */
  35. private function ref(?Channel $channel): ?array
  36. {
  37. return $channel === null ? null : [
  38. 'id' => (string) $channel->uid,
  39. 'name' => (string) $channel->name,
  40. 'type' => (string) $channel->type,
  41. 'lang' => (string) $channel->lang,
  42. 'status' => (int) $channel->status,
  43. ];
  44. }
  45. }