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\Call\Filter;
13 use Behat\Behat\Definition\Call\DefinitionCall;
14 use Behat\Behat\Transformation\Exception\UnsupportedCallException;
15 use Behat\Behat\Transformation\Transformer\ArgumentTransformer;
16 use Behat\Testwork\Call\Call;
17 use Behat\Testwork\Call\Filter\CallFilter;
20 * Handles definition calls by intercepting them and transforming their arguments using transformations.
22 * @author Konstantin Kudryashov <ever.zet@gmail.com>
24 final class DefinitionArgumentsTransformer implements CallFilter
27 * @var ArgumentTransformer[]
29 private $argumentTransformers = array();
32 * Registers new argument transformer.
34 * @param ArgumentTransformer $transformer
36 public function registerArgumentTransformer(ArgumentTransformer $transformer)
38 $this->argumentTransformers[] = $transformer;
44 public function supportsCall(Call $call)
46 return $call instanceof DefinitionCall;
52 public function filterCall(Call $definitionCall)
54 if (!$definitionCall instanceof DefinitionCall) {
55 throw new UnsupportedCallException(sprintf(
56 'DefinitionArgumentTransformer can not filter `%s` call.',
57 get_class($definitionCall)
61 $newArguments = array();
63 foreach ($definitionCall->getArguments() as $index => $value) {
64 $newValue = $this->transformArgument($definitionCall, $index, $value);
66 if ($newValue !== $value) {
70 $newArguments[$index] = $newValue;
74 return $definitionCall;
77 return new DefinitionCall(
78 $definitionCall->getEnvironment(),
79 $definitionCall->getFeature(),
80 $definitionCall->getStep(),
81 $definitionCall->getCallee(),
83 $definitionCall->getErrorReportingLevel()
88 * Transforms call argument using registered transformers.
90 * @param DefinitionCall $definitionCall
91 * @param integer|string $index
96 private function transformArgument(DefinitionCall $definitionCall, $index, $value)
98 foreach ($this->argumentTransformers as $transformer) {
99 if (!$transformer->supportsDefinitionAndArgument($definitionCall, $index, $value)) {
103 return $transformer->transformArgument($definitionCall, $index, $value);