* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Behat\Gherkin\Keywords; /** * Gherkin keywords dumper. * * @author Konstantin Kudryashov */ class KeywordsDumper { private $keywords; private $keywordsDumper; /** * Initializes dumper. * * @param KeywordsInterface $keywords Keywords instance */ public function __construct(KeywordsInterface $keywords) { $this->keywords = $keywords; $this->keywordsDumper = array($this, 'dumpKeywords'); } /** * Sets keywords mapper function. * * Callable should accept 2 arguments (array $keywords and Boolean $isShort) * * @param callable $mapper Mapper function */ public function setKeywordsDumperFunction($mapper) { $this->keywordsDumper = $mapper; } /** * Defaults keywords dumper. * * @param array $keywords Keywords list * @param Boolean $isShort Is short version * * @return string */ public function dumpKeywords(array $keywords, $isShort) { if ($isShort) { return 1 < count($keywords) ? '(' . implode('|', $keywords) . ')' : $keywords[0]; } return $keywords[0]; } /** * Dumps keyworded feature into string. * * @param string $language Keywords language * @param Boolean $short Dump short version * @param bool $excludeAsterisk * * @return string|array String for short version and array of features for extended */ public function dump($language, $short = true, $excludeAsterisk = false) { $this->keywords->setLanguage($language); $languageComment = ''; if ('en' !== $language) { $languageComment = "# language: $language\n"; } $keywords = explode('|', $this->keywords->getFeatureKeywords()); if ($short) { $keywords = call_user_func($this->keywordsDumper, $keywords, $short); return trim($languageComment . $this->dumpFeature($keywords, $short, $excludeAsterisk)); } $features = array(); foreach ($keywords as $keyword) { $keyword = call_user_func($this->keywordsDumper, array($keyword), $short); $features[] = trim($languageComment . $this->dumpFeature($keyword, $short, $excludeAsterisk)); } return $features; } /** * Dumps feature example. * * @param string $keyword Item keyword * @param Boolean $short Dump short version? * * @return string */ protected function dumpFeature($keyword, $short = true, $excludeAsterisk = false) { $dump = <<keywords->getBackgroundKeywords()); if ($short) { $keywords = call_user_func($this->keywordsDumper, $keywords, $short); $dump .= $this->dumpBackground($keywords, $short, $excludeAsterisk); } else { $keyword = call_user_func($this->keywordsDumper, array($keywords[0]), $short); $dump .= $this->dumpBackground($keyword, $short, $excludeAsterisk); } // Scenario $keywords = explode('|', $this->keywords->getScenarioKeywords()); if ($short) { $keywords = call_user_func($this->keywordsDumper, $keywords, $short); $dump .= $this->dumpScenario($keywords, $short, $excludeAsterisk); } else { foreach ($keywords as $keyword) { $keyword = call_user_func($this->keywordsDumper, array($keyword), $short); $dump .= $this->dumpScenario($keyword, $short, $excludeAsterisk); } } // Outline $keywords = explode('|', $this->keywords->getOutlineKeywords()); if ($short) { $keywords = call_user_func($this->keywordsDumper, $keywords, $short); $dump .= $this->dumpOutline($keywords, $short, $excludeAsterisk); } else { foreach ($keywords as $keyword) { $keyword = call_user_func($this->keywordsDumper, array($keyword), $short); $dump .= $this->dumpOutline($keyword, $short, $excludeAsterisk); } } return $dump; } /** * Dumps background example. * * @param string $keyword Item keyword * @param Boolean $short Dump short version? * * @return string */ protected function dumpBackground($keyword, $short = true, $excludeAsterisk = false) { $dump = <<dumpStep( $this->keywords->getGivenKeywords(), 'there is agent A', $short, $excludeAsterisk ); // And $dump .= $this->dumpStep( $this->keywords->getAndKeywords(), 'there is agent B', $short, $excludeAsterisk ); return $dump . "\n"; } /** * Dumps scenario example. * * @param string $keyword Item keyword * @param Boolean $short Dump short version? * * @return string */ protected function dumpScenario($keyword, $short = true, $excludeAsterisk = false) { $dump = <<dumpStep( $this->keywords->getGivenKeywords(), 'there is agent J', $short, $excludeAsterisk ); // And $dump .= $this->dumpStep( $this->keywords->getAndKeywords(), 'there is agent K', $short, $excludeAsterisk ); // When $dump .= $this->dumpStep( $this->keywords->getWhenKeywords(), 'I erase agent K\'s memory', $short, $excludeAsterisk ); // Then $dump .= $this->dumpStep( $this->keywords->getThenKeywords(), 'there should be agent J', $short, $excludeAsterisk ); // But $dump .= $this->dumpStep( $this->keywords->getButKeywords(), 'there should not be agent K', $short, $excludeAsterisk ); return $dump . "\n"; } /** * Dumps outline example. * * @param string $keyword Item keyword * @param Boolean $short Dump short version? * * @return string */ protected function dumpOutline($keyword, $short = true, $excludeAsterisk = false) { $dump = <<dumpStep( $this->keywords->getGivenKeywords(), 'there is agent ', $short, $excludeAsterisk ); // And $dump .= $this->dumpStep( $this->keywords->getAndKeywords(), 'there is agent ', $short, $excludeAsterisk ); // When $dump .= $this->dumpStep( $this->keywords->getWhenKeywords(), 'I erase agent \'s memory', $short, $excludeAsterisk ); // Then $dump .= $this->dumpStep( $this->keywords->getThenKeywords(), 'there should be agent ', $short, $excludeAsterisk ); // But $dump .= $this->dumpStep( $this->keywords->getButKeywords(), 'there should not be agent ', $short, $excludeAsterisk ); $keywords = explode('|', $this->keywords->getExamplesKeywords()); if ($short) { $keyword = call_user_func($this->keywordsDumper, $keywords, $short); } else { $keyword = call_user_func($this->keywordsDumper, array($keywords[0]), $short); } $dump .= <<keywordsDumper, $keywords, $short); $dump .= <<keywordsDumper, array($keyword), $short); $dump .= <<