OutlineNode.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. /**
  11. * Represents Gherkin Outline.
  12. *
  13. * @author Konstantin Kudryashov <ever.zet@gmail.com>
  14. */
  15. class OutlineNode implements ScenarioInterface
  16. {
  17. /**
  18. * @var string
  19. */
  20. private $title;
  21. /**
  22. * @var string[]
  23. */
  24. private $tags;
  25. /**
  26. * @var StepNode[]
  27. */
  28. private $steps;
  29. /**
  30. * @var ExampleTableNode
  31. */
  32. private $table;
  33. /**
  34. * @var string
  35. */
  36. private $keyword;
  37. /**
  38. * @var integer
  39. */
  40. private $line;
  41. /**
  42. * @var null|ExampleNode[]
  43. */
  44. private $examples;
  45. /**
  46. * Initializes outline.
  47. *
  48. * @param null|string $title
  49. * @param string[] $tags
  50. * @param StepNode[] $steps
  51. * @param ExampleTableNode $table
  52. * @param string $keyword
  53. * @param integer $line
  54. */
  55. public function __construct(
  56. $title,
  57. array $tags,
  58. array $steps,
  59. ExampleTableNode $table,
  60. $keyword,
  61. $line
  62. ) {
  63. $this->title = $title;
  64. $this->tags = $tags;
  65. $this->steps = $steps;
  66. $this->table = $table;
  67. $this->keyword = $keyword;
  68. $this->line = $line;
  69. }
  70. /**
  71. * Returns node type string
  72. *
  73. * @return string
  74. */
  75. public function getNodeType()
  76. {
  77. return 'Outline';
  78. }
  79. /**
  80. * Returns outline title.
  81. *
  82. * @return null|string
  83. */
  84. public function getTitle()
  85. {
  86. return $this->title;
  87. }
  88. /**
  89. * Checks if outline is tagged with tag.
  90. *
  91. * @param string $tag
  92. *
  93. * @return Boolean
  94. */
  95. public function hasTag($tag)
  96. {
  97. return in_array($tag, $this->getTags());
  98. }
  99. /**
  100. * Checks if outline has tags (both inherited from feature and own).
  101. *
  102. * @return Boolean
  103. */
  104. public function hasTags()
  105. {
  106. return 0 < count($this->getTags());
  107. }
  108. /**
  109. * Returns outline tags (including inherited from feature).
  110. *
  111. * @return string[]
  112. */
  113. public function getTags()
  114. {
  115. return $this->tags;
  116. }
  117. /**
  118. * Checks if outline has steps.
  119. *
  120. * @return Boolean
  121. */
  122. public function hasSteps()
  123. {
  124. return 0 < count($this->steps);
  125. }
  126. /**
  127. * Returns outline steps.
  128. *
  129. * @return StepNode[]
  130. */
  131. public function getSteps()
  132. {
  133. return $this->steps;
  134. }
  135. /**
  136. * Checks if outline has examples.
  137. *
  138. * @return Boolean
  139. */
  140. public function hasExamples()
  141. {
  142. return 0 < count($this->table->getColumnsHash());
  143. }
  144. /**
  145. * Returns examples table.
  146. *
  147. * @return ExampleTableNode
  148. */
  149. public function getExampleTable()
  150. {
  151. return $this->table;
  152. }
  153. /**
  154. * Returns list of examples for the outline.
  155. *
  156. * @return ExampleNode[]
  157. */
  158. public function getExamples()
  159. {
  160. return $this->examples = $this->examples ? : $this->createExamples();
  161. }
  162. /**
  163. * Returns outline keyword.
  164. *
  165. * @return string
  166. */
  167. public function getKeyword()
  168. {
  169. return $this->keyword;
  170. }
  171. /**
  172. * Returns outline declaration line number.
  173. *
  174. * @return integer
  175. */
  176. public function getLine()
  177. {
  178. return $this->line;
  179. }
  180. /**
  181. * Creates examples for this outline using examples table.
  182. *
  183. * @return ExampleNode[]
  184. */
  185. protected function createExamples()
  186. {
  187. $examples = array();
  188. foreach ($this->table->getColumnsHash() as $rowNum => $row) {
  189. $examples[] = new ExampleNode(
  190. $this->table->getRowAsString($rowNum + 1),
  191. $this->tags,
  192. $this->getSteps(),
  193. $row,
  194. $this->table->getRowLine($rowNum + 1),
  195. $this->getTitle()
  196. );
  197. }
  198. return $examples;
  199. }
  200. }