HtmlStripperImplementation::toText()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2.004

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 17
ccs 9
cts 10
cp 0.9
crap 2.004
rs 9.7
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * Copyright (C) 2016 Billie Thompson
5
 *
6
 * This software may be modified and distributed under the terms
7
 * of the MIT license.  See the LICENSE file for details.
8
 */
9
10
namespace PurpleBooth;
11
12
/**
13
 * Parse HTML to plain text.
14
 */
15
class HtmlStripperImplementation implements HtmlStripper
16
{
17
    /**
18
     * Parse the text.
19
     *
20
     * The actual logic of this function is to setup the XML parsing extension.
21
     *
22
     * @param string $html
23
     *
24
     * @return string
25
     */
26 6
    public function toText($html)
27
    {
28 6
        $parser = new Parser();
29
30 6
        $xmlParser = xml_parser_create();
31 6
        xml_set_element_handler($xmlParser, [$parser, 'startElement'], [$parser, 'endElement']);
32 6
        xml_set_character_data_handler($xmlParser, [$parser, 'characterData']);
33
34 6
        $wrappedHtml = "<root>$html</root>";
35 6
        $returnStatus = xml_parse($xmlParser, $wrappedHtml, true);
36
37 6
        if (!$returnStatus) {
38
            return $html;
39
        }
40
41 6
        return $parser->getText();
42
    }
43
}
44