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 | 43 | 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 | 42 | 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 | 12 | protected function loadOutlineHash(array $hash, $line = 0) |
|
160 | { |
||
161 | 12 | $hash = array_merge( |
|
162 | array( |
||
163 | 12 | 'title' => null, |
|
164 | 12 | 'tags' => array(), |
|
165 | 12 | 'keyword' => 'Scenario Outline', |
|
166 | 12 | 'line' => $line, |
|
167 | 12 | 'steps' => array(), |
|
168 | 12 | 'examples' => array(), |
|
169 | 12 | ), |
|
170 | $hash |
||
171 | 12 | ); |
|
172 | |||
173 | 12 | $steps = $this->loadStepsHash($hash['steps']); |
|
174 | |||
175 | 12 | if (isset($hash['examples']['keyword'])) { |
|
176 | 1 | $examplesKeyword = $hash['examples']['keyword']; |
|
177 | 1 | unset($hash['examples']['keyword']); |
|
178 | 1 | } else { |
|
179 | 11 | $examplesKeyword = 'Examples'; |
|
180 | } |
||
181 | |||
182 | 12 | $exHash = $hash['examples']; |
|
183 | $examples = array(); |
||
184 | 12 | ||
185 | if ($this->examplesAreInArray($exHash)) { |
||
186 | $examples = $this->processExamplesArray($exHash, $examplesKeyword, $examples); |
||
187 | } else { |
||
188 | // examples as a single table - we create an array with the only one element |
||
189 | $examples[] = new ExampleTableNode($exHash, $examplesKeyword);; |
||
190 | } |
||
191 | |||
192 | return new OutlineNode($hash['title'], $hash['tags'], $steps, $examples, $hash['keyword'], $hash['line']); |
||
193 | } |
||
194 | 37 | ||
195 | /** |
||
196 | 37 | * Loads steps from provided hash. |
|
197 | 37 | * |
|
198 | 30 | * @param array $hash |
|
199 | 37 | * |
|
200 | * @return StepNode[] |
||
201 | 37 | */ |
|
202 | private function loadStepsHash(array $hash) |
||
203 | { |
||
204 | $steps = array(); |
||
205 | foreach ($hash as $stepIterator => $stepHash) { |
||
206 | $steps[] = $this->loadStepHash($stepHash, $stepIterator); |
||
207 | } |
||
208 | |||
209 | return $steps; |
||
210 | } |
||
211 | |||
212 | 30 | /** |
|
213 | * Loads step from provided hash. |
||
214 | 30 | * |
|
215 | * @param array $hash Step hash |
||
216 | 30 | * @param integer $line Step definition line |
|
217 | 30 | * |
|
218 | 30 | * @return StepNode |
|
219 | 30 | */ |
|
220 | 30 | protected function loadStepHash(array $hash, $line = 0) |
|
245 | 4 | ||
246 | /** |
||
247 | 4 | * Loads table from provided hash. |
|
248 | * |
||
249 | * @param array $hash Table hash |
||
250 | * |
||
251 | * @return TableNode |
||
252 | */ |
||
253 | protected function loadTableHash(array $hash) |
||
257 | |||
258 | 7 | /** |
|
259 | * Loads PyString from provided hash. |
||
260 | 7 | * |
|
261 | * @param array $hash PyString hash |
||
262 | 7 | * @param integer $line |
|
263 | 7 | * |
|
264 | 7 | * @return PyStringNode |
|
265 | 7 | */ |
|
266 | protected function loadPyStringHash(array $hash, $line = 0) |
||
277 | |||
278 | /** |
||
279 | * Checks if examples node is an array |
||
280 | * @param $exHash object hash |
||
281 | * @return bool |
||
282 | */ |
||
283 | 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 | 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: