Token::getIdentifier()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Thruster\Component\Tokenizer;
4
5
use JsonSerializable;
6
7
/**
8
 * Class Token
9
 *
10
 * @package Thruster\Component\Tokenizer
11
 * @author  Aurimas Niekis <[email protected]>
12
 */
13
class Token implements TokenInterface, JsonSerializable
14
{
15
    /**
16
     * @var int
17
     */
18
    private $identifier;
19
20
    /**
21
     * @var string
22
     */
23
    private $content;
24
25
    /**
26
     * @var int
27
     */
28
    private $line;
29
30
    public function __construct(int $identifier, string $content, int $line)
31
    {
32
        $this->identifier = $identifier;
33
        $this->content    = $content;
34
        $this->line       = $line;
35
    }
36
37
    /**
38
     * @return int
39
     */
40
    public function getIdentifier(): int
41
    {
42
        return $this->identifier;
43
    }
44
45
    /**
46
     * @param int $identifier
47
     *
48
     * @return Token
49
     */
50
    public function setIdentifier(int $identifier): Token
51
    {
52
        $this->identifier = $identifier;
53
54
        return $this;
55
    }
56
57
    /**
58
     * @return string
59
     */
60
    public function getContent(): string
61
    {
62
        return $this->content;
63
    }
64
65
    /**
66
     * @param string $content
67
     *
68
     * @return Token
69
     */
70
    public function setContent(string $content): Token
71
    {
72
        $this->content = $content;
73
74
        return $this;
75
    }
76
77
    /**
78
     * @return int
79
     */
80
    public function getLine(): int
81
    {
82
        return $this->line;
83
    }
84
85
    /**
86
     * @param int $line
87
     *
88
     * @return Token
89
     */
90
    public function setLine(int $line): Token
91
    {
92
        $this->line = $line;
93
94
        return $this;
95
    }
96
97
    /**
98
     * @return string
99
     */
100
    public function getName(): string
101
    {
102
        return $this::name($this->identifier);
103
    }
104
105
    public static function name(int $identifier): string
106
    {
107
        if (isset(static::TOKEN_MAP[$identifier])) {
108
            return static::TOKEN_MAP[$identifier];
109
        }
110
111
        return token_name($identifier);
112
    }
113
114
    public static function fromArray(array $data)
115
    {
116
        return (function (int $identifier, string $content, int $line) {
117
            $tokenClass = static::TOKEN_TO_CLASS_MAP[$identifier];
118
119
            return new $tokenClass($content, $line);
120
        })(...$data);
121
    }
122
123
    public function toArray(): array
124
    {
125
        return [
126
            $this->getIdentifier(),
127
            $this->getContent(),
128
            $this->getLine(),
129
        ];
130
    }
131
132
    public function jsonSerialize()
133
    {
134
        return $this->toArray();
135
    }
136
}