Security update for permissions_by_term
[yaffs-website] / vendor / behat / behat / src / Behat / Behat / Transformation / Transformation / RowBasedTableTransformation.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\SimpleArgumentTransformation;
16 use Behat\Gherkin\Exception\NodeException;
17 use Behat\Gherkin\Node\TableNode;
18 use Behat\Testwork\Call\CallCenter;
19 use Behat\Testwork\Call\RuntimeCallee;
20 use ReflectionMethod;
21
22 /**
23  * Row-based table transformation.
24  *
25  * @author Konstantin Kudryashov <ever.zet@gmail.com>
26  */
27 final class RowBasedTableTransformation extends RuntimeCallee implements SimpleArgumentTransformation
28 {
29     const PATTERN_REGEX = '/^rowtable\:[[:print:]]+$/';
30
31     /**
32      * @var string
33      */
34     private $pattern;
35
36     /**
37      * {@inheritdoc}
38      */
39     static public function supportsPatternAndMethod($pattern, ReflectionMethod $method)
40     {
41         return 1 === preg_match(self::PATTERN_REGEX, $pattern);
42     }
43
44     /**
45      * Initializes transformation.
46      *
47      * @param string      $pattern
48      * @param callable    $callable
49      * @param null|string $description
50      */
51     public function __construct($pattern, $callable, $description = null)
52     {
53         $this->pattern = $pattern;
54
55         parent::__construct($callable, $description);
56     }
57
58     /**
59      * {@inheritdoc}
60      */
61     public function supportsDefinitionAndArgument(DefinitionCall $definitionCall, $argumentIndex, $value)
62     {
63         if (!$value instanceof TableNode) {
64             return false;
65         };
66
67         // What we're doing here is checking that we have a 2 column table.
68         // This bit checks we have two columns
69         try {
70             $value->getColumn(1);
71         } catch (NodeException $e) {
72             return false;
73         }
74
75         // And here we check we don't have a 3rd column
76         try {
77             $value->getColumn(2);
78         } catch (NodeException $e) {
79             // Once we know the table could be a row table, we check against the pattern.
80             return $this->pattern === 'rowtable:' . implode(',', $value->getColumn(0));
81         }
82
83         return false;
84     }
85
86     /**
87      * {@inheritdoc}
88      */
89     public function transformArgument(CallCenter $callCenter, DefinitionCall $definitionCall, $argumentIndex, $argumentValue)
90     {
91         $call = new TransformationCall(
92             $definitionCall->getEnvironment(),
93             $definitionCall->getCallee(),
94             $this,
95             array($argumentValue)
96         );
97
98         $result = $callCenter->makeCall($call);
99
100         if ($result->hasException()) {
101             throw $result->getException();
102         }
103
104         return $result->getReturn();
105     }
106
107     /**
108      * {@inheritdoc}
109      */
110     public function getPriority()
111     {
112         return 50;
113     }
114
115     /**
116      * {@inheritdoc}
117      */
118     public function getPattern()
119     {
120         return $this->pattern;
121     }
122
123     /**
124      * {@inheritdoc}
125      */
126     public function __toString()
127     {
128         return 'RowTableTransform ' . $this->pattern;
129     }
130 }