Completed
Push — master ( fd987e...4f1c16 )
by Kevin
02:24
created

AbstractToken::isElement()   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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
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
abstract class AbstractToken implements Token
6
{
7
    /** @var int */
8
    private $depth;
9
10
    /** @var null|Token */
11
    private $parent;
12
13
    /** @var boolean */
14
    private $throwOnError;
15
16
    /** @var string */
17
    private $type;
18
19
    /**
20
     * Constructor
21
     */
22 98
    public function __construct($type, Token $parent = null, $throwOnError = false)
23
    {
24 98
        if (!$this->isValidType($type)) {
25 1
            throw new \InvalidArgumentException('Invalid type: ' . $type);
26
        }
27
28 97
        $this->depth = 0;
29 97
        if ($parent instanceof Token) {
30 33
            $this->depth = $parent->getDepth() + 1;
31 33
        }
32
33 97
        $this->parent = $parent;
34 97
        $this->throwOnError = (boolean) $throwOnError;
35 97
        $this->type = $type;
36 97
    }
37
38 33
    public function getDepth()
39
    {
40 33
        return $this->depth;
41
    }
42
43 12
    public function isClosingElementImplied($html)
44
    {
45 12
        return false;
46
    }
47
48 21
    public function getParent()
49
    {
50 21
        return $this->parent;
51
    }
52
53
    /**
54
     * Getter for 'throwOnError'.
55
     *
56
     * @return boolean
57
     */
58 28
    protected function getThrowOnError()
59
    {
60 28
        return $this->throwOnError;
61
    }
62
63
    /**
64
     * Getter for 'type'.
65
     *
66
     * @return string
67
     */
68 1
    public function getType()
69
    {
70 1
        return $this->type;
71
    }
72
73 1
    public function isCDATA()
74
    {
75 1
        return $this->type === Token::CDATA;
76
    }
77
78 1
    public function isComment()
79
    {
80 1
        return $this->type === Token::COMMENT;
81
    }
82
83 1
    public function isDocType()
84
    {
85 1
        return $this->type === Token::DOCTYPE;
86
    }
87
88 1
    public function isElement()
89
    {
90 1
        return $this->type === Token::ELEMENT;
91
    }
92
93 1
    public function isPhp()
94
    {
95 1
        return $this->type === Token::PHP;
96
    }
97
98 1
    public function isText()
99
    {
100 1
        return $this->type === Token::TEXT;
101
    }
102
103 98
    protected function isValidType($type)
104
    {
105
        return $type === Token::CDATA
106 98
            || $type === Token::COMMENT
107 91
            || $type === Token::DOCTYPE
108 81
            || $type === Token::ELEMENT
109 73
            || $type === Token::PHP
110 98
            || $type === Token::TEXT;
111
    }
112
}
113