Scope.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace App\Services\Markup\Ir;
  3. use App\Services\Markup\Dialect;
  4. /**
  5. * 一段内容的上下文:属于哪些 channel、用哪种方言、在第几层内容里。
  6. *
  7. * 句子模板取回的译文、术语的 note、Tpl 模板的正文都是「另一段内容」,各自带新的 Scope(depth + 1)。
  8. * 模板参数里的 markdown(如脚注正文)是同一段内容的一部分,沿用外层 Scope。
  9. */
  10. final readonly class Scope
  11. {
  12. /** 内容嵌套的最大层数:文章 → 句子译文 → 术语 note。再往下按纯文本输出(v2 是两层) */
  13. public const MAX_DEPTH = 3;
  14. /**
  15. * @param list<string> $channels
  16. * @param bool $inTermNote 这段内容是某个术语的 note:里面的术语不再取它们自己的 note(只展开一层,v2 口径)
  17. */
  18. public function __construct(
  19. public array $channels = [],
  20. public Dialect $dialect = Dialect::Markdown,
  21. public int $depth = 1,
  22. public string $channelType = 'translation',
  23. public string $contentType = 'markdown',
  24. public bool $inTermNote = false,
  25. ) {}
  26. public function firstChannel(): ?string
  27. {
  28. return $this->channels[0] ?? null;
  29. }
  30. /**
  31. * 下一层内容的 Scope。
  32. *
  33. * @param list<string>|null $channels null 表示沿用当前 channel
  34. */
  35. public function nested(?array $channels = null, ?string $contentType = null, string $channelType = 'translation'): self
  36. {
  37. $contentType ??= 'markdown';
  38. return new self(
  39. channels: $channels ?? $this->channels,
  40. dialect: Dialect::fromContentType($contentType),
  41. depth: $this->depth + 1,
  42. channelType: $channelType,
  43. contentType: $contentType,
  44. );
  45. }
  46. public function canNest(): bool
  47. {
  48. return $this->depth < self::MAX_DEPTH;
  49. }
  50. }