Security update for permissions_by_term
[yaffs-website] / vendor / behat / gherkin / src / Behat / Gherkin / Keywords / KeywordsDumper.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\Keywords;
12
13 /**
14  * Gherkin keywords dumper.
15  *
16  * @author Konstantin Kudryashov <ever.zet@gmail.com>
17  */
18 class KeywordsDumper
19 {
20     private $keywords;
21     private $keywordsDumper;
22
23     /**
24      * Initializes dumper.
25      *
26      * @param KeywordsInterface $keywords Keywords instance
27      */
28     public function __construct(KeywordsInterface $keywords)
29     {
30         $this->keywords = $keywords;
31         $this->keywordsDumper = array($this, 'dumpKeywords');
32     }
33
34     /**
35      * Sets keywords mapper function.
36      *
37      * Callable should accept 2 arguments (array $keywords and Boolean $isShort)
38      *
39      * @param callable $mapper Mapper function
40      */
41     public function setKeywordsDumperFunction($mapper)
42     {
43         $this->keywordsDumper = $mapper;
44     }
45
46     /**
47      * Defaults keywords dumper.
48      *
49      * @param array   $keywords Keywords list
50      * @param Boolean $isShort  Is short version
51      *
52      * @return string
53      */
54     public function dumpKeywords(array $keywords, $isShort)
55     {
56         if ($isShort) {
57             return 1 < count($keywords) ? '(' . implode('|', $keywords) . ')' : $keywords[0];
58         }
59
60         return $keywords[0];
61     }
62
63     /**
64      * Dumps keyworded feature into string.
65      *
66      * @param string  $language Keywords language
67      * @param Boolean $short    Dump short version
68      * @param bool    $excludeAsterisk
69      *
70      * @return string|array String for short version and array of features for extended
71      */
72     public function dump($language, $short = true, $excludeAsterisk = false)
73     {
74         $this->keywords->setLanguage($language);
75         $languageComment = '';
76         if ('en' !== $language) {
77             $languageComment = "# language: $language\n";
78         }
79
80         $keywords = explode('|', $this->keywords->getFeatureKeywords());
81
82         if ($short) {
83             $keywords = call_user_func($this->keywordsDumper, $keywords, $short);
84
85             return trim($languageComment . $this->dumpFeature($keywords, $short, $excludeAsterisk));
86         }
87
88         $features = array();
89         foreach ($keywords as $keyword) {
90             $keyword = call_user_func($this->keywordsDumper, array($keyword), $short);
91             $features[] = trim($languageComment . $this->dumpFeature($keyword, $short, $excludeAsterisk));
92         }
93
94         return $features;
95     }
96
97     /**
98      * Dumps feature example.
99      *
100      * @param string  $keyword Item keyword
101      * @param Boolean $short   Dump short version?
102      *
103      * @return string
104      */
105     protected function dumpFeature($keyword, $short = true, $excludeAsterisk = false)
106     {
107         $dump = <<<GHERKIN
108 {$keyword}: Internal operations
109   In order to stay secret
110   As a secret organization
111   We need to be able to erase past agents' memory
112
113
114 GHERKIN;
115
116         // Background
117         $keywords = explode('|', $this->keywords->getBackgroundKeywords());
118         if ($short) {
119             $keywords = call_user_func($this->keywordsDumper, $keywords, $short);
120             $dump .= $this->dumpBackground($keywords, $short, $excludeAsterisk);
121         } else {
122             $keyword = call_user_func($this->keywordsDumper, array($keywords[0]), $short);
123             $dump .= $this->dumpBackground($keyword, $short, $excludeAsterisk);
124         }
125
126         // Scenario
127         $keywords = explode('|', $this->keywords->getScenarioKeywords());
128         if ($short) {
129             $keywords = call_user_func($this->keywordsDumper, $keywords, $short);
130             $dump .= $this->dumpScenario($keywords, $short, $excludeAsterisk);
131         } else {
132             foreach ($keywords as $keyword) {
133                 $keyword = call_user_func($this->keywordsDumper, array($keyword), $short);
134                 $dump .= $this->dumpScenario($keyword, $short, $excludeAsterisk);
135             }
136         }
137
138         // Outline
139         $keywords = explode('|', $this->keywords->getOutlineKeywords());
140         if ($short) {
141             $keywords = call_user_func($this->keywordsDumper, $keywords, $short);
142             $dump .= $this->dumpOutline($keywords, $short, $excludeAsterisk);
143         } else {
144             foreach ($keywords as $keyword) {
145                 $keyword = call_user_func($this->keywordsDumper, array($keyword), $short);
146                 $dump .= $this->dumpOutline($keyword, $short, $excludeAsterisk);
147             }
148         }
149
150         return $dump;
151     }
152
153     /**
154      * Dumps background example.
155      *
156      * @param string  $keyword Item keyword
157      * @param Boolean $short   Dump short version?
158      *
159      * @return string
160      */
161     protected function dumpBackground($keyword, $short = true, $excludeAsterisk = false)
162     {
163         $dump = <<<GHERKIN
164   {$keyword}:
165
166 GHERKIN;
167
168         // Given
169         $dump .= $this->dumpStep(
170             $this->keywords->getGivenKeywords(),
171             'there is agent A',
172             $short,
173             $excludeAsterisk
174         );
175
176         // And
177         $dump .= $this->dumpStep(
178             $this->keywords->getAndKeywords(),
179             'there is agent B',
180             $short,
181             $excludeAsterisk
182         );
183
184         return $dump . "\n";
185     }
186
187     /**
188      * Dumps scenario example.
189      *
190      * @param string  $keyword Item keyword
191      * @param Boolean $short   Dump short version?
192      *
193      * @return string
194      */
195     protected function dumpScenario($keyword, $short = true, $excludeAsterisk = false)
196     {
197         $dump = <<<GHERKIN
198   {$keyword}: Erasing agent memory
199
200 GHERKIN;
201
202         // Given
203         $dump .= $this->dumpStep(
204             $this->keywords->getGivenKeywords(),
205             'there is agent J',
206             $short,
207             $excludeAsterisk
208         );
209
210         // And
211         $dump .= $this->dumpStep(
212             $this->keywords->getAndKeywords(),
213             'there is agent K',
214             $short,
215             $excludeAsterisk
216         );
217
218         // When
219         $dump .= $this->dumpStep(
220             $this->keywords->getWhenKeywords(),
221             'I erase agent K\'s memory',
222             $short,
223             $excludeAsterisk
224         );
225
226         // Then
227         $dump .= $this->dumpStep(
228             $this->keywords->getThenKeywords(),
229             'there should be agent J',
230             $short,
231             $excludeAsterisk
232         );
233
234         // But
235         $dump .= $this->dumpStep(
236             $this->keywords->getButKeywords(),
237             'there should not be agent K',
238             $short,
239             $excludeAsterisk
240         );
241
242         return $dump . "\n";
243     }
244
245     /**
246      * Dumps outline example.
247      *
248      * @param string  $keyword Item keyword
249      * @param Boolean $short   Dump short version?
250      *
251      * @return string
252      */
253     protected function dumpOutline($keyword, $short = true, $excludeAsterisk = false)
254     {
255         $dump = <<<GHERKIN
256   {$keyword}: Erasing other agents' memory
257
258 GHERKIN;
259
260         // Given
261         $dump .= $this->dumpStep(
262             $this->keywords->getGivenKeywords(),
263             'there is agent <agent1>',
264             $short,
265             $excludeAsterisk
266         );
267
268         // And
269         $dump .= $this->dumpStep(
270             $this->keywords->getAndKeywords(),
271             'there is agent <agent2>',
272             $short,
273             $excludeAsterisk
274         );
275
276         // When
277         $dump .= $this->dumpStep(
278             $this->keywords->getWhenKeywords(),
279             'I erase agent <agent2>\'s memory',
280             $short,
281             $excludeAsterisk
282         );
283
284         // Then
285         $dump .= $this->dumpStep(
286             $this->keywords->getThenKeywords(),
287             'there should be agent <agent1>',
288             $short,
289             $excludeAsterisk
290         );
291
292         // But
293         $dump .= $this->dumpStep(
294             $this->keywords->getButKeywords(),
295             'there should not be agent <agent2>',
296             $short,
297             $excludeAsterisk
298         );
299
300         $keywords = explode('|', $this->keywords->getExamplesKeywords());
301         if ($short) {
302             $keyword = call_user_func($this->keywordsDumper, $keywords, $short);
303         } else {
304             $keyword = call_user_func($this->keywordsDumper, array($keywords[0]), $short);
305         }
306
307         $dump .= <<<GHERKIN
308
309     {$keyword}:
310       | agent1 | agent2 |
311       | D      | M      |
312
313 GHERKIN;
314
315         return $dump . "\n";
316     }
317
318     /**
319      * Dumps step example.
320      *
321      * @param string  $keywords Item keyword
322      * @param string  $text     Step text
323      * @param Boolean $short    Dump short version?
324      *
325      * @return string
326      */
327     protected function dumpStep($keywords, $text, $short = true, $excludeAsterisk = false)
328     {
329         $dump = '';
330
331         $keywords = explode('|', $keywords);
332         if ($short) {
333             $keywords = array_map(
334                 function ($keyword) {
335                     return str_replace('<', '', $keyword);
336                 },
337                 $keywords
338             );
339             $keywords = call_user_func($this->keywordsDumper, $keywords, $short);
340             $dump .= <<<GHERKIN
341     {$keywords} {$text}
342
343 GHERKIN;
344         } else {
345             foreach ($keywords as $keyword) {
346                 if ($excludeAsterisk && '*' === $keyword) {
347                     continue;
348                 }
349
350                 $indent = ' ';
351                 if (false !== mb_strpos($keyword, '<', 0, 'utf8')) {
352                     $keyword = mb_substr($keyword, 0, -1, 'utf8');
353                     $indent = '';
354                 }
355                 $keyword = call_user_func($this->keywordsDumper, array($keyword), $short);
356                 $dump .= <<<GHERKIN
357     {$keyword}{$indent}{$text}
358
359 GHERKIN;
360             }
361         }
362
363         return $dump;
364     }
365 }