Completed
Push — master ( 26056a...749be1 )
by Christophe
03:21 queued 10s
created

ArrayKeywords::setLanguage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
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\Keywords;
12
13
/**
14
 * Array initializable keywords holder.
15
 *
16
 * $keywords = new Behat\Gherkin\Keywords\ArrayKeywords(array(
17
 *     'en' => array(
18
 *         'feature'          => 'Feature',
19
 *         'background'       => 'Background',
20
 *         'scenario'         => 'Scenario',
21
 *         'scenario_outline' => 'Scenario Outline|Scenario Template',
22
 *         'examples'         => 'Examples|Scenarios',
23
 *         'given'            => 'Given',
24
 *         'when'             => 'When',
25
 *         'then'             => 'Then',
26
 *         'and'              => 'And',
27
 *         'but'              => 'But'
28
 *     ),
29
 *     'ru' => array(
30
 *         'feature'          => 'Функционал',
31
 *         'background'       => 'Предыстория',
32
 *         'scenario'         => 'Сценарий',
33
 *         'scenario_outline' => 'Структура сценария',
34
 *         'examples'         => 'Примеры',
35
 *         'given'            => 'Допустим',
36
 *         'when'             => 'Если',
37
 *         'then'             => 'То',
38
 *         'and'              => 'И',
39
 *         'but'              => 'Но'
40
 *     )
41
 * ));
42
 *
43
 * @author Konstantin Kudryashov <[email protected]>
44
 */
45
class ArrayKeywords implements KeywordsInterface
46
{
47
    private $keywords = array();
48
    private $keywordString = array();
49
    private $language;
50
51
    /**
52
     * Initializes holder with keywords.
53
     *
54
     * @param array $keywords Keywords array
55
     */
56 247
    public function __construct(array $keywords)
57
    {
58 247
        $this->keywords = $keywords;
59 247
    }
60
61
    /**
62
     * Sets keywords holder language.
63
     *
64
     * @param string $language Language name
65
     */
66 245
    public function setLanguage($language)
67
    {
68 245
        if (!isset($this->keywords[$language])) {
69 3
            $this->language = 'en';
70
        } else {
71 245
            $this->language = $language;
72
        }
73 245
    }
74
75
    /**
76
     * Returns Feature keywords (splitted by "|").
77
     *
78
     * @return string
79
     */
80 245
    public function getFeatureKeywords()
81
    {
82 245
        return $this->keywords[$this->language]['feature'];
83
    }
84
85
    /**
86
     * Returns Background keywords (splitted by "|").
87
     *
88
     * @return string
89
     */
90 245
    public function getBackgroundKeywords()
91
    {
92 245
        return $this->keywords[$this->language]['background'];
93
    }
94
95
    /**
96
     * Returns Scenario keywords (splitted by "|").
97
     *
98
     * @return string
99
     */
100 245
    public function getScenarioKeywords()
101
    {
102 245
        return $this->keywords[$this->language]['scenario'];
103
    }
104
105
    /**
106
     * Returns Scenario Outline keywords (splitted by "|").
107
     *
108
     * @return string
109
     */
110 245
    public function getOutlineKeywords()
111
    {
112 245
        return $this->keywords[$this->language]['scenario_outline'];
113
    }
114
115
    /**
116
     * Returns Examples keywords (splitted by "|").
117
     *
118
     * @return string
119
     */
120 245
    public function getExamplesKeywords()
121
    {
122 245
        return $this->keywords[$this->language]['examples'];
123
    }
124
125
    /**
126
     * Returns Given keywords (splitted by "|").
127
     *
128
     * @return string
129
     */
130 236
    public function getGivenKeywords()
131
    {
132 236
        return $this->keywords[$this->language]['given'];
133
    }
134
135
    /**
136
     * Returns When keywords (splitted by "|").
137
     *
138
     * @return string
139
     */
140 236
    public function getWhenKeywords()
141
    {
142 236
        return $this->keywords[$this->language]['when'];
143
    }
144
145
    /**
146
     * Returns Then keywords (splitted by "|").
147
     *
148
     * @return string
149
     */
150 236
    public function getThenKeywords()
151
    {
152 236
        return $this->keywords[$this->language]['then'];
153
    }
154
155
    /**
156
     * Returns And keywords (splitted by "|").
157
     *
158
     * @return string
159
     */
160 236
    public function getAndKeywords()
161
    {
162 236
        return $this->keywords[$this->language]['and'];
163
    }
164
165
    /**
166
     * Returns But keywords (splitted by "|").
167
     *
168
     * @return string
169
     */
170 236
    public function getButKeywords()
171
    {
172 236
        return $this->keywords[$this->language]['but'];
173
    }
174
175
    /**
176
     * Returns all step keywords (Given, When, Then, And, But).
177
     *
178
     * @return string
179
     */
180 232
    public function getStepKeywords()
181
    {
182 232
        if (!isset($this->keywordString[$this->language])) {
183 232
            $keywords = array_merge(
184 232
                explode('|', $this->getGivenKeywords()),
185 232
                explode('|', $this->getWhenKeywords()),
186 232
                explode('|', $this->getThenKeywords()),
187 232
                explode('|', $this->getAndKeywords()),
188 232
                explode('|', $this->getButKeywords())
189
            );
190
191 232
            usort($keywords, function ($keyword1, $keyword2) {
192 232
                return mb_strlen($keyword2, 'utf8') - mb_strlen($keyword1, 'utf8');
193 232
            });
194
195
            $this->keywordString[$this->language] = implode('|', $keywords);
196
        }
197
198
        return $this->keywordString[$this->language];
199
    }
200
}
201