| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.MaxExceeded |
| 4 |
namespace Yoast\WP\SEO\Llms_Txt\Domain\Markdown\Sections; |
| 5 |
|
| 6 |
use Yoast\WP\SEO\Llms_Txt\Application\Markdown_Escaper; |
| 7 |
use Yoast\WP\SEO\Llms_Txt\Domain\Markdown\Items\Link; |
| 8 |
|
| 9 |
/** |
| 10 |
* Represents a link list markdown section. |
| 11 |
*/ |
| 12 |
class Link_List implements Section_Interface { |
| 13 |
|
| 14 |
/** |
| 15 |
* The type of the links. |
| 16 |
* |
| 17 |
* @var string |
| 18 |
*/ |
| 19 |
private $type; |
| 20 |
|
| 21 |
/** |
| 22 |
* The links. |
| 23 |
* |
| 24 |
* @var Link[] |
| 25 |
*/ |
| 26 |
private $links = []; |
| 27 |
|
| 28 |
/** |
| 29 |
* Class constructor. |
| 30 |
* |
| 31 |
* @param string $type The type of the links. |
| 32 |
* @param Link[] $links The links. |
| 33 |
*/ |
| 34 |
public function __construct( string $type, array $links ) { |
| 35 |
$this->type = $type; |
| 36 |
|
| 37 |
foreach ( $links as $link ) { |
| 38 |
$this->add_link( $link ); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Adds a link to the list. |
| 44 |
* |
| 45 |
* @param Link $link The link to add. |
| 46 |
* |
| 47 |
* @return void |
| 48 |
*/ |
| 49 |
public function add_link( Link $link ): void { |
| 50 |
$this->links[] = $link; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Returns the prefix of the link list section. |
| 55 |
* |
| 56 |
* @return string |
| 57 |
*/ |
| 58 |
public function get_prefix(): string { |
| 59 |
return '## '; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Renders the link item. |
| 64 |
* |
| 65 |
* @return string |
| 66 |
*/ |
| 67 |
public function render(): string { |
| 68 |
if ( empty( $this->links ) ) { |
| 69 |
return ''; |
| 70 |
} |
| 71 |
|
| 72 |
$rendered_links = []; |
| 73 |
foreach ( $this->links as $link ) { |
| 74 |
$rendered_links[] = '- ' . $link->render(); |
| 75 |
} |
| 76 |
|
| 77 |
return $this->type . \PHP_EOL . \implode( \PHP_EOL, $rendered_links ); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Escapes the markdown content. |
| 82 |
* |
| 83 |
* @param Markdown_Escaper $markdown_escaper The markdown escaper. |
| 84 |
* |
| 85 |
* @return void |
| 86 |
*/ |
| 87 |
public function escape_markdown( Markdown_Escaper $markdown_escaper ): void { |
| 88 |
$this->type = $markdown_escaper->escape_markdown_content( $this->type ); |
| 89 |
|
| 90 |
foreach ( $this->links as $link ) { |
| 91 |
$link->escape_markdown( $markdown_escaper ); |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|