IndexTipitakaDiscussionRequest.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace App\Http\Requests\V3;
  3. use App\Models\Discussion;
  4. use Closure;
  5. use Illuminate\Foundation\Http\FormRequest;
  6. use Illuminate\Validation\Rule;
  7. /**
  8. * `GET /v3/tipitaka-discussions/{channel}` 的入参。
  9. *
  10. * **只有 channel 是必填的,而且它在路径上。** 这里全是可选的范围、过滤与分页参数:
  11. * 不带 book 就是整个 channel;`book` 只在跟 `chapter` / `para` 一起用时才必填
  12. * (与 `tipitaka-reading` 同一口径)。
  13. */
  14. class IndexTipitakaDiscussionRequest extends FormRequest
  15. {
  16. public function authorize(): bool
  17. {
  18. return true;
  19. }
  20. /**
  21. * @return array<string, array<int, mixed>>
  22. */
  23. public function rules(): array
  24. {
  25. return [
  26. 'book' => ['integer', 'min:1', 'required_with:chapter,para,to'],
  27. // 章节起始段落号;服务端按 pali_texts 的 chapter_len 展开成段落区间
  28. 'chapter' => ['integer', 'min:1', 'prohibits:para,to'],
  29. 'para' => ['integer', 'min:1'],
  30. 'to' => ['integer', 'min:1'],
  31. // 逗号分隔多选,如 issue,glossary;不传只列话题(评论线程)
  32. 'type' => ['sometimes', 'string', 'regex:/^[a-z]+(,[a-z]+)*$/', function (string $attribute, mixed $value, Closure $fail) {
  33. if (array_diff(explode(',', (string) $value), Discussion::COMMENT_TYPES) !== []) {
  34. $fail(__('validation.in', ['attribute' => $attribute]));
  35. }
  36. }],
  37. 'status' => ['sometimes', 'string', Rule::in(Discussion::STATUSES)],
  38. // 逗号分隔多选,如 error,warning
  39. 'severity' => ['sometimes', 'string', 'regex:/^[a-z]+(,[a-z]+)*$/'],
  40. 'page' => ['integer', 'min:1'],
  41. 'per_page' => ['integer', 'min:1', 'max:200'],
  42. ];
  43. }
  44. }