*/ private array $blocks = []; public function h1(string $text): self { $this->blocks[] = ['type' => 'h1', 'text' => $text]; return $this; } public function h2(string $text): self { $this->blocks[] = ['type' => 'h2', 'text' => $text]; return $this; } public function h3(string $text): self { $this->blocks[] = ['type' => 'h3', 'text' => $text]; return $this; } public function paragraph(string $text): self { $text = trim($text); if ($text === '') { return $this; } $this->blocks[] = ['type' => 'p', 'text' => $text]; return $this; } public function bullet(string $text): self { $this->blocks[] = ['type' => 'bullet', 'text' => $text]; return $this; } public function link(string $label, string $url): self { $this->blocks[] = ['type' => 'link', 'text' => $label, 'url' => $url]; return $this; } public function spacer(): self { $this->blocks[] = ['type' => 'spacer']; return $this; } /** * @return list */ public function blocks(): array { return $this->blocks; } public function toPlainText(): string { $lines = []; foreach ($this->blocks as $block) { $type = $block['type'] ?? ''; $text = trim((string) ($block['text'] ?? '')); match ($type) { 'h1' => $lines[] = $text, 'h2' => $lines[] = $text, 'h3' => $lines[] = $text, 'p' => $lines[] = $text, 'bullet' => $lines[] = '· '.$text, 'link' => $lines[] = $text !== '' ? $text.':'.($block['url'] ?? '') : (string) ($block['url'] ?? ''), 'spacer' => $lines[] = '', default => null, }; } return trim(preg_replace("/\n{3,}/", "\n\n", implode("\n", $lines)) ?? ''); } }