2
0

AnnotationToken.php 918 B

1234567891011121314151617181920212223242526272829303132
  1. <?php
  2. namespace App\Services\Markup\Annotation;
  3. use App\Services\Markup\Parsing\Placeholder;
  4. /**
  5. * 批注标记在源码里的占位符:`\u{E002}n\u{E003}`,n 是 {@see AnnotationSet} 里的下标。
  6. *
  7. * 与模板占位符({@see Placeholder},E000 / E001)用不同的字符,互不干扰。
  8. */
  9. final class AnnotationToken
  10. {
  11. public const OPEN = "\u{E002}";
  12. public const CLOSE = "\u{E003}";
  13. public const PATTERN = '/'."\u{E002}".'(\d+)'."\u{E003}".'/u';
  14. public static function make(int $index): string
  15. {
  16. return self::OPEN.$index.self::CLOSE;
  17. }
  18. /**
  19. * 去掉占位符(代码里、属性里等不该出现批注的地方),以及源码里本来就有的这两个字符。
  20. */
  21. public static function strip(string $text): string
  22. {
  23. return str_replace([self::OPEN, self::CLOSE], '', (string) preg_replace(self::PATTERN, '', $text));
  24. }
  25. }