ChapterTemplate.php 1.7 KB

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