4 * This file is part of the Behat Gherkin.
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
11 namespace Behat\Gherkin\Node;
14 * Represents Gherkin Outline Example.
16 * @author Konstantin Kudryashov <ever.zet@gmail.com>
18 class ExampleNode implements ScenarioInterface
31 private $outlineSteps;
41 * @var null|StepNode[]
47 private $outlineTitle;
50 * Initializes outline.
52 * @param string $title
53 * @param string[] $tags
54 * @param StepNode[] $outlineSteps
55 * @param string[] $tokens
56 * @param integer $line
57 * @param string|null $outlineTitle
59 public function __construct($title, array $tags, $outlineSteps, array $tokens, $line, $outlineTitle = null)
61 $this->title = $title;
63 $this->outlineSteps = $outlineSteps;
64 $this->tokens = $tokens;
66 $this->outlineTitle = $outlineTitle;
70 * Returns node type string
74 public function getNodeType()
80 * Returns node keyword.
84 public function getKeyword()
86 return $this->getNodeType();
90 * Returns example title.
94 public function getTitle()
100 * Checks if outline is tagged with tag.
106 public function hasTag($tag)
108 return in_array($tag, $this->getTags());
112 * Checks if outline has tags (both inherited from feature and own).
116 public function hasTags()
118 return 0 < count($this->getTags());
122 * Returns outline tags (including inherited from feature).
126 public function getTags()
132 * Checks if outline has steps.
136 public function hasSteps()
138 return 0 < count($this->outlineSteps);
142 * Returns outline steps.
146 public function getSteps()
148 return $this->steps = $this->steps ? : $this->createExampleSteps();
152 * Returns example tokens.
156 public function getTokens()
158 return $this->tokens;
162 * Returns outline declaration line number.
166 public function getLine()
172 * Returns outline title.
176 public function getOutlineTitle()
178 return $this->outlineTitle;
182 * Creates steps for this example from abstract outline steps.
186 protected function createExampleSteps()
189 foreach ($this->outlineSteps as $outlineStep) {
190 $keyword = $outlineStep->getKeyword();
191 $keywordType = $outlineStep->getKeywordType();
192 $text = $this->replaceTextTokens($outlineStep->getText());
193 $args = $this->replaceArgumentsTokens($outlineStep->getArguments());
194 $line = $outlineStep->getLine();
196 $steps[] = new StepNode($keyword, $text, $args, $line, $keywordType);
203 * Replaces tokens in arguments with row values.
205 * @param ArgumentInterface[] $arguments
207 * @return ArgumentInterface[]
209 protected function replaceArgumentsTokens(array $arguments)
211 foreach ($arguments as $num => $argument) {
212 if ($argument instanceof TableNode) {
213 $arguments[$num] = $this->replaceTableArgumentTokens($argument);
215 if ($argument instanceof PyStringNode) {
216 $arguments[$num] = $this->replacePyStringArgumentTokens($argument);
224 * Replaces tokens in table with row values.
226 * @param TableNode $argument
230 protected function replaceTableArgumentTokens(TableNode $argument)
232 $table = $argument->getTable();
233 foreach ($table as $line => $row) {
234 foreach (array_keys($row) as $col) {
235 $table[$line][$col] = $this->replaceTextTokens($table[$line][$col]);
239 return new TableNode($table);
243 * Replaces tokens in PyString with row values.
245 * @param PyStringNode $argument
247 * @return PyStringNode
249 protected function replacePyStringArgumentTokens(PyStringNode $argument)
251 $strings = $argument->getStrings();
252 foreach ($strings as $line => $string) {
253 $strings[$line] = $this->replaceTextTokens($strings[$line]);
256 return new PyStringNode($strings, $argument->getLine());
260 * Replaces tokens in text with row values.
262 * @param string $text
266 protected function replaceTextTokens($text)
268 foreach ($this->tokens as $key => $val) {
269 $text = str_replace('<' . $key . '>', $val, $text);