| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong |
| 4 |
namespace Yoast\WP\SEO\Llms_Txt\Domain\Markdown; |
| 5 |
|
| 6 |
use Yoast\WP\SEO\Llms_Txt\Domain\Markdown\Sections\Section_Interface; |
| 7 |
|
| 8 |
/** |
| 9 |
* The renderer of the LLMs.txt file. |
| 10 |
*/ |
| 11 |
class Llms_Txt_Renderer { |
| 12 |
|
| 13 |
/** |
| 14 |
* The sections. |
| 15 |
* |
| 16 |
* @var Section_Interface[] |
| 17 |
*/ |
| 18 |
private $sections; |
| 19 |
|
| 20 |
/** |
| 21 |
* Adds a section. |
| 22 |
* |
| 23 |
* @param Section_Interface $section The section to add. |
| 24 |
* |
| 25 |
* @return void |
| 26 |
*/ |
| 27 |
public function add_section( Section_Interface $section ): void { |
| 28 |
$this->sections[] = $section; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Returns the sections. |
| 33 |
* |
| 34 |
* @return Section_Interface[] |
| 35 |
*/ |
| 36 |
public function get_sections(): array { |
| 37 |
return $this->sections; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Renders the items of the bucket. |
| 42 |
* |
| 43 |
* @return string |
| 44 |
*/ |
| 45 |
public function render(): string { |
| 46 |
if ( empty( $this->sections ) ) { |
| 47 |
return ''; |
| 48 |
} |
| 49 |
|
| 50 |
$rendered_sections = []; |
| 51 |
foreach ( $this->sections as $section ) { |
| 52 |
$section_content = $section->render(); |
| 53 |
if ( $section_content === '' ) { |
| 54 |
continue; |
| 55 |
} |
| 56 |
|
| 57 |
$rendered_sections[] = $section->get_prefix() . $section_content . \PHP_EOL; |
| 58 |
} |
| 59 |
|
| 60 |
return \implode( \PHP_EOL, $rendered_sections ); |
| 61 |
} |
| 62 |
} |
| 63 |
|