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;
13 use Behat\Behat\Definition\Exception\UnknownPatternException;
14 use Behat\Behat\Definition\Exception\UnsupportedPatternTypeException;
15 use Behat\Behat\Definition\Pattern\Policy\PatternPolicy;
18 * Transforms patterns using registered policies.
20 * @author Konstantin Kudryashov <ever.zet@gmail.com>
22 final class PatternTransformer
25 * @var PatternPolicy[]
27 private $policies = array();
30 * Registers pattern policy.
32 * @param PatternPolicy $policy
34 public function registerPatternPolicy(PatternPolicy $policy)
36 $this->policies[] = $policy;
43 * @param string $stepText
47 * @throws UnsupportedPatternTypeException
49 public function generatePattern($type, $stepText)
51 foreach ($this->policies as $policy) {
52 if ($policy->supportsPatternType($type)) {
53 return $policy->generatePattern($stepText);
57 throw new UnsupportedPatternTypeException(sprintf('Can not find policy for a pattern type `%s`.', $type), $type);
61 * Transforms pattern string to regex.
63 * @param string $pattern
67 * @throws UnknownPatternException
69 public function transformPatternToRegex($pattern)
71 foreach ($this->policies as $policy) {
72 if ($policy->supportsPattern($pattern)) {
73 return $policy->transformPatternToRegex($pattern);
77 throw new UnknownPatternException(sprintf('Can not find policy for a pattern `%s`.', $pattern), $pattern);