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\Transformation\Transformation;
13 use Behat\Behat\Definition\Call\DefinitionCall;
14 use Behat\Behat\Transformation\Call\TransformationCall;
15 use Behat\Behat\Transformation\RegexGenerator;
16 use Behat\Behat\Transformation\Transformation;
17 use Behat\Testwork\Call\CallCenter;
18 use Behat\Testwork\Call\RuntimeCallee;
22 * Pattern-based transformation.
24 * @author Konstantin Kudryashov <ever.zet@gmail.com>
26 final class PatternTransformation extends RuntimeCallee implements Transformation
34 * Initializes transformation.
36 * @param string $pattern
37 * @param callable $callable
38 * @param null|string $description
40 public function __construct($pattern, $callable, $description = null)
42 $this->pattern = $pattern;
44 parent::__construct($callable, $description);
48 * Checks if transformer supports argument.
50 * @param RegexGenerator $regexGenerator
51 * @param DefinitionCall $definitionCall
52 * @param mixed $argumentValue
56 public function supportsDefinitionAndArgument(
57 RegexGenerator $regexGenerator,
58 DefinitionCall $definitionCall,
61 $regex = $regexGenerator->generateRegex(
62 $definitionCall->getEnvironment()->getSuite()->getName(),
64 $definitionCall->getFeature()->getLanguage()
67 return $this->match($regex, $argumentValue, $match);
71 * Transforms argument value using transformation and returns a new one.
73 * @param RegexGenerator $regexGenerator
74 * @param CallCenter $callCenter
75 * @param DefinitionCall $definitionCall
76 * @param mixed $argumentValue
80 * @throws Exception If transformation throws exception
82 public function transformArgument(
83 RegexGenerator $regexGenerator,
84 CallCenter $callCenter,
85 DefinitionCall $definitionCall,
88 $regex = $regexGenerator->generateRegex(
89 $definitionCall->getEnvironment()->getSuite()->getName(),
91 $definitionCall->getFeature()->getLanguage()
94 $this->match($regex, $argumentValue, $arguments);
96 $call = new TransformationCall(
97 $definitionCall->getEnvironment(),
98 $definitionCall->getCallee(),
103 $result = $callCenter->makeCall($call);
105 if ($result->hasException()) {
106 throw $result->getException();
109 return $result->getReturn();
115 public function getPattern()
117 return $this->pattern;
123 public function __toString()
125 return 'PatternTransform ' . $this->pattern;
129 * @param $regexPattern
130 * @param $argumentValue
135 private function match($regexPattern, $argumentValue, &$match)
137 if (is_string($argumentValue) && preg_match($regexPattern, $argumentValue, $match)) {
138 // take arguments from capture groups if there are some
139 if (count($match) > 1) {
140 $match = array_slice($match, 1);