QaTemplate.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace App\Services\Markup\Templates;
  3. use App\Services\Markup\Ir\Call;
  4. use App\Services\Markup\Render\RenderContext;
  5. use App\Services\Markup\Resolve\QaLoader;
  6. use App\Services\Markup\Resolve\Resolver;
  7. /**
  8. * 嵌入问答话题:`{{qa|discussion id|style}}`。
  9. */
  10. final class QaTemplate extends Template
  11. {
  12. protected string $component = 'qa';
  13. public function params(): array
  14. {
  15. return ['id' => ParamSpec::at(1), 'style' => ParamSpec::at(2)];
  16. }
  17. public function collect(Call $call, Resolver $resolver): void
  18. {
  19. $resolver->loader(QaLoader::class)->request((string) $call->param('id'));
  20. }
  21. /**
  22. * @return array<string, mixed>
  23. */
  24. public function props(Call $call, RenderContext $ctx): array
  25. {
  26. $id = (string) $call->param('id');
  27. $props = ['id' => $id, 'title' => ''];
  28. $qa = $ctx->loader(QaLoader::class)->result($id);
  29. if ($qa !== null) {
  30. $props['title'] = (string) $qa->title;
  31. $props['resType'] = $qa->res_type;
  32. }
  33. return $props;
  34. }
  35. public function text(Call $call, RenderContext $ctx): string
  36. {
  37. return (string) $this->props($call, $ctx)['title'];
  38. }
  39. }