Completed
Pull Request — master (#112)
by Christophe
02:50
created

ArrayLoader::supports()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 3
eloc 2
nc 3
nop 1
crap 3
1
<?php
2
3
/*
4
 * This file is part of the Behat Gherkin.
5
 * (c) Konstantin Kudryashov <[email protected]>
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 <[email protected]>
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 1
    public function supports($resource)
37
    {
38 1
        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 43
    public function load($resource)
49
    {
50 43
        $features = array();
51
52 43
        if (isset($resource['features'])) {
53 8
            foreach ($resource['features'] as $iterator => $hash) {
54 7
                $feature = $this->loadFeatureHash($hash, $iterator);
55 7
                $features[] = $feature;
56 8
            }
57 43
        } elseif (isset($resource['feature'])) {
58 35
            $feature = $this->loadFeatureHash($resource['feature']);
59 35
            $features[] = $feature;
60 35
        }
61
62 43
        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 42
    protected function loadFeatureHash(array $hash, $line = 0)
74
    {
75 42
        $hash = array_merge(
76
            array(
77 42
                'title' => null,
78 42
                'description' => null,
79 42
                'tags' => array(),
80 42
                'keyword' => 'Feature',
81 42
                'language' => 'en',
82 42
                'line' => $line,
83 42
                'scenarios' => array(),
84 42
            ),
85
            $hash
86 42
        );
87 42
        $background = isset($hash['background']) ? $this->loadBackgroundHash($hash['background']) : null;
88
89 42
        $scenarios = array();
90 42
        foreach ((array) $hash['scenarios'] as $scenarioIterator => $scenarioHash) {
91 35
            if (isset($scenarioHash['type']) && 'outline' === $scenarioHash['type']) {
92 12
                $scenarios[] = $this->loadOutlineHash($scenarioHash, $scenarioIterator);
93 12
            } else {
94 28
                $scenarios[] = $this->loadScenarioHash($scenarioHash, $scenarioIterator);
95
            }
96 42
        }
97
98 42
        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 8
    protected function loadBackgroundHash(array $hash)
109
    {
110 8
        $hash = array_merge(
111
            array(
112 8
                'title' => null,
113 8
                'keyword' => 'Background',
114 8
                'line' => 0,
115 8
                'steps' => array(),
116 8
            ),
117
            $hash
118 8
        );
119
120 8
        $steps = $this->loadStepsHash($hash['steps']);
121
122 8
        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 28
    protected function loadScenarioHash(array $hash, $line = 0)
134
    {
135 28
        $hash = array_merge(
136
            array(
137 28
                'title' => null,
138 28
                'tags' => array(),
139 28
                'keyword' => 'Scenario',
140 28
                'line' => $line,
141 28
                'steps' => array(),
142 28
            ),
143
            $hash
144 28
        );
145
146 28
        $steps = $this->loadStepsHash($hash['steps']);
147
148 28
        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 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
        $examples = new ExampleTableNode($hash['examples'], $examplesKeyword);
183
184 12
        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 37
    private function loadStepsHash(array $hash)
195
    {
196 37
        $steps = array();
197 37
        foreach ($hash as $stepIterator => $stepHash) {
198 30
            $steps[] = $this->loadStepHash($stepHash, $stepIterator);
199 37
        }
200
201 37
        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 30
    protected function loadStepHash(array $hash, $line = 0)
213
    {
214 30
        $hash = array_merge(
215
            array(
216 30
                'keyword_type' => 'Given',
217 30
                'type' => 'Given',
218 30
                'text' => null,
219 30
                'keyword' => 'Scenario',
220 30
                'line' => $line,
221 30
                'arguments' => array(),
222 30
            ),
223
            $hash
224 30
        );
225
226 30
        $arguments = array();
227 30
        foreach ($hash['arguments'] as $argumentHash) {
228 9
            if ('table' === $argumentHash['type']) {
229 4
                $arguments[] = $this->loadTableHash($argumentHash['rows']);
230 9
            } elseif ('pystring' === $argumentHash['type']) {
231 7
                $arguments[] = $this->loadPyStringHash($argumentHash, $hash['line'] + 1);
232 7
            }
233 30
        }
234
235 30
        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 4
    protected function loadTableHash(array $hash)
246
    {
247 4
        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 7
    protected function loadPyStringHash(array $hash, $line = 0)
259
    {
260 7
        $line = isset($hash['line']) ? $hash['line'] : $line;
261
262 7
        $strings = array();
263 7
        foreach (explode("\n", $hash['text']) as $string) {
264 7
            $strings[] = $string;
265 7
        }
266
267 7
        return new PyStringNode($strings, $line);
268
    }
269
}
270