Completed
Push — master ( 68f73c...f03bb1 )
by Kevin
03:04
created

CData::getValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 4
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Kevintweber\HtmlTokenizer\Tokens;
4
5
use Kevintweber\HtmlTokenizer\HtmlTokenizer;
6
use Kevintweber\HtmlTokenizer\Exceptions\ParseException;
7
8
class CData extends AbstractToken
9
{
10
    public function __construct(Token $parent = null, bool $throwOnError = true)
11
    {
12
        parent::__construct(Token::CDATA, $parent, $throwOnError);
13
    }
14
15
    public function parse(string $html) : string
16
    {
17
        $html = ltrim($html);
18
19
        // Get token position.
20
        $positionArray = HtmlTokenizer::getPosition($html);
21
        $this->line = $positionArray['line'];
22
        $this->position = $positionArray['position'];
23
24
        // Parse token.
25
        $posOfEndOfCData = mb_strpos($html, ']]>');
26
        if ($posOfEndOfCData === false) {
27
            if ($this->getThrowOnError()) {
28
                throw new ParseException('Invalid CDATA.');
29
            }
30
31
            return '';
32
        }
33
34
        $this->value = trim(mb_substr($html, 9, $posOfEndOfCData - 9));
35
36
        return mb_substr($html, $posOfEndOfCData + 3);
37
    }
38
39
    public function toArray() : array
40
    {
41
        return array(
42
            'type' => 'cdata',
43
            'value' => $this->value,
44
            'line' => $this->getLine(),
45
            'position' => $this->getPosition()
46
        );
47
    }
48
}
49