4 * This file is part of the Behat.
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\Behat\Definition\Pattern\Policy;
13 use Behat\Behat\Definition\Pattern\Pattern;
14 use Behat\Behat\Definition\Exception\InvalidPatternException;
15 use Behat\Transliterator\Transliterator;
18 * Defines a way to handle turnip patterns.
20 * @author Konstantin Kudryashov <ever.zet@gmail.com>
22 final class TurnipPatternPolicy implements PatternPolicy
24 const TOKEN_REGEX = "[\"']?(?P<%s>(?<=\")[^\"]*(?=\")|(?<=')[^']*(?=')|\-?[\w\.\,]+)['\"]?";
26 const PLACEHOLDER_REGEXP = "/\\\:(\w+)/";
27 const OPTIONAL_WORD_REGEXP = '/(\s)?\\\\\(([^\\\]+)\\\\\)(\s)?/';
28 const ALTERNATIVE_WORD_REGEXP = '/(\w+)\\\\\/(\w+)/';
33 private $regexCache = array();
38 private static $placeholderPatterns = array(
39 "/(?<!\w)\"[^\"]+\"(?!\w)/",
40 "/(?<!\w)'[^']+'(?!\w)/",
41 "/(?<!\w|\.|\,)\-?\d+(?:[\.\,]\d+)?(?!\w|\.|\,)/"
47 public function supportsPatternType($type)
49 return null === $type || 'turnip' === $type;
55 public function generatePattern($stepText)
59 foreach (self::$placeholderPatterns as $replacePattern) {
60 $pattern = preg_replace_callback(
62 function () use (&$count) { return ':arg' . ++$count; },
66 $pattern = $this->escapeAlternationSyntax($pattern);
67 $canonicalText = $this->generateCanonicalText($stepText);
69 return new Pattern($canonicalText, $pattern, $count);
75 public function supportsPattern($pattern)
83 public function transformPatternToRegex($pattern)
85 if (!isset($this->regexCache[$pattern])) {
86 $this->regexCache[$pattern] = $this->createTransformedRegex($pattern);
88 return $this->regexCache[$pattern];
92 * @param string $pattern
95 private function createTransformedRegex($pattern)
97 $regex = preg_quote($pattern, '/');
99 $regex = $this->replaceTokensWithRegexCaptureGroups($regex);
100 $regex = $this->replaceTurnipOptionalEndingWithRegex($regex);
101 $regex = $this->replaceTurnipAlternativeWordsWithRegex($regex);
103 return '/^' . $regex . '$/i';
107 * Generates canonical text for step text.
109 * @param string $stepText
113 private function generateCanonicalText($stepText)
115 $canonicalText = preg_replace(self::$placeholderPatterns, '', $stepText);
116 $canonicalText = Transliterator::transliterate($canonicalText, ' ');
117 $canonicalText = preg_replace('/[^a-zA-Z\_\ ]/', '', $canonicalText);
118 $canonicalText = str_replace(' ', '', ucwords($canonicalText));
120 return $canonicalText;
124 * Replaces turnip tokens with regex capture groups.
126 * @param string $regex
130 private function replaceTokensWithRegexCaptureGroups($regex)
132 $tokenRegex = self::TOKEN_REGEX;
134 return preg_replace_callback(
135 self::PLACEHOLDER_REGEXP,
136 array($this, 'replaceTokenWithRegexCaptureGroup'),
141 private function replaceTokenWithRegexCaptureGroup($tokenMatch)
143 if (strlen($tokenMatch[1]) >= 32) {
144 throw new InvalidPatternException(
145 "Token name should not exceed 32 characters, but `{$tokenMatch[1]}` was used."
149 return sprintf(self::TOKEN_REGEX, $tokenMatch[1]);
153 * Replaces turnip optional ending with regex non-capturing optional group.
155 * @param string $regex
159 private function replaceTurnipOptionalEndingWithRegex($regex)
161 return preg_replace(self::OPTIONAL_WORD_REGEXP, '(?:\1)?(?:\2)?(?:\3)?', $regex);
165 * Replaces turnip alternative words with regex non-capturing alternating group.
167 * @param string $regex
171 private function replaceTurnipAlternativeWordsWithRegex($regex)
173 $regex = preg_replace(self::ALTERNATIVE_WORD_REGEXP, '(?:\1|\2)', $regex);
174 $regex = $this->removeEscapingOfAlternationSyntax($regex);
180 * Adds escaping to alternation syntax in pattern.
182 * By default, Turnip treats `/` as alternation syntax. Meaning `one/two` for Turnip
183 * means either `one` or `two`. Sometimes though you'll want to use slash character
184 * with different purpose (URL, UNIX paths). In this case, you would escape slashes
187 * This method adds escaping to all slashes in generated snippets.
189 * @param string $pattern
193 private function escapeAlternationSyntax($pattern)
195 return str_replace('/', '\/', $pattern);
199 * Removes escaping of alternation syntax from regex.
201 * This method removes those escaping backslashes from your slashes, so your steps
202 * could be matched against your escaped definitions.
204 * @param string $regex
208 private function removeEscapingOfAlternationSyntax($regex)
210 return str_replace('\\\/', '/', $regex);