Completed
Push — master ( 15134a...d6e1fc )
by Kevin
02:45
created

CData::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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