Security update for permissions_by_term
[yaffs-website] / vendor / behat / gherkin / src / Behat / Gherkin / Filter / LineFilter.php
1 <?php
2
3 /*
4  * This file is part of the Behat Gherkin.
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\Gherkin\Filter;
12
13 use Behat\Gherkin\Node\ExampleTableNode;
14 use Behat\Gherkin\Node\FeatureNode;
15 use Behat\Gherkin\Node\OutlineNode;
16 use Behat\Gherkin\Node\ScenarioInterface;
17
18 /**
19  * Filters scenarios by definition line number.
20  *
21  * @author Konstantin Kudryashov <ever.zet@gmail.com>
22  */
23 class LineFilter implements FilterInterface
24 {
25     protected $filterLine;
26
27     /**
28      * Initializes filter.
29      *
30      * @param string $filterLine Line of the scenario to filter on
31      */
32     public function __construct($filterLine)
33     {
34         $this->filterLine = intval($filterLine);
35     }
36
37     /**
38      * Checks if Feature matches specified filter.
39      *
40      * @param FeatureNode $feature Feature instance
41      *
42      * @return Boolean
43      */
44     public function isFeatureMatch(FeatureNode $feature)
45     {
46         return $this->filterLine === $feature->getLine();
47     }
48
49     /**
50      * Checks if scenario or outline matches specified filter.
51      *
52      * @param ScenarioInterface $scenario Scenario or Outline node instance
53      *
54      * @return Boolean
55      */
56     public function isScenarioMatch(ScenarioInterface $scenario)
57     {
58         if ($this->filterLine === $scenario->getLine()) {
59             return true;
60         }
61
62         if ($scenario instanceof OutlineNode && $scenario->hasExamples()) {
63             return $this->filterLine === $scenario->getLine()
64                 || in_array($this->filterLine, $scenario->getExampleTable()->getLines());
65         }
66
67         return false;
68     }
69
70     /**
71      * Filters feature according to the filter and returns new one.
72      *
73      * @param FeatureNode $feature
74      *
75      * @return FeatureNode
76      */
77     public function filterFeature(FeatureNode $feature)
78     {
79         $scenarios = array();
80         foreach ($feature->getScenarios() as $scenario) {
81             if (!$this->isScenarioMatch($scenario)) {
82                 continue;
83             }
84
85             if ($scenario instanceof OutlineNode && $scenario->hasExamples()) {
86                 $table = $scenario->getExampleTable()->getTable();
87                 $lines = array_keys($table);
88
89                 if (in_array($this->filterLine, $lines)) {
90                     $filteredTable = array($lines[0] => $table[$lines[0]]);
91
92                     if ($lines[0] !== $this->filterLine) {
93                         $filteredTable[$this->filterLine] = $table[$this->filterLine];
94                     }
95
96                     $scenario = new OutlineNode(
97                         $scenario->getTitle(),
98                         $scenario->getTags(),
99                         $scenario->getSteps(),
100                         new ExampleTableNode($filteredTable, $scenario->getExampleTable()->getKeyword()),
101                         $scenario->getKeyword(),
102                         $scenario->getLine()
103                     );
104                 }
105             }
106
107             $scenarios[] = $scenario;
108         }
109
110         return new FeatureNode(
111             $feature->getTitle(),
112             $feature->getDescription(),
113             $feature->getTags(),
114             $feature->getBackground(),
115             $scenarios,
116             $feature->getKeyword(),
117             $feature->getLanguage(),
118             $feature->getFile(),
119             $feature->getLine()
120         );
121     }
122 }