d6cde918d7a42c35d8ab8d02b824f7c6fdb0914e
[yaffs-website] / vendor / behat / behat / src / Behat / Behat / Transformation / Call / Filter / DefinitionArgumentsTransformer.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\Call\Filter;
12
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;
18
19 /**
20  * Handles definition calls by intercepting them and transforming their arguments using transformations.
21  *
22  * @author Konstantin Kudryashov <ever.zet@gmail.com>
23  */
24 final class DefinitionArgumentsTransformer implements CallFilter
25 {
26     /**
27      * @var ArgumentTransformer[]
28      */
29     private $argumentTransformers = array();
30
31     /**
32      * Registers new argument transformer.
33      *
34      * @param ArgumentTransformer $transformer
35      */
36     public function registerArgumentTransformer(ArgumentTransformer $transformer)
37     {
38         $this->argumentTransformers[] = $transformer;
39     }
40
41     /**
42      * {@inheritdoc}
43      */
44     public function supportsCall(Call $call)
45     {
46         return $call instanceof DefinitionCall;
47     }
48
49     /**
50      * {@inheritdoc}
51      */
52     public function filterCall(Call $definitionCall)
53     {
54         if (!$definitionCall instanceof DefinitionCall) {
55             throw new UnsupportedCallException(sprintf(
56                 'DefinitionArgumentTransformer can not filter `%s` call.',
57                 get_class($definitionCall)
58             ), $definitionCall);
59         }
60
61         $newArguments = array();
62         $transformed = false;
63         foreach ($definitionCall->getArguments() as $index => $value) {
64             $newValue = $this->transformArgument($definitionCall, $index, $value);
65
66             if ($newValue !== $value) {
67                 $transformed = true;
68             }
69
70             $newArguments[$index] = $newValue;
71         }
72
73         if (!$transformed) {
74             return $definitionCall;
75         }
76
77         return new DefinitionCall(
78             $definitionCall->getEnvironment(),
79             $definitionCall->getFeature(),
80             $definitionCall->getStep(),
81             $definitionCall->getCallee(),
82             $newArguments,
83             $definitionCall->getErrorReportingLevel()
84         );
85     }
86
87     /**
88      * Transforms call argument using registered transformers.
89      *
90      * @param DefinitionCall $definitionCall
91      * @param integer|string $index
92      * @param mixed          $value
93      *
94      * @return mixed
95      */
96     private function transformArgument(DefinitionCall $definitionCall, $index, $value)
97     {
98         foreach ($this->argumentTransformers as $transformer) {
99             if (!$transformer->supportsDefinitionAndArgument($definitionCall, $index, $value)) {
100                 continue;
101             }
102
103             return $transformer->transformArgument($definitionCall, $index, $value);
104         }
105
106         return $value;
107     }
108 }