1 | <?php |
||
17 | class Token { |
||
18 | |||
19 | const INVALID_TYPE = -1; |
||
20 | |||
21 | const INVALID_LINE = -1; |
||
22 | |||
23 | const INVALID_VALUE = null; |
||
24 | |||
25 | const INVALID_INDEX = -1; |
||
26 | |||
27 | /** |
||
28 | * @var int |
||
29 | */ |
||
30 | protected $type = self::INVALID_TYPE; |
||
31 | |||
32 | /** |
||
33 | * @var string|null |
||
34 | */ |
||
35 | protected $value; |
||
36 | |||
37 | /** |
||
38 | * @var int |
||
39 | */ |
||
40 | protected $line = self::INVALID_LINE; |
||
41 | |||
42 | /** |
||
43 | * Indicate position in current collection |
||
44 | * |
||
45 | * @var int |
||
46 | */ |
||
47 | protected $index = self::INVALID_INDEX; |
||
48 | |||
49 | |||
50 | /** |
||
51 | * You need to provide at least 3 elements |
||
52 | * |
||
53 | * @param array $data |
||
54 | * @throws Exception |
||
55 | */ |
||
56 | 537 | public function __construct(array $data = []) { |
|
57 | 537 | if (!empty($data)) { |
|
58 | 486 | $this->setData($data); |
|
59 | } |
||
60 | 528 | } |
|
61 | |||
62 | |||
63 | /** |
||
64 | * @return string |
||
65 | */ |
||
66 | 6 | public function __toString() { |
|
67 | 6 | return $this->assemble(); |
|
68 | } |
||
69 | |||
70 | |||
71 | /** |
||
72 | * @return string |
||
73 | */ |
||
74 | 9 | public function assemble() : string { |
|
75 | 9 | return $this->value !== null ? (string) $this->value : ''; |
|
76 | } |
||
77 | |||
78 | |||
79 | /** |
||
80 | * @param array $data |
||
81 | * @return $this |
||
82 | * @throws Exception |
||
83 | */ |
||
84 | 486 | protected function setData(array $data) : self { |
|
85 | 486 | if (!array_key_exists(0, $data)) { |
|
86 | 3 | throw new InvalidArgumentException('Please provide type of token'); |
|
87 | } |
||
88 | |||
89 | 483 | $this->setType((int) $data[0]); |
|
90 | |||
91 | 483 | if (!isset($data[1])) { |
|
92 | 3 | throw new InvalidArgumentException('Please provide value of token'); |
|
93 | } |
||
94 | |||
95 | 480 | $this->setValue($data[1]); |
|
96 | |||
97 | 480 | if (!array_key_exists(2, $data)) { |
|
98 | 3 | throw new InvalidArgumentException('Please provide line of token'); |
|
99 | } |
||
100 | |||
101 | 477 | $this->setLine($data[2]); |
|
102 | |||
103 | 477 | if (array_key_exists(3, $data)) { |
|
104 | 3 | $this->setIndex($data[3]); |
|
105 | } |
||
106 | |||
107 | 477 | return $this; |
|
108 | } |
||
109 | |||
110 | |||
111 | /** |
||
112 | * @return array |
||
113 | */ |
||
114 | 9 | public function getData() : array { |
|
117 | |||
118 | |||
119 | /** |
||
120 | * @param int $type |
||
121 | * @return $this |
||
122 | */ |
||
123 | 483 | public function setType(int $type) : self { |
|
127 | |||
128 | |||
129 | /** |
||
130 | * @return int |
||
131 | */ |
||
132 | 297 | public function getType() : int { |
|
135 | |||
136 | |||
137 | /** |
||
138 | * @return string |
||
139 | */ |
||
140 | 6 | public function getTypeName() : string { |
|
143 | |||
144 | |||
145 | /** |
||
146 | * @return string|null |
||
147 | */ |
||
148 | 495 | public function getValue() { |
|
151 | |||
152 | |||
153 | /** |
||
154 | * @param string|int $value |
||
155 | * @throws InvalidArgumentException |
||
156 | * @return $this |
||
157 | */ |
||
158 | 498 | public function setValue($value) : self { |
|
165 | |||
166 | |||
167 | /** |
||
168 | * @return int |
||
169 | */ |
||
170 | 453 | public function getLine() : int { |
|
173 | |||
174 | |||
175 | /** |
||
176 | * @param int $line |
||
177 | * @return $this |
||
178 | */ |
||
179 | 480 | public function setLine(int $line) : self { |
|
183 | |||
184 | |||
185 | /** |
||
186 | * @return bool |
||
187 | */ |
||
188 | 468 | public function isValid() : bool { |
|
191 | |||
192 | |||
193 | /** |
||
194 | * Remove all data from token so this token become invalid |
||
195 | * |
||
196 | * @return $this |
||
197 | */ |
||
198 | 90 | public function remove() : self { |
|
205 | |||
206 | |||
207 | /** |
||
208 | * Add part to the end of value |
||
209 | * |
||
210 | * @param string $part |
||
211 | * @return $this |
||
212 | * @throws Exception |
||
213 | */ |
||
214 | 33 | public function appendToValue($part) : self { |
|
224 | |||
225 | |||
226 | /** |
||
227 | * Add part to the begin of value |
||
228 | * |
||
229 | * @param string $part |
||
230 | * @return $this |
||
231 | * @throws Exception |
||
232 | */ |
||
233 | 36 | public function prependToValue($part) : self { |
|
243 | |||
244 | |||
245 | /** |
||
246 | * @return null|int |
||
247 | */ |
||
248 | 225 | public function getIndex() { |
|
251 | |||
252 | |||
253 | /** |
||
254 | * @param int $index |
||
255 | * @return $this |
||
256 | */ |
||
257 | 489 | public function setIndex(int $index) : self { |
|
261 | |||
262 | |||
263 | 9 | public function equal(Token $token) : bool { |
|
270 | |||
271 | } |