Security update for permissions_by_term
[yaffs-website] / vendor / behat / gherkin / src / Behat / Gherkin / Node / ExampleNode.php
1 <?php
2
3 /*
4  * This file is part of the Behat Gherkin.
5  * (c) Konstantin Kudryashov <ever.zet@gmail.com>
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 /**
14  * Represents Gherkin Outline Example.
15  *
16  * @author Konstantin Kudryashov <ever.zet@gmail.com>
17  */
18 class ExampleNode implements ScenarioInterface
19 {
20     /**
21      * @var string
22      */
23     private $title;
24     /**
25      * @var string[]
26      */
27     private $tags;
28     /**
29      * @var StepNode[]
30      */
31     private $outlineSteps;
32     /**
33      * @var string[]
34      */
35     private $tokens;
36     /**
37      * @var integer
38      */
39     private $line;
40     /**
41      * @var null|StepNode[]
42      */
43     private $steps;
44     /**
45      * @var string
46      */
47     private $outlineTitle;
48
49     /**
50      * Initializes outline.
51      *
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
58      */
59     public function __construct($title, array $tags, $outlineSteps, array $tokens, $line, $outlineTitle = null)
60     {
61         $this->title = $title;
62         $this->tags = $tags;
63         $this->outlineSteps = $outlineSteps;
64         $this->tokens = $tokens;
65         $this->line = $line;
66         $this->outlineTitle = $outlineTitle;
67     }
68
69     /**
70      * Returns node type string
71      *
72      * @return string
73      */
74     public function getNodeType()
75     {
76         return 'Example';
77     }
78
79     /**
80      * Returns node keyword.
81      *
82      * @return string
83      */
84     public function getKeyword()
85     {
86         return $this->getNodeType();
87     }
88
89     /**
90      * Returns example title.
91      *
92      * @return string
93      */
94     public function getTitle()
95     {
96         return $this->title;
97     }
98
99     /**
100      * Checks if outline is tagged with tag.
101      *
102      * @param string $tag
103      *
104      * @return Boolean
105      */
106     public function hasTag($tag)
107     {
108         return in_array($tag, $this->getTags());
109     }
110
111     /**
112      * Checks if outline has tags (both inherited from feature and own).
113      *
114      * @return Boolean
115      */
116     public function hasTags()
117     {
118         return 0 < count($this->getTags());
119     }
120
121     /**
122      * Returns outline tags (including inherited from feature).
123      *
124      * @return string[]
125      */
126     public function getTags()
127     {
128         return $this->tags;
129     }
130
131     /**
132      * Checks if outline has steps.
133      *
134      * @return Boolean
135      */
136     public function hasSteps()
137     {
138         return 0 < count($this->outlineSteps);
139     }
140
141     /**
142      * Returns outline steps.
143      *
144      * @return StepNode[]
145      */
146     public function getSteps()
147     {
148         return $this->steps = $this->steps ? : $this->createExampleSteps();
149     }
150
151     /**
152      * Returns example tokens.
153      *
154      * @return string[]
155      */
156     public function getTokens()
157     {
158         return $this->tokens;
159     }
160
161     /**
162      * Returns outline declaration line number.
163      *
164      * @return integer
165      */
166     public function getLine()
167     {
168         return $this->line;
169     }
170
171     /**
172      * Returns outline title.
173      *
174      * @return string
175      */
176     public function getOutlineTitle()
177     {
178         return $this->outlineTitle;
179     }
180
181     /**
182      * Creates steps for this example from abstract outline steps.
183      *
184      * @return StepNode[]
185      */
186     protected function createExampleSteps()
187     {
188         $steps = array();
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();
195
196             $steps[] = new StepNode($keyword, $text, $args, $line, $keywordType);
197         }
198
199         return $steps;
200     }
201
202     /**
203      * Replaces tokens in arguments with row values.
204      *
205      * @param ArgumentInterface[] $arguments
206      *
207      * @return ArgumentInterface[]
208      */
209     protected function replaceArgumentsTokens(array $arguments)
210     {
211         foreach ($arguments as $num => $argument) {
212             if ($argument instanceof TableNode) {
213                 $arguments[$num] = $this->replaceTableArgumentTokens($argument);
214             }
215             if ($argument instanceof PyStringNode) {
216                 $arguments[$num] = $this->replacePyStringArgumentTokens($argument);
217             }
218         }
219
220         return $arguments;
221     }
222
223     /**
224      * Replaces tokens in table with row values.
225      *
226      * @param TableNode $argument
227      *
228      * @return TableNode
229      */
230     protected function replaceTableArgumentTokens(TableNode $argument)
231     {
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]);
236             }
237         }
238
239         return new TableNode($table);
240     }
241
242     /**
243      * Replaces tokens in PyString with row values.
244      *
245      * @param PyStringNode $argument
246      *
247      * @return PyStringNode
248      */
249     protected function replacePyStringArgumentTokens(PyStringNode $argument)
250     {
251         $strings = $argument->getStrings();
252         foreach ($strings as $line => $string) {
253             $strings[$line] = $this->replaceTextTokens($strings[$line]);
254         }
255
256         return new PyStringNode($strings, $argument->getLine());
257     }
258
259     /**
260      * Replaces tokens in text with row values.
261      *
262      * @param string $text
263      *
264      * @return string
265      */
266     protected function replaceTextTokens($text)
267     {
268         foreach ($this->tokens as $key => $val) {
269             $text = str_replace('<' . $key . '>', $val, $text);
270         }
271
272         return $text;
273     }
274 }