Call.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace App\Services\Markup\Ir;
  3. use App\Services\Markup\Resolve\Resolver;
  4. use App\Services\Markup\Templates\Template;
  5. /**
  6. * 一次模板调用:`{{name|a|b=c}}`、`[[term]]`、`` `note` ``、```note、```mermaid 都落成它。
  7. *
  8. * 参数原样保存;声明为 markdown 的参数(如脚注正文)在解析时就递归解析成 slot。
  9. * 取数阶段({@see Resolver})把查到的数据放进 $data。
  10. */
  11. final class Call implements Node
  12. {
  13. /** @var array<string, mixed> 取数结果,由模板自己约定键名 */
  14. public array $data = [];
  15. /**
  16. * @param string $name 源码里写的名字(别名也原样保留,如 `n`、`ql`)
  17. * @param array<string, Document> $slots markdown 参数解析出的内容
  18. * @param string $raw 源码原文(`{{…}}` 整段),未知模板与超深嵌套时原样输出
  19. */
  20. public function __construct(
  21. public string $name,
  22. public Template $template,
  23. public Params $params,
  24. public array $slots,
  25. public Scope $scope,
  26. public string $raw,
  27. ) {}
  28. /**
  29. * 按「命名参数优先、其次位置参数、最后默认值」取参数,去首尾空白(v2 的 get_param)。
  30. */
  31. public function param(string $name, ?string $default = null): ?string
  32. {
  33. $spec = $this->template->params()[$name] ?? null;
  34. return $this->params->get($name, $spec?->position, $default ?? $spec?->default ?? '');
  35. }
  36. public function slot(string $name): ?Document
  37. {
  38. return $this->slots[$name] ?? null;
  39. }
  40. /**
  41. * 换掉一个命名参数,返回新的调用(逐段对读复制段落时用)。
  42. */
  43. public function withParam(string $name, string $value): self
  44. {
  45. $copy = clone $this;
  46. $copy->params = $this->params->with($name, $value);
  47. return $copy;
  48. }
  49. }