QuoteLinkTemplate.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. /**
  27. * 只有给了 book + para 的才算引用了经文段落(按书名册页引用的不知道是哪一段)。
  28. */
  29. public function references(Call $call): array
  30. {
  31. return self::paragraphRef($call->param('book').'-'.$call->param('para'));
  32. }
  33. public function collect(Call $call, Resolver $resolver): void
  34. {
  35. $call->data['quoteLink'] = $resolver->loader(QuoteLinkLoader::class)->request($call);
  36. }
  37. /**
  38. * @return array{props: array<string, mixed>, text: string}
  39. */
  40. private function resolved(Call $call, RenderContext $ctx): array
  41. {
  42. $result = $ctx->loader(QuoteLinkLoader::class)->result((string) ($call->data['quoteLink'] ?? ''));
  43. if ($result === null) {
  44. return ['props' => ['found' => false], 'text' => ''];
  45. }
  46. $props = $result['props'];
  47. $text = '';
  48. if ($result['termKey'] !== null) {
  49. $term = TermTemplate::termProps($ctx, $result['termKey'], (string) $props['bookName']);
  50. $props['term'] = $term;
  51. if (isset($term['id'])) {
  52. $text = (string) $term['meaning'];
  53. } else {
  54. $text = (string) $call->param('bookname');
  55. }
  56. }
  57. return ['props' => $props, 'text' => $text.$result['text']];
  58. }
  59. /**
  60. * @return array<string, mixed>
  61. */
  62. public function props(Call $call, RenderContext $ctx): array
  63. {
  64. return $this->resolved($call, $ctx)['props'];
  65. }
  66. public function text(Call $call, RenderContext $ctx): string
  67. {
  68. return $this->resolved($call, $ctx)['text'];
  69. }
  70. }