Completed
Pull Request — master (#129)
by Colin
02:51
created

LinkConverter::isValidAutolink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace League\HTMLToMarkdown\Converter;
4
5
use League\HTMLToMarkdown\ElementInterface;
6
7
class LinkConverter implements ConverterInterface
8
{
9
    /**
10
     * @param ElementInterface $element
11
     *
12
     * @return string
13
     */
14 3
    public function convert(ElementInterface $element)
15
    {
16 3
        $href = $element->getAttribute('href');
17 3
        $title = $element->getAttribute('title');
18 3
        $text = trim($element->getValue());
19
20 3
        if ($title !== '') {
21 3
            $markdown = '[' . $text . '](' . $href . ' "' . $title . '")';
22 3
        } elseif ($href === $text && $this->isValidAutolink($href)) {
23 3
            $markdown = '<' . $href . '>';
24 3
        } else {
25 3
            $markdown = '[' . $text . '](' . $href . ')';
26
        }
27
28 3
        if (!$href) {
29 3
            $markdown = html_entity_decode($element->getChildrenAsString());
30 3
        }
31
32 3
        return $markdown;
33
    }
34
35
    /**
36
     * @return string[]
37
     */
38 81
    public function getSupportedTags()
39
    {
40 81
        return array('a');
41
    }
42
43
    /**
44
     * @param string $href
45
     *
46
     * @return bool
47
     */
48 3
    private function isValidAutolink($href)
49
    {
50 3
        return preg_match('/^[A-Za-z][A-Za-z0-9.+-]{1,31}:[^<>\x00-\x20]*/i', $href) === 1;
51
    }
52
}
53