Completed
Push — master ( 7aa624...fe99b2 )
by Kevin
02:25
created

Php::getValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 4
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
7
class Php extends AbstractToken
8
{
9
    /** @var string */
10
    private $value;
11
12 9
    public function __construct(Token $parent = null, $throwOnError = false)
13
    {
14 9
        parent::__construct(Token::PHP, $parent, $throwOnError);
15
16 9
        $this->value = null;
17 9
    }
18
19 7
    public function parse($html)
20
    {
21 7
        $html = ltrim($html);
22
23
        // Get token position.
24 7
        $positionArray = HtmlTokenizer::getPosition($html);
25 7
        $this->setLine($positionArray['line']);
26 7
        $this->setPosition($positionArray['position']);
27
28
        // Parse token.
29 7
        $startPos = 3;
30 7
        if (mb_substr($html, 0, 5) == '<?php') {
31 7
            $startPos = 6;
32 7
        }
33
34 7
        $posOfEndOfPhp = mb_strpos($html, '?>');
35 7
        if ($posOfEndOfPhp === false) {
36 1
            $this->value = trim(mb_substr($html, $startPos));
37
38 1
            return '';
39
        }
40
41 6
        $this->value = trim(mb_substr($html, $startPos, $posOfEndOfPhp - $startPos - 1));
42
43 6
        return mb_substr($html, $posOfEndOfPhp + 2);
44
    }
45
46
    /**
47
     * Getter for 'value'.
48
     *
49
     * @return string
50
     */
51 4
    public function getValue()
52
    {
53 4
        return $this->value;
54
    }
55
56 2
    public function toArray()
57
    {
58
        return array(
59 2
            'type' => 'php',
60 2
            'value' => $this->value,
61 2
            'line' => $this->getLine(),
62 2
            'position' => $this->getPosition()
63 2
        );
64
    }
65
}
66