|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Behat Gherkin. |
|
5
|
|
|
* (c) Konstantin Kudryashov <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Behat\Gherkin\Node; |
|
12
|
|
|
|
|
13
|
|
|
use ArrayIterator; |
|
14
|
|
|
use Behat\Gherkin\Exception\NodeException; |
|
15
|
|
|
use Iterator; |
|
16
|
|
|
use IteratorAggregate; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Represents Gherkin Table argument. |
|
20
|
|
|
* |
|
21
|
|
|
* @author Konstantin Kudryashov <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
class TableNode implements ArgumentInterface, IteratorAggregate |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
private $table; |
|
29
|
|
|
/** |
|
30
|
|
|
* @var integer |
|
31
|
|
|
*/ |
|
32
|
|
|
private $maxLineLength = array(); |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Initializes table. |
|
36
|
|
|
* |
|
37
|
|
|
* @param array $table Table in form of [$rowLineNumber => [$val1, $val2, $val3]] |
|
38
|
|
|
* |
|
39
|
|
|
* @throws NodeException If the given table is invalid |
|
40
|
|
|
*/ |
|
41
|
54 |
|
public function __construct(array $table) |
|
42
|
|
|
{ |
|
43
|
54 |
|
$this->table = $table; |
|
44
|
54 |
|
$columnCount = null; |
|
45
|
|
|
|
|
46
|
54 |
|
foreach ($this->getRows() as $row) { |
|
|
|
|
|
|
47
|
|
|
|
|
48
|
41 |
|
if (!is_array($row)) { |
|
49
|
1 |
|
throw new NodeException('Table is not two-dimensional.'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
40 |
|
if ($columnCount === null) { |
|
53
|
40 |
|
$columnCount = count($row); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
40 |
|
if (count($row) !== $columnCount) { |
|
57
|
1 |
|
throw new NodeException('Table does not have same number of columns in every row.'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
40 |
|
if (!is_array($row)) { |
|
61
|
|
|
throw new NodeException('Table is not two-dimensional.'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
40 |
|
foreach ($row as $column => $string) { |
|
65
|
40 |
|
if (!isset($this->maxLineLength[$column])) { |
|
66
|
40 |
|
$this->maxLineLength[$column] = 0; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
40 |
|
if (!is_scalar($string)) { |
|
70
|
1 |
|
throw new NodeException('Table is not two-dimensional.'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
39 |
|
$this->maxLineLength[$column] = max($this->maxLineLength[$column], mb_strlen($string, 'utf8')); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
51 |
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Creates a table from a given list. |
|
80
|
|
|
* |
|
81
|
|
|
* @param array $list One-dimensional array |
|
82
|
|
|
* |
|
83
|
|
|
* @return TableNode |
|
84
|
|
|
* |
|
85
|
|
|
* @throws NodeException If the given list is not a one-dimensional array |
|
86
|
|
|
*/ |
|
87
|
2 |
|
public static function fromList(array $list) |
|
88
|
|
|
{ |
|
89
|
2 |
|
if (count($list) !== count($list, COUNT_RECURSIVE)) { |
|
90
|
1 |
|
throw new NodeException('List is not a one-dimensional array.'); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
1 |
|
array_walk($list, function (&$item) { |
|
94
|
1 |
|
$item = array($item); |
|
95
|
1 |
|
}); |
|
96
|
1 |
|
return new self($list); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Returns node type. |
|
101
|
|
|
* |
|
102
|
|
|
* @return string |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getNodeType() |
|
105
|
|
|
{ |
|
106
|
|
|
return 'Table'; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Returns table hash, formed by columns (ColumnsHash). |
|
111
|
|
|
* |
|
112
|
|
|
* @return array |
|
113
|
|
|
*/ |
|
114
|
4 |
|
public function getHash() |
|
115
|
|
|
{ |
|
116
|
4 |
|
return $this->getColumnsHash(); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Returns table hash, formed by columns. |
|
121
|
|
|
* |
|
122
|
|
|
* @return array |
|
123
|
|
|
*/ |
|
124
|
19 |
|
public function getColumnsHash() |
|
125
|
|
|
{ |
|
126
|
19 |
|
$rows = $this->getRows(); |
|
127
|
19 |
|
$keys = array_shift($rows); |
|
128
|
|
|
|
|
129
|
19 |
|
$hash = array(); |
|
130
|
19 |
|
foreach ($rows as $row) { |
|
131
|
11 |
|
$hash[] = array_combine($keys, $row); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
19 |
|
return $hash; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Returns table hash, formed by rows. |
|
139
|
|
|
* |
|
140
|
|
|
* @return array |
|
141
|
|
|
*/ |
|
142
|
2 |
|
public function getRowsHash() |
|
143
|
|
|
{ |
|
144
|
2 |
|
$hash = array(); |
|
145
|
|
|
|
|
146
|
2 |
|
foreach ($this->getRows() as $row) { |
|
147
|
2 |
|
$hash[array_shift($row)] = (1 == count($row)) ? $row[0] : $row; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
2 |
|
return $hash; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Returns numerated table lines. |
|
155
|
|
|
* Line numbers are keys, lines are values. |
|
156
|
|
|
* |
|
157
|
|
|
* @return array |
|
158
|
|
|
*/ |
|
159
|
4 |
|
public function getTable() |
|
160
|
|
|
{ |
|
161
|
4 |
|
return $this->table; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Returns table rows. |
|
166
|
|
|
* |
|
167
|
|
|
* @return array |
|
168
|
|
|
*/ |
|
169
|
54 |
|
public function getRows() |
|
170
|
|
|
{ |
|
171
|
54 |
|
return array_values($this->table); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Returns table definition lines. |
|
176
|
|
|
* |
|
177
|
|
|
* @return array |
|
178
|
|
|
*/ |
|
179
|
5 |
|
public function getLines() |
|
180
|
|
|
{ |
|
181
|
5 |
|
return array_keys($this->table); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Returns specific row in a table. |
|
186
|
|
|
* |
|
187
|
|
|
* @param integer $index Row number |
|
188
|
|
|
* |
|
189
|
|
|
* @return array |
|
190
|
|
|
* |
|
191
|
|
|
* @throws NodeException If row with specified index does not exist |
|
192
|
|
|
*/ |
|
193
|
8 |
View Code Duplication |
public function getRow($index) |
|
|
|
|
|
|
194
|
|
|
{ |
|
195
|
8 |
|
$rows = $this->getRows(); |
|
196
|
|
|
|
|
197
|
8 |
|
if (!isset($rows[$index])) { |
|
198
|
|
|
throw new NodeException(sprintf('Rows #%d does not exist in table.', $index)); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
8 |
|
return $rows[$index]; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Returns specific column in a table. |
|
206
|
|
|
* |
|
207
|
|
|
* @param integer $index Column number |
|
208
|
|
|
* |
|
209
|
|
|
* @return array |
|
210
|
|
|
* |
|
211
|
|
|
* @throws NodeException If column with specified index does not exist |
|
212
|
|
|
*/ |
|
213
|
1 |
|
public function getColumn($index) |
|
214
|
|
|
{ |
|
215
|
1 |
|
if ($index >= count($this->getRow(0))) { |
|
216
|
|
|
throw new NodeException(sprintf('Column #%d does not exist in table.', $index)); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
1 |
|
$rows = $this->getRows(); |
|
220
|
1 |
|
$column = array(); |
|
221
|
|
|
|
|
222
|
1 |
|
foreach ($rows as $row) { |
|
223
|
1 |
|
$column[] = $row[$index]; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
1 |
|
return $column; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Returns line number at which specific row was defined. |
|
231
|
|
|
* |
|
232
|
|
|
* @param integer $index |
|
233
|
|
|
* |
|
234
|
|
|
* @return integer |
|
235
|
|
|
* |
|
236
|
|
|
* @throws NodeException If row with specified index does not exist |
|
237
|
|
|
*/ |
|
238
|
4 |
View Code Duplication |
public function getRowLine($index) |
|
|
|
|
|
|
239
|
|
|
{ |
|
240
|
4 |
|
$lines = array_keys($this->table); |
|
241
|
|
|
|
|
242
|
4 |
|
if (!isset($lines[$index])) { |
|
243
|
|
|
throw new NodeException(sprintf('Rows #%d does not exist in table.', $index)); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
4 |
|
return $lines[$index]; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* Converts row into delimited string. |
|
251
|
|
|
* |
|
252
|
|
|
* @param integer $rowNum Row number |
|
253
|
|
|
* |
|
254
|
|
|
* @return string |
|
255
|
|
|
*/ |
|
256
|
5 |
|
public function getRowAsString($rowNum) |
|
257
|
|
|
{ |
|
258
|
5 |
|
$values = array(); |
|
259
|
5 |
|
foreach ($this->getRow($rowNum) as $column => $value) { |
|
260
|
5 |
|
$values[] = $this->padRight(' ' . $value . ' ', $this->maxLineLength[$column] + 2); |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
5 |
|
return sprintf('|%s|', implode('|', $values)); |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* Converts row into delimited string. |
|
268
|
|
|
* |
|
269
|
|
|
* @param integer $rowNum Row number |
|
270
|
|
|
* @param callable $wrapper Wrapper function |
|
271
|
|
|
* |
|
272
|
|
|
* @return string |
|
273
|
|
|
*/ |
|
274
|
|
|
public function getRowAsStringWithWrappedValues($rowNum, $wrapper) |
|
275
|
|
|
{ |
|
276
|
|
|
$values = array(); |
|
277
|
|
|
foreach ($this->getRow($rowNum) as $column => $value) { |
|
278
|
|
|
$value = $this->padRight(' ' . $value . ' ', $this->maxLineLength[$column] + 2); |
|
279
|
|
|
|
|
280
|
|
|
$values[] = call_user_func($wrapper, $value, $column); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
return sprintf('|%s|', implode('|', $values)); |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* Converts entire table into string |
|
288
|
|
|
* |
|
289
|
|
|
* @return string |
|
290
|
|
|
*/ |
|
291
|
2 |
|
public function getTableAsString() |
|
292
|
|
|
{ |
|
293
|
2 |
|
$lines = array(); |
|
294
|
2 |
|
for ($i = 0; $i < count($this->getRows()); $i++) { |
|
|
|
|
|
|
295
|
2 |
|
$lines[] = $this->getRowAsString($i); |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
2 |
|
return implode("\n", $lines); |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
/** |
|
302
|
|
|
* Returns line number at which table was started. |
|
303
|
|
|
* |
|
304
|
|
|
* @return integer |
|
305
|
|
|
*/ |
|
306
|
|
|
public function getLine() |
|
307
|
|
|
{ |
|
308
|
|
|
return $this->getRowLine(0); |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
/** |
|
312
|
|
|
* Converts table into string |
|
313
|
|
|
* |
|
314
|
|
|
* @return string |
|
315
|
|
|
*/ |
|
316
|
|
|
public function __toString() |
|
317
|
|
|
{ |
|
318
|
|
|
return $this->getTableAsString(); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
/** |
|
322
|
|
|
* Retrieves a hash iterator. |
|
323
|
|
|
* |
|
324
|
|
|
* @return Iterator |
|
325
|
|
|
*/ |
|
326
|
1 |
|
public function getIterator() |
|
327
|
|
|
{ |
|
328
|
1 |
|
return new ArrayIterator($this->getHash()); |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
/** |
|
332
|
|
|
* Pads string right. |
|
333
|
|
|
* |
|
334
|
|
|
* @param string $text Text to pad |
|
335
|
|
|
* @param integer $length Length |
|
336
|
|
|
* |
|
337
|
|
|
* @return string |
|
338
|
|
|
*/ |
|
339
|
5 |
|
protected function padRight($text, $length) |
|
340
|
|
|
{ |
|
341
|
5 |
|
while ($length > mb_strlen($text, 'utf8')) { |
|
342
|
5 |
|
$text = $text . ' '; |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
5 |
|
return $text; |
|
346
|
|
|
} |
|
347
|
|
|
} |
|
348
|
|
|
|