1 | <?php |
||
7 | class LinkConverter implements ConverterInterface |
||
8 | { |
||
9 | /** |
||
10 | * @param ElementInterface $element |
||
11 | * |
||
12 | * @return string |
||
13 | */ |
||
14 | 6 | public function convert(ElementInterface $element) |
|
15 | { |
||
16 | 6 | $href = $element->getAttribute('href'); |
|
17 | 6 | $title = $element->getAttribute('title'); |
|
18 | 6 | $text = trim($element->getValue(), "\t\n\r\0\x0B"); |
|
19 | |||
20 | 6 | if ($title !== '') { |
|
21 | 3 | $markdown = '[' . $text . '](' . $href . ' "' . $title . '")'; |
|
22 | 6 | } elseif ($href === $text && $this->isValidAutolink($href)) { |
|
23 | 3 | $markdown = '<' . $href . '>'; |
|
24 | 6 | } elseif ($href === 'mailto:' . $text && $this->isValidEmail($text)) { |
|
25 | 3 | $markdown = '<' . $text . '>'; |
|
26 | } else { |
||
27 | 6 | if (stristr($href, ' ')) { |
|
28 | $href = '<'.$href.'>'; |
||
29 | } |
||
30 | 6 | $markdown = '[' . $text . '](' . $href . ')'; |
|
31 | } |
||
32 | |||
33 | 6 | if (!$href) { |
|
34 | 3 | $markdown = html_entity_decode($element->getChildrenAsString()); |
|
35 | } |
||
36 | |||
37 | 6 | return $markdown; |
|
38 | } |
||
39 | |||
40 | /** |
||
41 | * @return string[] |
||
42 | */ |
||
43 | 90 | public function getSupportedTags() |
|
47 | |||
48 | /** |
||
49 | * @param string $href |
||
50 | * |
||
51 | * @return bool |
||
52 | */ |
||
53 | 6 | private function isValidAutolink($href) |
|
57 | |||
58 | /** |
||
59 | * @param string $email |
||
60 | * |
||
61 | * @return bool |
||
62 | */ |
||
63 | 3 | private function isValidEmail($email) |
|
68 | } |
||
69 |