PaliWordSuggestionController.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. namespace App\Http\Controllers\V3;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\V3\IndexPaliWordSuggestionRequest;
  5. use App\Http\Resources\V3\PaliWordSuggestionResource;
  6. use App\Services\V3\PaliWordSuggestionService;
  7. /**
  8. * 巴利词补全(只读、公开)。
  9. *
  10. * 取代 `GET /v2/pali-word-index?view=key`(`WordIndexController@index`)。v2 那份在 dashboard-v4
  11. * 下线前原样保留,不要改动。
  12. */
  13. class PaliWordSuggestionController extends Controller
  14. {
  15. public function __construct(private readonly PaliWordSuggestionService $words) {}
  16. /**
  17. * 巴利词补全
  18. *
  19. * 按已输入的前缀列出三藏里出现过的词形,带出现次数与黑体次数。前缀同时匹配去掉变音符号的写法
  20. * (输入 dhamma 也能补出 dhammā),不区分大小写。与输入完全相同的词排在最前,其余按出现次数降序。
  21. *
  22. * @unauthenticated
  23. *
  24. * @queryParam q string required 已输入的前缀。Example: vitakk
  25. * @queryParam limit integer 最多返回几条,上限 50。Default: 10
  26. *
  27. * @meta total integer 本次返回的条数
  28. */
  29. public function __invoke(IndexPaliWordSuggestionRequest $request)
  30. {
  31. $items = $this->words->suggest($request->validated('q'), $request->integer('limit', 10));
  32. return PaliWordSuggestionResource::collection($items)->additional(['meta' => ['total' => count($items)]]);
  33. }
  34. }