FormatToken::getType()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Popy\Calendar\Parser;
4
5
/**
6
 * Format Token used by FormatLexer & FormatParser. Immutable.
7
 */
8
class FormatToken
9
{
10
    const TYPE_LITTERAL = 1;
11
    const TYPE_SYMBOL = 2;
12
    const TYPE_EOF = 3;
13
14
    /**
15
     * Token value.
16
     *
17
     * @var string|null
18
     */
19
    protected $value;
20
21
    /**
22
     * Token type.
23
     *
24
     * @var integer
25
     */
26
    protected $type;
27
28
    /**
29
     * Token alias. Usefull to map new tokens to already existing behaviours in
30
     * ResultMappers
31
     *
32
     * @var string|null
33
     */
34
    protected $alias;
35
36
    /**
37
     * Class constructor.
38
     *
39
     * @param string|null $value Token value.
40
     * @param integer     $type  Token type.
41
     */
42
    public function __construct($value, $type)
43
    {
44
        $this->value = $value;
45
        $this->type  = $type;
46
    }
47
48
    /**
49
     * Gets the token value.
50
     *
51
     * @return string|null
52
     */
53
    public function getValue()
54
    {
55
        return $this->value;
56
    }
57
58
    /**
59
     * Gets the token name (possibly aliased).
60
     *
61
     * @return string|null
62
     */
63
    public function getName()
64
    {
65
        return $this->alias ?: $this->value;
66
    }
67
68
    /**
69
     * Returns a new instance with the input alias.
70
     *
71
     * @param string|null $alias
72
     *
73
     * @return static
74
     */
75
    public function withAlias($alias)
76
    {
77
        $res = clone $this;
78
        $res->alias = $alias;
79
80
        return $res;
81
    }
82
83
    /**
84
     * Checks if token is a TYPE_SYMBOL matching the input symbol.
85
     *
86
     * @param string $symbol
87
     *
88
     * @return boolean
89
     */
90
    public function is($symbol)
91
    {
92
        return $this->type === self::TYPE_SYMBOL && $this->value === $symbol;
93
    }
94
95
    /**
96
     * Checks if token is a TYPE_SYMBOL matching one of the arguments,
97
     * or one of the symbol contained in the first argument if it is an array
98
     *
99
     * @param array|string $symbols
100
     * @param string       ...$symbols
101
     *
102
     * @return boolean
103
     */
104
    public function isOne($symbols)
105
    {
106
        if ($this->type !== self::TYPE_SYMBOL) {
107
            return false;
108
        }
109
110
        if (!is_array($symbols)) {
111
            $symbols = func_get_args();
112
        }
113
114
        return in_array($this->value, $symbols);
115
    }
116
117
    /**
118
     * Set type.
119
     *
120
     * @param integer $type
121
     */
122
    public function setType($type)
123
    {
124
        $res = clone $this;
125
126
        $res->type = $type;
127
128
        return $res;
129
    }
130
131
    /**
132
     * Checks if token is a symbol.
133
     *
134
     * @return boolean
135
     */
136
    public function isSymbol()
137
    {
138
        return $this->type === self::TYPE_SYMBOL;
139
    }
140
141
    /**
142
     * Checks if symbol is litteral
143
     *
144
     * @return boolean
145
     */
146
    public function isLitteral()
147
    {
148
        return $this->type === self::TYPE_LITTERAL;
149
    }
150
151
    /**
152
     * Set litteral.
153
     */
154
    public function setLitteral()
155
    {
156
        return $this->setType(self::TYPE_LITTERAL);
157
    }
158
159
    /**
160
     * Checks if token is of given type.
161
     *
162
     * @param integer $type
163
     *
164
     * @return boolean
165
     */
166
    public function isType($type)
167
    {
168
        return $this->type === $type;
169
    }
170
171
    /**
172
     * Get token type.
173
     *
174
     * @return integer
175
     */
176
    public function getType()
177
    {
178
        return $this->type;
179
    }
180
}
181