ChapterTemplate.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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\Templates\Concerns\CitesParagraph;
  6. use App\Services\Markup\Templates\Concerns\EmbedsArticle;
  7. /**
  8. * 嵌入经文章节:`{{chapter|id=book-para|title|channel|style}}`,没给 id 时用 `book=` + `para=`。
  9. *
  10. * 与 `{{article|type=chapter|…}}` 等价(前端是同一个 `article` 组件),
  11. * 后者拼 id 也走 {@see self::resolveId()}。`style=reference` 且没给标题时显示缩写 + 段号({@see CitesParagraph})。
  12. */
  13. final class ChapterTemplate extends Template
  14. {
  15. use CitesParagraph;
  16. use EmbedsArticle;
  17. protected string $component = 'article';
  18. public function params(): array
  19. {
  20. return [
  21. 'id' => ParamSpec::at(1),
  22. 'title' => ParamSpec::at(2),
  23. 'channel' => ParamSpec::at(3),
  24. 'style' => ParamSpec::at(4),
  25. 'book' => new ParamSpec,
  26. 'para' => new ParamSpec,
  27. ];
  28. }
  29. /**
  30. * 章节 id(`book-para`):给了 id 就用 id,否则由书号 + 段号拼出;书号也没给时为空。
  31. */
  32. public static function resolveId(string $id, string $book, string $para): string
  33. {
  34. if ($id !== '' || $book === '') {
  35. return $id;
  36. }
  37. return ((int) $book).'-'.((int) $para);
  38. }
  39. /**
  40. * @return array<string, mixed>
  41. */
  42. public function props(Call $call, RenderContext $ctx): array
  43. {
  44. return $this->embedProps($call, 'chapter', $this->id($call), ['style', 'channel', 'title']);
  45. }
  46. public function references(Call $call): array
  47. {
  48. return self::paragraphRef($this->id($call));
  49. }
  50. protected function title(Call $call): string
  51. {
  52. return $this->citedTitle($call, $this->id($call));
  53. }
  54. private function id(Call $call): string
  55. {
  56. return self::resolveId((string) $call->param('id'), (string) $call->param('book'), (string) $call->param('para'));
  57. }
  58. }