QuoteLinkTemplate.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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\QuoteLinkLoader;
  6. use App\Services\Markup\Resolve\Resolver;
  7. /**
  8. * 引用某版本的册页:`{{ql|type|bookname|volume|page|style|title|book|para}}`,取数口径见 {@see QuoteLinkLoader}。
  9. */
  10. final class QuoteLinkTemplate extends Template
  11. {
  12. protected string $component = 'quote-link';
  13. public function params(): array
  14. {
  15. return [
  16. 'type' => ParamSpec::at(1),
  17. 'bookname' => ParamSpec::at(2),
  18. 'volume' => ParamSpec::at(3),
  19. 'page' => ParamSpec::at(4),
  20. 'style' => ParamSpec::at(5, 'modal'),
  21. 'title' => ParamSpec::at(6),
  22. 'book' => ParamSpec::at(7),
  23. 'para' => ParamSpec::at(8),
  24. ];
  25. }
  26. public function collect(Call $call, Resolver $resolver): void
  27. {
  28. $call->data['quoteLink'] = $resolver->loader(QuoteLinkLoader::class)->request($call);
  29. }
  30. /**
  31. * @return array{props: array<string, mixed>, text: string}
  32. */
  33. private function resolved(Call $call, RenderContext $ctx): array
  34. {
  35. $result = $ctx->loader(QuoteLinkLoader::class)->result((string) ($call->data['quoteLink'] ?? ''));
  36. if ($result === null) {
  37. return ['props' => ['found' => false], 'text' => ''];
  38. }
  39. $props = $result['props'];
  40. $text = '';
  41. if ($result['termKey'] !== null) {
  42. $term = TermTemplate::termProps($ctx, $result['termKey'], (string) $props['bookName']);
  43. $props['term'] = $term;
  44. if (isset($term['id'])) {
  45. $text = (string) $term['meaning'];
  46. } else {
  47. $text = (string) $call->param('bookname');
  48. }
  49. }
  50. return ['props' => $props, 'text' => $text.$result['text']];
  51. }
  52. /**
  53. * @return array<string, mixed>
  54. */
  55. public function props(Call $call, RenderContext $ctx): array
  56. {
  57. return $this->resolved($call, $ctx)['props'];
  58. }
  59. public function text(Call $call, RenderContext $ctx): string
  60. {
  61. return $this->resolved($call, $ctx)['text'];
  62. }
  63. }