4 * This file is part of the Behat Gherkin.
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
11 namespace Behat\Gherkin\Keywords;
14 * Gherkin keywords dumper.
16 * @author Konstantin Kudryashov <ever.zet@gmail.com>
21 private $keywordsDumper;
26 * @param KeywordsInterface $keywords Keywords instance
28 public function __construct(KeywordsInterface $keywords)
30 $this->keywords = $keywords;
31 $this->keywordsDumper = array($this, 'dumpKeywords');
35 * Sets keywords mapper function.
37 * Callable should accept 2 arguments (array $keywords and Boolean $isShort)
39 * @param callable $mapper Mapper function
41 public function setKeywordsDumperFunction($mapper)
43 $this->keywordsDumper = $mapper;
47 * Defaults keywords dumper.
49 * @param array $keywords Keywords list
50 * @param Boolean $isShort Is short version
54 public function dumpKeywords(array $keywords, $isShort)
57 return 1 < count($keywords) ? '(' . implode('|', $keywords) . ')' : $keywords[0];
64 * Dumps keyworded feature into string.
66 * @param string $language Keywords language
67 * @param Boolean $short Dump short version
68 * @param bool $excludeAsterisk
70 * @return string|array String for short version and array of features for extended
72 public function dump($language, $short = true, $excludeAsterisk = false)
74 $this->keywords->setLanguage($language);
75 $languageComment = '';
76 if ('en' !== $language) {
77 $languageComment = "# language: $language\n";
80 $keywords = explode('|', $this->keywords->getFeatureKeywords());
83 $keywords = call_user_func($this->keywordsDumper, $keywords, $short);
85 return trim($languageComment . $this->dumpFeature($keywords, $short, $excludeAsterisk));
89 foreach ($keywords as $keyword) {
90 $keyword = call_user_func($this->keywordsDumper, array($keyword), $short);
91 $features[] = trim($languageComment . $this->dumpFeature($keyword, $short, $excludeAsterisk));
98 * Dumps feature example.
100 * @param string $keyword Item keyword
101 * @param Boolean $short Dump short version?
105 protected function dumpFeature($keyword, $short = true, $excludeAsterisk = false)
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
117 $keywords = explode('|', $this->keywords->getBackgroundKeywords());
119 $keywords = call_user_func($this->keywordsDumper, $keywords, $short);
120 $dump .= $this->dumpBackground($keywords, $short, $excludeAsterisk);
122 $keyword = call_user_func($this->keywordsDumper, array($keywords[0]), $short);
123 $dump .= $this->dumpBackground($keyword, $short, $excludeAsterisk);
127 $keywords = explode('|', $this->keywords->getScenarioKeywords());
129 $keywords = call_user_func($this->keywordsDumper, $keywords, $short);
130 $dump .= $this->dumpScenario($keywords, $short, $excludeAsterisk);
132 foreach ($keywords as $keyword) {
133 $keyword = call_user_func($this->keywordsDumper, array($keyword), $short);
134 $dump .= $this->dumpScenario($keyword, $short, $excludeAsterisk);
139 $keywords = explode('|', $this->keywords->getOutlineKeywords());
141 $keywords = call_user_func($this->keywordsDumper, $keywords, $short);
142 $dump .= $this->dumpOutline($keywords, $short, $excludeAsterisk);
144 foreach ($keywords as $keyword) {
145 $keyword = call_user_func($this->keywordsDumper, array($keyword), $short);
146 $dump .= $this->dumpOutline($keyword, $short, $excludeAsterisk);
154 * Dumps background example.
156 * @param string $keyword Item keyword
157 * @param Boolean $short Dump short version?
161 protected function dumpBackground($keyword, $short = true, $excludeAsterisk = false)
169 $dump .= $this->dumpStep(
170 $this->keywords->getGivenKeywords(),
177 $dump .= $this->dumpStep(
178 $this->keywords->getAndKeywords(),
188 * Dumps scenario example.
190 * @param string $keyword Item keyword
191 * @param Boolean $short Dump short version?
195 protected function dumpScenario($keyword, $short = true, $excludeAsterisk = false)
198 {$keyword}: Erasing agent memory
203 $dump .= $this->dumpStep(
204 $this->keywords->getGivenKeywords(),
211 $dump .= $this->dumpStep(
212 $this->keywords->getAndKeywords(),
219 $dump .= $this->dumpStep(
220 $this->keywords->getWhenKeywords(),
221 'I erase agent K\'s memory',
227 $dump .= $this->dumpStep(
228 $this->keywords->getThenKeywords(),
229 'there should be agent J',
235 $dump .= $this->dumpStep(
236 $this->keywords->getButKeywords(),
237 'there should not be agent K',
246 * Dumps outline example.
248 * @param string $keyword Item keyword
249 * @param Boolean $short Dump short version?
253 protected function dumpOutline($keyword, $short = true, $excludeAsterisk = false)
256 {$keyword}: Erasing other agents' memory
261 $dump .= $this->dumpStep(
262 $this->keywords->getGivenKeywords(),
263 'there is agent <agent1>',
269 $dump .= $this->dumpStep(
270 $this->keywords->getAndKeywords(),
271 'there is agent <agent2>',
277 $dump .= $this->dumpStep(
278 $this->keywords->getWhenKeywords(),
279 'I erase agent <agent2>\'s memory',
285 $dump .= $this->dumpStep(
286 $this->keywords->getThenKeywords(),
287 'there should be agent <agent1>',
293 $dump .= $this->dumpStep(
294 $this->keywords->getButKeywords(),
295 'there should not be agent <agent2>',
300 $keywords = explode('|', $this->keywords->getExamplesKeywords());
302 $keyword = call_user_func($this->keywordsDumper, $keywords, $short);
304 $keyword = call_user_func($this->keywordsDumper, array($keywords[0]), $short);
319 * Dumps step example.
321 * @param string $keywords Item keyword
322 * @param string $text Step text
323 * @param Boolean $short Dump short version?
327 protected function dumpStep($keywords, $text, $short = true, $excludeAsterisk = false)
331 $keywords = explode('|', $keywords);
333 $keywords = array_map(
334 function ($keyword) {
335 return str_replace('<', '', $keyword);
339 $keywords = call_user_func($this->keywordsDumper, $keywords, $short);
345 foreach ($keywords as $keyword) {
346 if ($excludeAsterisk && '*' === $keyword) {
351 if (false !== mb_strpos($keyword, '<', 0, 'utf8')) {
352 $keyword = mb_substr($keyword, 0, -1, 'utf8');
355 $keyword = call_user_func($this->keywordsDumper, array($keyword), $short);
357 {$keyword}{$indent}{$text}