ParagraphConverter   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Test Coverage

Coverage 97.56%

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 118
ccs 40
cts 41
cp 0.9756
rs 10
c 0
b 0
f 0
wmc 14

6 Methods

Rating   Name   Duplication   Size   Complexity  
A escapeSpecialCharacters() 0 7 1
A getSupportedTags() 0 3 1
A convert() 0 20 3
A escapeFirstCharacters() 0 14 3
A escapeOtherCharactersRegex() 0 17 3
A escapeOtherCharacters() 0 14 3
1
<?php
2
3
namespace Pilipinews\Common\Converters;
4
5
use League\HTMLToMarkdown\Converter\ConverterInterface;
6
use League\HTMLToMarkdown\ElementInterface;
7
8
/**
9
 * Paragraph Converter
10
 *
11
 * @package Pilipinews
12
 * @author  Rougin Gutib <[email protected]>
13
 */
14
class ParagraphConverter 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 15
    public function convert(ElementInterface $element)
23
    {
24 15
        $value = $element->getValue();
25
26 15
        $markdown = '';
27
28 15
        $lines = preg_split('/\r\n|\r|\n/', $value);
29
30 15
        foreach ($lines as $line)
31
        {
32
            /*
33
             * Some special characters need to be escaped based on the position that they appear
34
             * The following function will deal with those special cases.
35
             */
36
37 15
            $markdown .= $this->escapeSpecialCharacters($line);
38 15
            $markdown .= "\n";
39 5
        }
40
41 15
        return trim($markdown) !== '' ? "\n" . rtrim(ltrim($markdown)) . "\n" : '';
42
    }
43
44
    /**
45
     * Returns the supported HTML tags.
46
     *
47
     * @return string[]
48
     */
49 15
    public function getSupportedTags()
50
    {
51 15
        return array('p');
52
    }
53
54
    /**
55
     * @param string $line
56
     *
57
     * @return string
58
     */
59 15
    private function escapeSpecialCharacters($line)
60
    {
61 15
        $line = $this->escapeFirstCharacters($line);
62
63 15
        $line = $this->escapeOtherCharacters($line);
64
65 15
        return $this->escapeOtherCharactersRegex($line);
66
    }
67
68
    /**
69
     * @param string $line
70
     *
71
     * @return string
72
     */
73 15
    private function escapeFirstCharacters($line)
74
    {
75 15
        $escapable = array('>', '- ', '+ ', '--', '~~~', '---', '- - -');
76
77 15
        foreach ($escapable as $i)
78
        {
79 15
            if (strpos(ltrim($line), $i) === 0)
80 5
            {
81
                // Found a character that must be escaped, adding a backslash before
82 7
                return '\\' . ltrim($line);
83
            }
84 5
        }
85
86 15
        return $line;
87
    }
88
89
    /**
90
     * @param string $line
91
     *
92
     * @return string
93
     */
94 15
    private function escapeOtherCharacters($line)
95
    {
96 15
        $escapable = array('<!--');
97
98 15
        foreach ($escapable as $i)
99
        {
100 15
            if (strpos($line, $i) !== false)
101 5
            {
102
                // Found an escapable character, escaping it
103 5
                $line = substr_replace($line, '\\', strpos($line, $i), 0);
104
            }
105 5
        }
106
107 15
        return $line;
108
    }
109
110
    /**
111
     * @param string $line
112
     *
113
     * @return string
114
     */
115 15
    private function escapeOtherCharactersRegex($line)
116
    {
117
        // Match numbers ending on ')' or '.' that are at the beginning of the line.
118 15
        $regExs = array('/^[0-9]+(?=\)|\.)/');
119
120 15
        foreach ($regExs as $i)
121
        {
122 15
            if (preg_match($i, $line, $match))
123 5
            {
124
                // Matched an escapable character, adding a backslash on
125
                // the string before the offending character
126
127 7
                $line = substr_replace($line, '\\', strlen($match[0]), 0);
128 1
            }
129 5
        }
130
131 15
        return $line;
132
    }
133
}
134