Completed
Push — master ( dd701b...c9307f )
by Billie
29s
created

HtmlStripperImplementation::toText()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2.004

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
ccs 9
cts 10
cp 0.9
rs 9.4285
cc 2
eloc 10
nc 2
nop 1
crap 2.004
1
<?php
2
3
namespace PurpleBooth;
4
5
/**
6
 * Parse HTML to plain text
7
 *
8
 * @package PurpleBooth
9
 */
10
class HtmlStripperImplementation implements HtmlStripper
11
{
12
    /**
13
     * Parse the text.
14
     *
15
     * The actual logic of this function is to setup the XML parsing extension.
16
     *
17
     * @param string $html
18
     *
19
     * @return string
20
     */
21 6
    public function toText($html)
22
    {
23 6
        $parser = new Parser();
24
25 6
        $xmlParser = xml_parser_create();
26 6
        xml_set_element_handler($xmlParser, [$parser, 'startElement'], [$parser, 'endElement']);
27 6
        xml_set_character_data_handler($xmlParser, [$parser, 'characterData']);
28
29 6
        $wrappedHtml  = "<root>$html</root>";
30 6
        $returnStatus = xml_parse($xmlParser, $wrappedHtml, true);
31
32 6
        if (!$returnStatus) {
33
            return $html;
34
        }
35
36 6
        return $parser->getText();
37
    }
38
}
39