IndexChannelRequest.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace App\Http\Requests\V3;
  3. use App\Services\V3\ChannelService;
  4. use Illuminate\Foundation\Http\FormRequest;
  5. use Illuminate\Validation\Rule;
  6. /**
  7. * `GET /v3/channels` 的入参:公开目录。`system=true` 只要系统 channel;带 book + para 时每条多返回该章完成度。
  8. */
  9. class IndexChannelRequest extends FormRequest
  10. {
  11. /** channels.type 的取值 */
  12. public const TYPES = ['translation', 'nissaya', 'original', 'wbw', 'commentary', 'similar'];
  13. /**
  14. * 登录与权限在中间件 / Service 里判:authorize() 返回 false 会渲染成 403,
  15. * 而「没登录」应该是 401、无权要带具体文案。
  16. */
  17. public function authorize(): bool
  18. {
  19. return true;
  20. }
  21. /**
  22. * @return array<string, array<int, mixed>>
  23. */
  24. public function rules(): array
  25. {
  26. return [
  27. 'system' => ['sometimes', 'in:true,false,1,0'],
  28. 'search' => ['sometimes', 'nullable', 'string', 'max:128'],
  29. 'type' => ['sometimes', 'string', Rule::in(self::TYPES)],
  30. 'lang' => ['sometimes', 'string', 'max:16'],
  31. // 不能用 sometimes:缺 book 时 sometimes 会跳过整条规则,required_with 就不生效
  32. 'book' => ['required_with:para', 'integer', 'min:1'],
  33. 'para' => ['required_with:book', 'integer', 'min:1'],
  34. 'order' => ['sometimes', 'string', Rule::in(ChannelService::ORDERS)],
  35. 'dir' => ['sometimes', 'in:asc,desc'],
  36. 'page' => ['integer', 'min:1'],
  37. 'per_page' => ['integer', 'min:1', 'max:200'],
  38. ];
  39. }
  40. }