| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.MaxExceeded |
| 4 |
namespace Yoast\WP\SEO\Llms_Txt\Domain\Markdown\Items; |
| 5 |
|
| 6 |
use Yoast\WP\SEO\Llms_Txt\Application\Markdown_Escaper; |
| 7 |
|
| 8 |
/** |
| 9 |
* Represents a link markdown item. |
| 10 |
*/ |
| 11 |
class Link implements Item_Interface { |
| 12 |
|
| 13 |
/** |
| 14 |
* The description that is part of this link. |
| 15 |
* |
| 16 |
* @var string |
| 17 |
*/ |
| 18 |
private $description; |
| 19 |
|
| 20 |
/** |
| 21 |
* The link text. |
| 22 |
* |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
private $text; |
| 26 |
|
| 27 |
/** |
| 28 |
* The anchor text. |
| 29 |
* |
| 30 |
* @var string |
| 31 |
*/ |
| 32 |
private $anchor; |
| 33 |
|
| 34 |
/** |
| 35 |
* Class constructor. |
| 36 |
* |
| 37 |
* @param string $text The link text. |
| 38 |
* @param string $anchor The anchor text. |
| 39 |
* @param string $description The description. |
| 40 |
*/ |
| 41 |
public function __construct( string $text, string $anchor, string $description = '' ) { |
| 42 |
$this->text = $text; |
| 43 |
$this->anchor = $anchor; |
| 44 |
$this->description = $description; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Renders the link item. |
| 49 |
* |
| 50 |
* @return string |
| 51 |
*/ |
| 52 |
public function render(): string { |
| 53 |
$description = ( $this->description !== '' ) ? ": $this->description" : ''; |
| 54 |
return "[$this->text]($this->anchor)$description"; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Escapes the markdown content. |
| 59 |
* |
| 60 |
* @param param Markdown_Escaper $markdown_escaper The markdown escaper. |
| 61 |
* |
| 62 |
* @return void |
| 63 |
*/ |
| 64 |
public function escape_markdown( Markdown_Escaper $markdown_escaper ): void { |
| 65 |
$this->text = $markdown_escaper->escape_markdown_content( $this->text ); |
| 66 |
$this->description = $markdown_escaper->escape_markdown_content( $this->description ); |
| 67 |
$this->anchor = $markdown_escaper->escape_markdown_url( $this->anchor ); |
| 68 |
} |
| 69 |
} |
| 70 |
|