Security update for permissions_by_term
[yaffs-website] / vendor / behat / behat / src / Behat / Behat / Transformation / Transformer / RepositoryArgumentTransformer.php
1 <?php
2
3 /*
4  * This file is part of the Behat.
5  * (c) Konstantin Kudryashov <ever.zet@gmail.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 namespace Behat\Behat\Transformation\Transformer;
12
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;
23
24 /**
25  * Argument transformer based on transformations repository.
26  *
27  * @author Konstantin Kudryashov <ever.zet@gmail.com>
28  */
29 final class RepositoryArgumentTransformer implements ArgumentTransformer, RegexGenerator
30 {
31     /**
32      * @var TransformationRepository
33      */
34     private $repository;
35     /**
36      * @var CallCenter
37      */
38     private $callCenter;
39     /**
40      * @var PatternTransformer
41      */
42     private $patternTransformer;
43     /**
44      * @var TranslatorInterface
45      */
46     private $translator;
47
48     /**
49      * Initializes transformer.
50      *
51      * @param TransformationRepository $repository
52      * @param CallCenter               $callCenter
53      * @param PatternTransformer       $patternTransformer
54      * @param TranslatorInterface      $translator
55      */
56     public function __construct(
57         TransformationRepository $repository,
58         CallCenter $callCenter,
59         PatternTransformer $patternTransformer,
60         TranslatorInterface $translator
61     ) {
62         $this->repository = $repository;
63         $this->callCenter = $callCenter;
64         $this->patternTransformer = $patternTransformer;
65         $this->translator = $translator;
66     }
67
68     /**
69      * {@inheritdoc}
70      */
71     public function supportsDefinitionAndArgument(DefinitionCall $definitionCall, $argumentIndex, $argumentValue)
72     {
73         return count($this->repository->getEnvironmentTransformations($definitionCall->getEnvironment())) > 0;
74     }
75
76     /**
77      * {@inheritdoc}
78      */
79     public function transformArgument(DefinitionCall $definitionCall, $argumentIndex, $argumentValue)
80     {
81         $environment = $definitionCall->getEnvironment();
82         list($simpleTransformations, $normalTransformations) = $this->splitSimpleAndNormalTransformations(
83             $this->repository->getEnvironmentTransformations($environment)
84         );
85
86         $newValue = $this->applySimpleTransformations($simpleTransformations, $definitionCall, $argumentIndex, $argumentValue);
87         $newValue = $this->applyNormalTransformations($normalTransformations, $definitionCall, $argumentIndex, $newValue);
88
89         return $newValue;
90     }
91
92     /**
93      * {@inheritdoc}
94      */
95     public function generateRegex($suiteName, $pattern, $language)
96     {
97         $translatedPattern = $this->translator->trans($pattern, array(), $suiteName, $language);
98         if ($pattern == $translatedPattern) {
99             return $this->patternTransformer->transformPatternToRegex($pattern);
100         }
101
102         return $this->patternTransformer->transformPatternToRegex($translatedPattern);
103     }
104
105     /**
106      * Apply simple argument transformations in priority order.
107      *
108      * @param SimpleArgumentTransformation[] $transformations
109      * @param DefinitionCall                 $definitionCall
110      * @param integer|string                 $index
111      * @param mixed                          $value
112      *
113      * @return mixed
114      */
115     private function applySimpleTransformations(array $transformations, DefinitionCall $definitionCall, $index, $value)
116     {
117         usort($transformations, function (SimpleArgumentTransformation $t1, SimpleArgumentTransformation $t2) {
118             if ($t1->getPriority() == $t2->getPriority()) {
119                 return 0;
120             }
121
122             return ($t1->getPriority() > $t2->getPriority()) ? -1 : 1;
123         });
124
125         $newValue = $value;
126         foreach ($transformations as $transformation) {
127             $newValue = $this->transform($definitionCall, $transformation, $index, $newValue);
128         }
129
130         return $newValue;
131     }
132
133     /**
134      * Apply normal (non-simple) argument transformations.
135      *
136      * @param Transformation[] $transformations
137      * @param DefinitionCall   $definitionCall
138      * @param integer|string   $index
139      * @param mixed            $value
140      *
141      * @return mixed
142      */
143     private function applyNormalTransformations(array $transformations, DefinitionCall $definitionCall, $index, $value)
144     {
145         $newValue = $value;
146         foreach ($transformations as $transformation) {
147             $newValue = $this->transform($definitionCall, $transformation, $index, $newValue);
148         }
149
150         return $newValue;
151     }
152
153     /**
154      * Transforms argument value using registered transformers.
155      *
156      * @param Transformation $transformation
157      * @param DefinitionCall $definitionCall
158      * @param integer|string $index
159      * @param mixed          $value
160      *
161      * @return mixed
162      */
163     private function transform(DefinitionCall $definitionCall, Transformation $transformation, $index, $value)
164     {
165         if (is_object($value) && !$value instanceof ArgumentInterface) {
166             return $value;
167         }
168
169         if ($transformation instanceof SimpleArgumentTransformation &&
170             $transformation->supportsDefinitionAndArgument($definitionCall, $index, $value)) {
171             return $transformation->transformArgument($this->callCenter, $definitionCall, $index, $value);
172         }
173
174         if ($transformation instanceof PatternTransformation &&
175             $transformation->supportsDefinitionAndArgument($this, $definitionCall, $value)) {
176             return $transformation->transformArgument($this, $this->callCenter, $definitionCall, $value);
177         }
178
179         return $value;
180     }
181
182     /**
183      * Splits transformations into simple and normal ones.
184      *
185      * @param Transformation[] $transformations
186      *
187      * @return array
188      */
189     private function splitSimpleAndNormalTransformations(array $transformations)
190     {
191         return array_reduce($transformations, function ($acc, $t) {
192             return array(
193                 $t instanceof SimpleArgumentTransformation ? array_merge($acc[0], array($t)) : $acc[0],
194                 !$t instanceof SimpleArgumentTransformation ? array_merge($acc[1], array($t)) : $acc[1],
195             );
196         }, array(array(), array()));
197     }
198 }