TableNode.php 7.7 KB

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