1: <?php
2:
3: namespace vierbergenlars\Forage\QueryParser;
4:
5: /**
6: * A token from the lexer
7: */
8: class Token
9: {
10:
11: /**
12: * @internal
13: */
14:
15: const T_NONE = 0;
16:
17: /**
18: * A field name token
19: */
20:
21: const T_FIELD_NAME = 1;
22:
23: /**
24: * A string token
25: */
26:
27: const T_STRING = 2;
28:
29: /**
30: * A field weight token
31: */
32:
33: const T_FIELD_WEIGHT = 3;
34:
35: /**
36: * A field value token
37: */
38:
39: const T_FIELD_VALUE = 4;
40:
41: /**
42: * A search field token
43: */
44:
45: const T_FIELD_SEARCH = 5;
46:
47: /**
48: * The token type
49: * @var int
50: */
51: protected $type;
52:
53: /**
54: * The token data
55: * @var string
56: */
57: protected $data = null;
58:
59: /**
60: * The token start position
61: * @var int
62: */
63: protected $startPos;
64:
65: /**
66: * Create a new token
67: * @internal
68: * @param int $type The token type
69: * @param int $startPos The token start position
70: */
71: public function __construct($type, $startPos)
72: {
73: $this->type = $type;
74: $this->startPos = $startPos;
75: }
76:
77: /**
78: * Append data to the token
79: * @internal
80: * @param string $data
81: */
82: public function addData($data)
83: {
84: $this->data.=$data;
85: }
86:
87: /**
88: * Updates the token type
89: * @internal
90: * @param int $type
91: */
92: public function setType($type)
93: {
94: $this->type = $type;
95: }
96:
97: /**
98: * Updates the token type if it is {@link self::T_NONE}
99: * @internal
100: * @param int $type
101: */
102: public function setTypeIfNone($type)
103: {
104: if($this->type == self::T_NONE)
105: $this->type = $type;
106: }
107:
108: /**
109: * Check if the token type is {@link self::T_NONE} or the given token
110: * @internal
111: * @param int $type
112: * @return bool
113: */
114: public function isTypeNoneOr($type)
115: {
116: return ($this->type == self::T_NONE || $this->type == $type);
117: }
118:
119: /**
120: * Gets the token type
121: * @internal
122: * @return int
123: */
124: public function getType()
125: {
126: return $this->type;
127: }
128:
129: /**
130: * Gets the token data
131: * @internal
132: * @return string
133: */
134: public function getData()
135: {
136: return $this->data;
137: }
138:
139: /**
140: * Gets the token start position in the string
141: * @internal
142: * @return int
143: */
144: public function getStartPosition()
145: {
146: return $this->startPos;
147: }
148:
149: /**
150: * Sets the token start position
151: * @internal
152: * @param int $startPos
153: */
154: public function setStartPosition($startPos)
155: {
156: $this->startPos = $startPos;
157: }
158:
159: /**
160: * Gets the token's name.
161: * @param int $token A token.
162: * @return string
163: */
164: public static function getName($token)
165: {
166: $refl = new \ReflectionClass(__CLASS__);
167: $constants = $refl->getConstants();
168: $token_name = array_search($token, $constants);
169: if($token_name)
170: return $token_name;
171: return 'UNKNOWN_TOKEN';
172: }
173:
174: }
175: