Completed
Pull Request — master (#90)
by
unknown
06:09
created

TextNode::isTextNode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace PHPHtmlParser\Dom;
3
4
/**
5
 * Class TextNode
6
 *
7
 * @package PHPHtmlParser\Dom
8
 */
9
class TextNode extends LeafNode
10
{
11
12
    /**
13
     * This is a text node.
14
     *
15
     * @var Tag
16
     */
17
    protected $tag;
18
19
    /**
20
     * This is the text in this node.
21
     *
22
     * @var string
23
     */
24
    protected $text;
25
26
    /**
27
     * This is the converted version of the text.
28
     *
29
     * @var string
30
     */
31
    protected $convertedText = null;
32
33
    /**
34
     * Sets the text for this node.
35
     *
36
     * @param string $text
37
     */
38
    public function __construct($text)
39
    {
40
        // remove double spaces
41
        $text = mb_ereg_replace('\s+', ' ', $text);
42
43
        // restore line breaks
44
        $text = str_replace('&#10;', "\n", $text);
45
46
        $this->text = $text;
47
        $this->tag  = new Tag('text');
48
        parent::__construct();
49
    }
50
51
    /**
52
     * Returns the text of this node.
53
     *
54
     * @return string
55
     */
56
    public function text()
57
    {
58
        // convert charset
59
        if ( ! is_null($this->encode)) {
60
            if ( ! is_null($this->convertedText)) {
61
                // we already know the converted value
62
                return $this->convertedText;
63
            }
64
            $text = $this->encode->convert($this->text);
65
66
            // remember the conversion
67
            $this->convertedText = $text;
68
69
            return $text;
70
        } else {
71
            return $this->text;
72
        }
73
    }
74
75
    /**
76
     * This node has no html, just return the text.
77
     *
78
     * @return string
79
     * @uses $this->text()
80
     */
81
    public function innerHtml()
82
    {
83
        return $this->text();
84
    }
85
86
    /**
87
     * This node has no html, just return the text.
88
     *
89
     * @return string
90
     * @uses $this->text()
91
     */
92
    public function outerHtml()
93
    {
94
        return $this->text();
95
    }
96
97
    /**
98
     * Call this when something in the node tree has changed. Like a child has been added
99
     * or a parent has been changed.
100
     */
101
    protected function clear()
102
    {
103
        $this->convertedText = null;
104
    }
105
106
    public function isTextNode()
107
    {
108
        return true;
109
    }
110
111
    public function setText($text)
112
    {
113
        $this->text = $text;
114
    }
115
116
}
117