1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Platine Expression |
5
|
|
|
* |
6
|
|
|
* Platine Expression is an expression parser, evaluator with support of custom |
7
|
|
|
* operators and functions |
8
|
|
|
* |
9
|
|
|
* This content is released under the MIT License (MIT) |
10
|
|
|
* |
11
|
|
|
* Copyright (c) 2020 Platine Expression |
12
|
|
|
* Copyright (c) Alexander Kiryukhin |
13
|
|
|
* |
14
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
15
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
16
|
|
|
* in the Software without restriction, including without limitation the rights |
17
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
18
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
19
|
|
|
* furnished to do so, subject to the following conditions: |
20
|
|
|
* |
21
|
|
|
* The above copyright notice and this permission notice shall be included in all |
22
|
|
|
* copies or substantial portions of the Software. |
23
|
|
|
* |
24
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
25
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
26
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
27
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
28
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
29
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
30
|
|
|
* SOFTWARE. |
31
|
|
|
*/ |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @file Token.php |
35
|
|
|
* |
36
|
|
|
* The Token class |
37
|
|
|
* |
38
|
|
|
* @package Platine\Expression |
39
|
|
|
* @author Platine Developers Team |
40
|
|
|
* @copyright Copyright (c) 2020 |
41
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
42
|
|
|
* @link https://www.platine-php.com |
43
|
|
|
* @version 1.0.0 |
44
|
|
|
* @filesource |
45
|
|
|
*/ |
46
|
|
|
declare(strict_types=1); |
47
|
|
|
|
48
|
|
|
namespace Platine\Expression; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @class Token |
52
|
|
|
* @package Platine\Expression |
53
|
|
|
*/ |
54
|
|
|
class Token |
55
|
|
|
{ |
56
|
|
|
/** |
57
|
|
|
* Constants |
58
|
|
|
*/ |
59
|
|
|
public const LITERAL = 'literal'; |
60
|
|
|
public const VARIABLE = 'variable'; |
61
|
|
|
public const OPERATOR = 'operator'; |
62
|
|
|
public const LEFT_PARENTHESIS = 'LP'; |
63
|
|
|
public const RIGHT_PARENTHESIS = 'RP'; |
64
|
|
|
public const FUNCTION = 'function'; |
65
|
|
|
public const PARAM_SEPARATOR = 'PS'; |
66
|
|
|
public const STRING = 'string'; |
67
|
|
|
public const SPACE = 'space'; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* The function number of parameter |
71
|
|
|
* @var int |
72
|
|
|
*/ |
73
|
|
|
protected int $paramCount = 0; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Create new instance |
77
|
|
|
* @param string $type The token type |
78
|
|
|
* @param mixed $value The token value |
79
|
|
|
* @param string|null $name The token name |
80
|
|
|
*/ |
81
|
|
|
public function __construct( |
82
|
|
|
protected string $type, |
83
|
|
|
protected mixed $value, |
84
|
|
|
protected ?string $name = null |
85
|
|
|
) { |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Return the token type |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
public function getType(): string |
93
|
|
|
{ |
94
|
|
|
return $this->type; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Return the token value |
99
|
|
|
* @return mixed |
100
|
|
|
*/ |
101
|
|
|
public function getValue(): mixed |
102
|
|
|
{ |
103
|
|
|
return $this->value; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Return the token name |
108
|
|
|
* @return string|null |
109
|
|
|
*/ |
110
|
|
|
public function getName(): ?string |
111
|
|
|
{ |
112
|
|
|
return $this->name; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Return the number of function parameter count |
117
|
|
|
* @return int |
118
|
|
|
*/ |
119
|
|
|
public function getParamCount(): int |
120
|
|
|
{ |
121
|
|
|
return $this->paramCount; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Set the parameter count |
126
|
|
|
* @param int $paramCount |
127
|
|
|
* @return $this |
128
|
|
|
*/ |
129
|
|
|
public function setParamCount(int $paramCount): self |
130
|
|
|
{ |
131
|
|
|
$this->paramCount = $paramCount; |
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Set the token value |
137
|
|
|
* @param mixed $value |
138
|
|
|
* @return $this |
139
|
|
|
*/ |
140
|
|
|
public function setValue(mixed $value): self |
141
|
|
|
{ |
142
|
|
|
$this->value = $value; |
143
|
|
|
return $this; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|