1 | <?php |
||
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 | 1 | public function supports($resource) |
|
40 | |||
41 | /** |
||
42 | * Loads features from provided resource. |
||
43 | * |
||
44 | * @param mixed $resource Resource to load |
||
45 | * |
||
46 | * @return FeatureNode[] |
||
47 | */ |
||
48 | 44 | public function load($resource) |
|
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 | 43 | protected function loadFeatureHash(array $hash, $line = 0) |
|
100 | |||
101 | /** |
||
102 | * Loads background from provided hash. |
||
103 | * |
||
104 | * @param array $hash Background hash |
||
105 | * |
||
106 | * @return BackgroundNode |
||
107 | */ |
||
108 | 8 | protected function loadBackgroundHash(array $hash) |
|
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 | 28 | protected function loadScenarioHash(array $hash, $line = 0) |
|
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 | 13 | protected function loadOutlineHash(array $hash, $line = 0) |
|
160 | { |
||
161 | 13 | $hash = array_merge( |
|
162 | array( |
||
163 | 13 | 'title' => null, |
|
164 | 13 | 'tags' => array(), |
|
165 | 13 | 'keyword' => 'Scenario Outline', |
|
166 | 13 | 'line' => $line, |
|
167 | 13 | 'steps' => array(), |
|
168 | 13 | 'examples' => array(), |
|
169 | 13 | ), |
|
170 | $hash |
||
171 | 13 | ); |
|
172 | |||
173 | 13 | $steps = $this->loadStepsHash($hash['steps']); |
|
174 | |||
175 | 13 | if (isset($hash['examples']['keyword'])) { |
|
176 | 1 | $examplesKeyword = $hash['examples']['keyword']; |
|
177 | 1 | unset($hash['examples']['keyword']); |
|
178 | 1 | } else { |
|
179 | 12 | $examplesKeyword = 'Examples'; |
|
180 | } |
||
181 | |||
182 | 13 | $exHash = $hash['examples']; |
|
183 | 13 | $examples = array(); |
|
184 | |||
185 | 13 | if ($this->examplesAreInArray($exHash)) { |
|
186 | 2 | $examples = $this->processExamplesArray($exHash, $examplesKeyword, $examples); |
|
187 | 2 | } else { |
|
188 | // examples as a single table - we create an array with the only one element |
||
189 | 12 | $examples[] = new ExampleTableNode($exHash, $examplesKeyword);; |
|
190 | } |
||
191 | |||
192 | 13 | return new OutlineNode($hash['title'], $hash['tags'], $steps, $examples, $hash['keyword'], $hash['line']); |
|
193 | } |
||
194 | |||
195 | /** |
||
196 | * Loads steps from provided hash. |
||
197 | * |
||
198 | * @param array $hash |
||
199 | * |
||
200 | * @return StepNode[] |
||
201 | */ |
||
202 | 38 | private function loadStepsHash(array $hash) |
|
203 | { |
||
204 | 38 | $steps = array(); |
|
205 | 38 | foreach ($hash as $stepIterator => $stepHash) { |
|
206 | 31 | $steps[] = $this->loadStepHash($stepHash, $stepIterator); |
|
207 | 38 | } |
|
208 | |||
209 | 38 | return $steps; |
|
210 | } |
||
211 | |||
212 | /** |
||
213 | * Loads step from provided hash. |
||
214 | * |
||
215 | * @param array $hash Step hash |
||
216 | * @param integer $line Step definition line |
||
217 | * |
||
218 | * @return StepNode |
||
219 | */ |
||
220 | 31 | protected function loadStepHash(array $hash, $line = 0) |
|
221 | { |
||
222 | 31 | $hash = array_merge( |
|
223 | array( |
||
224 | 31 | 'keyword_type' => 'Given', |
|
225 | 31 | 'type' => 'Given', |
|
226 | 31 | 'text' => null, |
|
227 | 31 | 'keyword' => 'Scenario', |
|
228 | 31 | 'line' => $line, |
|
229 | 31 | 'arguments' => array(), |
|
230 | 31 | ), |
|
231 | $hash |
||
232 | 31 | ); |
|
233 | |||
234 | 31 | $arguments = array(); |
|
235 | 31 | foreach ($hash['arguments'] as $argumentHash) { |
|
236 | 9 | if ('table' === $argumentHash['type']) { |
|
237 | 4 | $arguments[] = $this->loadTableHash($argumentHash['rows']); |
|
238 | 9 | } elseif ('pystring' === $argumentHash['type']) { |
|
239 | 7 | $arguments[] = $this->loadPyStringHash($argumentHash, $hash['line'] + 1); |
|
240 | 7 | } |
|
241 | 31 | } |
|
242 | |||
243 | 31 | return new StepNode($hash['type'], $hash['text'], $arguments, $hash['line'], $hash['keyword_type']); |
|
244 | } |
||
245 | |||
246 | /** |
||
247 | * Loads table from provided hash. |
||
248 | * |
||
249 | * @param array $hash Table hash |
||
250 | * |
||
251 | * @return TableNode |
||
252 | */ |
||
253 | 4 | protected function loadTableHash(array $hash) |
|
257 | |||
258 | /** |
||
259 | * Loads PyString from provided hash. |
||
260 | * |
||
261 | * @param array $hash PyString hash |
||
262 | * @param integer $line |
||
263 | * |
||
264 | * @return PyStringNode |
||
265 | */ |
||
266 | 7 | protected function loadPyStringHash(array $hash, $line = 0) |
|
267 | { |
||
268 | 7 | $line = isset($hash['line']) ? $hash['line'] : $line; |
|
269 | |||
270 | 7 | $strings = array(); |
|
271 | 7 | foreach (explode("\n", $hash['text']) as $string) { |
|
272 | 7 | $strings[] = $string; |
|
273 | 7 | } |
|
274 | |||
275 | 7 | return new PyStringNode($strings, $line); |
|
276 | } |
||
277 | |||
278 | /** |
||
279 | * Checks if examples node is an array |
||
280 | * @param $exHash object hash |
||
281 | * @return bool |
||
282 | */ |
||
283 | 13 | private function examplesAreInArray($exHash) |
|
287 | |||
288 | /** |
||
289 | * Processes cases when examples are in the form of array of arrays |
||
290 | * OR in the form of array of objects |
||
291 | * |
||
292 | * @param $exHash array hash |
||
293 | * @param $examplesKeyword string |
||
294 | * @param $examples array |
||
295 | * @return array |
||
296 | */ |
||
297 | 2 | private function processExamplesArray($exHash, $examplesKeyword, $examples) |
|
312 | } |
||
313 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: