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\Transformer;
13 use Behat\Behat\Definition\Call\DefinitionCall;
14 use Behat\Behat\Definition\Pattern\PatternTransformer;
15 use Behat\Behat\Transformation\SimpleArgumentTransformation;
16 use Behat\Behat\Transformation\Transformation\PatternTransformation;
17 use Behat\Behat\Transformation\RegexGenerator;
18 use Behat\Behat\Transformation\Transformation;
19 use Behat\Behat\Transformation\TransformationRepository;
20 use Behat\Gherkin\Node\ArgumentInterface;
21 use Behat\Testwork\Call\CallCenter;
22 use Symfony\Component\Translation\TranslatorInterface;
25 * Argument transformer based on transformations repository.
27 * @author Konstantin Kudryashov <ever.zet@gmail.com>
29 final class RepositoryArgumentTransformer implements ArgumentTransformer, RegexGenerator
32 * @var TransformationRepository
40 * @var PatternTransformer
42 private $patternTransformer;
44 * @var TranslatorInterface
49 * Initializes transformer.
51 * @param TransformationRepository $repository
52 * @param CallCenter $callCenter
53 * @param PatternTransformer $patternTransformer
54 * @param TranslatorInterface $translator
56 public function __construct(
57 TransformationRepository $repository,
58 CallCenter $callCenter,
59 PatternTransformer $patternTransformer,
60 TranslatorInterface $translator
62 $this->repository = $repository;
63 $this->callCenter = $callCenter;
64 $this->patternTransformer = $patternTransformer;
65 $this->translator = $translator;
71 public function supportsDefinitionAndArgument(DefinitionCall $definitionCall, $argumentIndex, $argumentValue)
73 return count($this->repository->getEnvironmentTransformations($definitionCall->getEnvironment())) > 0;
79 public function transformArgument(DefinitionCall $definitionCall, $argumentIndex, $argumentValue)
81 $environment = $definitionCall->getEnvironment();
82 list($simpleTransformations, $normalTransformations) = $this->splitSimpleAndNormalTransformations(
83 $this->repository->getEnvironmentTransformations($environment)
86 $newValue = $this->applySimpleTransformations($simpleTransformations, $definitionCall, $argumentIndex, $argumentValue);
87 $newValue = $this->applyNormalTransformations($normalTransformations, $definitionCall, $argumentIndex, $newValue);
95 public function generateRegex($suiteName, $pattern, $language)
97 $translatedPattern = $this->translator->trans($pattern, array(), $suiteName, $language);
98 if ($pattern == $translatedPattern) {
99 return $this->patternTransformer->transformPatternToRegex($pattern);
102 return $this->patternTransformer->transformPatternToRegex($translatedPattern);
106 * Apply simple argument transformations in priority order.
108 * @param SimpleArgumentTransformation[] $transformations
109 * @param DefinitionCall $definitionCall
110 * @param integer|string $index
111 * @param mixed $value
115 private function applySimpleTransformations(array $transformations, DefinitionCall $definitionCall, $index, $value)
117 usort($transformations, function (SimpleArgumentTransformation $t1, SimpleArgumentTransformation $t2) {
118 if ($t1->getPriority() == $t2->getPriority()) {
122 return ($t1->getPriority() > $t2->getPriority()) ? -1 : 1;
126 foreach ($transformations as $transformation) {
127 $newValue = $this->transform($definitionCall, $transformation, $index, $newValue);
134 * Apply normal (non-simple) argument transformations.
136 * @param Transformation[] $transformations
137 * @param DefinitionCall $definitionCall
138 * @param integer|string $index
139 * @param mixed $value
143 private function applyNormalTransformations(array $transformations, DefinitionCall $definitionCall, $index, $value)
146 foreach ($transformations as $transformation) {
147 $newValue = $this->transform($definitionCall, $transformation, $index, $newValue);
154 * Transforms argument value using registered transformers.
156 * @param Transformation $transformation
157 * @param DefinitionCall $definitionCall
158 * @param integer|string $index
159 * @param mixed $value
163 private function transform(DefinitionCall $definitionCall, Transformation $transformation, $index, $value)
165 if (is_object($value) && !$value instanceof ArgumentInterface) {
169 if ($transformation instanceof SimpleArgumentTransformation &&
170 $transformation->supportsDefinitionAndArgument($definitionCall, $index, $value)) {
171 return $transformation->transformArgument($this->callCenter, $definitionCall, $index, $value);
174 if ($transformation instanceof PatternTransformation &&
175 $transformation->supportsDefinitionAndArgument($this, $definitionCall, $value)) {
176 return $transformation->transformArgument($this, $this->callCenter, $definitionCall, $value);
183 * Splits transformations into simple and normal ones.
185 * @param Transformation[] $transformations
189 private function splitSimpleAndNormalTransformations(array $transformations)
191 return array_reduce($transformations, function ($acc, $t) {
193 $t instanceof SimpleArgumentTransformation ? array_merge($acc[0], array($t)) : $acc[0],
194 !$t instanceof SimpleArgumentTransformation ? array_merge($acc[1], array($t)) : $acc[1],
196 }, array(array(), array()));