LinkConverter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 41
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isValidAutolink() 0 3 1
A getSupportedTags() 0 3 1
A convert() 0 13 3
1
<?php
2
3
namespace Pilipinews\Common\Converters;
4
5
use League\HTMLToMarkdown\Converter\ConverterInterface;
6
use League\HTMLToMarkdown\ElementInterface;
7
8
/**
9
 * Link Converter
10
 *
11
 * @package Pilipinews
12
 * @author  Rougin Gutib <[email protected]>
13
 */
14
class LinkConverter implements ConverterInterface
15
{
16
    /**
17
     * Converts the specified element into a parsed string.
18
     *
19
     * @param  \League\HTMLToMarkdown\ElementInterface $element
20
     * @return string
21
     */
22 9
    public function convert(ElementInterface $element)
23
    {
24 9
        $text = trim($element->getValue(), "\t\n\r\0\x0B");
25
26 9
        $href = $element->getAttribute('href');
27
28 9
        $markdown = $text . ' (' . $href . ')';
29
30 9
        $autolink = $this->isValidAutolink((string) $href);
31
32 9
        $href === $text && $autolink && $markdown = $href;
33
34 9
        return $markdown;
35
    }
36
37
    /**
38
     * Returns the supported HTML tags.
39
     *
40
     * @return string[]
41
     */
42 15
    public function getSupportedTags()
43
    {
44 15
        return array('a');
45
    }
46
47
    /**
48
     * @param string $href
49
     *
50
     * @return bool
51
     */
52 9
    private function isValidAutolink($href)
53
    {
54 9
        return preg_match('/^[A-Za-z][A-Za-z0-9.+-]{1,31}:[^<>\x00-\x20]*/i', $href) === 1;
55
    }
56
}
57