Security update for permissions_by_term
[yaffs-website] / vendor / behat / behat / src / Behat / Behat / Transformation / Transformation / PatternTransformation.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\Transformation;
12
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;
19 use Exception;
20
21 /**
22  * Pattern-based transformation.
23  *
24  * @author Konstantin Kudryashov <ever.zet@gmail.com>
25  */
26 final class PatternTransformation extends RuntimeCallee implements Transformation
27 {
28     /**
29      * @var string
30      */
31     private $pattern;
32
33     /**
34      * Initializes transformation.
35      *
36      * @param string      $pattern
37      * @param callable    $callable
38      * @param null|string $description
39      */
40     public function __construct($pattern, $callable, $description = null)
41     {
42         $this->pattern = $pattern;
43
44         parent::__construct($callable, $description);
45     }
46
47     /**
48      * Checks if transformer supports argument.
49      *
50      * @param RegexGenerator $regexGenerator
51      * @param DefinitionCall $definitionCall
52      * @param mixed          $argumentValue
53      *
54      * @return bool
55      */
56     public function supportsDefinitionAndArgument(
57         RegexGenerator $regexGenerator,
58         DefinitionCall $definitionCall,
59         $argumentValue
60     ) {
61         $regex = $regexGenerator->generateRegex(
62             $definitionCall->getEnvironment()->getSuite()->getName(),
63             $this->pattern,
64             $definitionCall->getFeature()->getLanguage()
65         );
66
67         return $this->match($regex, $argumentValue, $match);
68     }
69
70     /**
71      * Transforms argument value using transformation and returns a new one.
72      *
73      * @param RegexGenerator $regexGenerator
74      * @param CallCenter     $callCenter
75      * @param DefinitionCall $definitionCall
76      * @param mixed          $argumentValue
77      *
78      * @return mixed
79      *
80      * @throws Exception If transformation throws exception
81      */
82     public function transformArgument(
83         RegexGenerator $regexGenerator,
84         CallCenter $callCenter,
85         DefinitionCall $definitionCall,
86         $argumentValue
87     ) {
88         $regex = $regexGenerator->generateRegex(
89             $definitionCall->getEnvironment()->getSuite()->getName(),
90             $this->pattern,
91             $definitionCall->getFeature()->getLanguage()
92         );
93
94         $this->match($regex, $argumentValue, $arguments);
95
96         $call = new TransformationCall(
97             $definitionCall->getEnvironment(),
98             $definitionCall->getCallee(),
99             $this,
100             $arguments
101         );
102
103         $result = $callCenter->makeCall($call);
104
105         if ($result->hasException()) {
106             throw $result->getException();
107         }
108
109         return $result->getReturn();
110     }
111
112     /**
113      * {@inheritdoc}
114      */
115     public function getPattern()
116     {
117         return $this->pattern;
118     }
119
120     /**
121      * {@inheritdoc}
122      */
123     public function __toString()
124     {
125         return 'PatternTransform ' . $this->pattern;
126     }
127
128     /**
129      * @param $regexPattern
130      * @param $argumentValue
131      * @param $match
132      *
133      * @return bool
134      */
135     private function match($regexPattern, $argumentValue, &$match)
136     {
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);
141             }
142
143             return true;
144         }
145
146         return false;
147     }
148 }