Security update for permissions_by_term
[yaffs-website] / vendor / behat / gherkin / src / Behat / Gherkin / Loader / ArrayLoader.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\Loader;
12
13 use Behat\Gherkin\Node\BackgroundNode;
14 use Behat\Gherkin\Node\ExampleTableNode;
15 use Behat\Gherkin\Node\FeatureNode;
16 use Behat\Gherkin\Node\OutlineNode;
17 use Behat\Gherkin\Node\PyStringNode;
18 use Behat\Gherkin\Node\ScenarioNode;
19 use Behat\Gherkin\Node\StepNode;
20 use Behat\Gherkin\Node\TableNode;
21
22 /**
23  * From-array loader.
24  *
25  * @author Konstantin Kudryashov <ever.zet@gmail.com>
26  */
27 class ArrayLoader implements LoaderInterface
28 {
29     /**
30      * Checks if current loader supports provided resource.
31      *
32      * @param mixed $resource Resource to load
33      *
34      * @return Boolean
35      */
36     public function supports($resource)
37     {
38         return is_array($resource) && (isset($resource['features']) || isset($resource['feature']));
39     }
40
41     /**
42      * Loads features from provided resource.
43      *
44      * @param mixed $resource Resource to load
45      *
46      * @return FeatureNode[]
47      */
48     public function load($resource)
49     {
50         $features = array();
51
52         if (isset($resource['features'])) {
53             foreach ($resource['features'] as $iterator => $hash) {
54                 $feature = $this->loadFeatureHash($hash, $iterator);
55                 $features[] = $feature;
56             }
57         } elseif (isset($resource['feature'])) {
58             $feature = $this->loadFeatureHash($resource['feature']);
59             $features[] = $feature;
60         }
61
62         return $features;
63     }
64
65     /**
66      * Loads feature from provided feature hash.
67      *
68      * @param array   $hash Feature hash
69      * @param integer $line
70      *
71      * @return FeatureNode
72      */
73     protected function loadFeatureHash(array $hash, $line = 0)
74     {
75         $hash = array_merge(
76             array(
77                 'title' => null,
78                 'description' => null,
79                 'tags' => array(),
80                 'keyword' => 'Feature',
81                 'language' => 'en',
82                 'line' => $line,
83                 'scenarios' => array(),
84             ),
85             $hash
86         );
87         $background = isset($hash['background']) ? $this->loadBackgroundHash($hash['background']) : null;
88
89         $scenarios = array();
90         foreach ((array) $hash['scenarios'] as $scenarioIterator => $scenarioHash) {
91             if (isset($scenarioHash['type']) && 'outline' === $scenarioHash['type']) {
92                 $scenarios[] = $this->loadOutlineHash($scenarioHash, $scenarioIterator);
93             } else {
94                 $scenarios[] = $this->loadScenarioHash($scenarioHash, $scenarioIterator);
95             }
96         }
97
98         return new FeatureNode($hash['title'], $hash['description'], $hash['tags'], $background, $scenarios, $hash['keyword'], $hash['language'], null, $hash['line']);
99     }
100
101     /**
102      * Loads background from provided hash.
103      *
104      * @param array $hash Background hash
105      *
106      * @return BackgroundNode
107      */
108     protected function loadBackgroundHash(array $hash)
109     {
110         $hash = array_merge(
111             array(
112                 'title' => null,
113                 'keyword' => 'Background',
114                 'line' => 0,
115                 'steps' => array(),
116             ),
117             $hash
118         );
119
120         $steps = $this->loadStepsHash($hash['steps']);
121
122         return new BackgroundNode($hash['title'], $steps, $hash['keyword'], $hash['line']);
123     }
124
125     /**
126      * Loads scenario from provided scenario hash.
127      *
128      * @param array   $hash Scenario hash
129      * @param integer $line Scenario definition line
130      *
131      * @return ScenarioNode
132      */
133     protected function loadScenarioHash(array $hash, $line = 0)
134     {
135         $hash = array_merge(
136             array(
137                 'title' => null,
138                 'tags' => array(),
139                 'keyword' => 'Scenario',
140                 'line' => $line,
141                 'steps' => array(),
142             ),
143             $hash
144         );
145
146         $steps = $this->loadStepsHash($hash['steps']);
147
148         return new ScenarioNode($hash['title'], $hash['tags'], $steps, $hash['keyword'], $hash['line']);
149     }
150
151     /**
152      * Loads outline from provided outline hash.
153      *
154      * @param array   $hash Outline hash
155      * @param integer $line Outline definition line
156      *
157      * @return OutlineNode
158      */
159     protected function loadOutlineHash(array $hash, $line = 0)
160     {
161         $hash = array_merge(
162             array(
163                 'title' => null,
164                 'tags' => array(),
165                 'keyword' => 'Scenario Outline',
166                 'line' => $line,
167                 'steps' => array(),
168                 'examples' => array(),
169             ),
170             $hash
171         );
172
173         $steps = $this->loadStepsHash($hash['steps']);
174
175         if (isset($hash['examples']['keyword'])) {
176             $examplesKeyword = $hash['examples']['keyword'];
177             unset($hash['examples']['keyword']);
178         } else {
179             $examplesKeyword = 'Examples';
180         }
181
182         $examples = new ExampleTableNode($hash['examples'], $examplesKeyword);
183
184         return new OutlineNode($hash['title'], $hash['tags'], $steps, $examples, $hash['keyword'], $hash['line']);
185     }
186
187     /**
188      * Loads steps from provided hash.
189      *
190      * @param array $hash
191      *
192      * @return StepNode[]
193      */
194     private function loadStepsHash(array $hash)
195     {
196         $steps = array();
197         foreach ($hash as $stepIterator => $stepHash) {
198             $steps[] = $this->loadStepHash($stepHash, $stepIterator);
199         }
200
201         return $steps;
202     }
203
204     /**
205      * Loads step from provided hash.
206      *
207      * @param array   $hash Step hash
208      * @param integer $line Step definition line
209      *
210      * @return StepNode
211      */
212     protected function loadStepHash(array $hash, $line = 0)
213     {
214         $hash = array_merge(
215             array(
216                 'keyword_type' => 'Given',
217                 'type' => 'Given',
218                 'text' => null,
219                 'keyword' => 'Scenario',
220                 'line' => $line,
221                 'arguments' => array(),
222             ),
223             $hash
224         );
225
226         $arguments = array();
227         foreach ($hash['arguments'] as $argumentHash) {
228             if ('table' === $argumentHash['type']) {
229                 $arguments[] = $this->loadTableHash($argumentHash['rows']);
230             } elseif ('pystring' === $argumentHash['type']) {
231                 $arguments[] = $this->loadPyStringHash($argumentHash, $hash['line'] + 1);
232             }
233         }
234
235         return new StepNode($hash['type'], $hash['text'], $arguments, $hash['line'], $hash['keyword_type']);
236     }
237
238     /**
239      * Loads table from provided hash.
240      *
241      * @param array $hash Table hash
242      *
243      * @return TableNode
244      */
245     protected function loadTableHash(array $hash)
246     {
247         return new TableNode($hash);
248     }
249
250     /**
251      * Loads PyString from provided hash.
252      *
253      * @param array   $hash PyString hash
254      * @param integer $line
255      *
256      * @return PyStringNode
257      */
258     protected function loadPyStringHash(array $hash, $line = 0)
259     {
260         $line = isset($hash['line']) ? $hash['line'] : $line;
261
262         $strings = array();
263         foreach (explode("\n", $hash['text']) as $string) {
264             $strings[] = $string;
265         }
266
267         return new PyStringNode($strings, $line);
268     }
269 }