Security update for permissions_by_term
[yaffs-website] / vendor / behat / gherkin / src / Behat / Gherkin / Filter / LineRangeFilter.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 range.
20  *
21  * @author Fabian Kiss <headrevision@gmail.com>
22  */
23 class LineRangeFilter implements FilterInterface
24 {
25     protected $filterMinLine;
26     protected $filterMaxLine;
27
28     /**
29      * Initializes filter.
30      *
31      * @param string $filterMinLine Minimum line of a scenario to filter on
32      * @param string $filterMaxLine Maximum line of a scenario to filter on
33      */
34     public function __construct($filterMinLine, $filterMaxLine)
35     {
36         $this->filterMinLine = intval($filterMinLine);
37         if ($filterMaxLine == '*') {
38             $this->filterMaxLine = PHP_INT_MAX;
39         } else {
40             $this->filterMaxLine = intval($filterMaxLine);
41         }
42     }
43
44     /**
45      * Checks if Feature matches specified filter.
46      *
47      * @param FeatureNode $feature Feature instance
48      *
49      * @return Boolean
50      */
51     public function isFeatureMatch(FeatureNode $feature)
52     {
53         return $this->filterMinLine <= $feature->getLine()
54             && $this->filterMaxLine >= $feature->getLine();
55     }
56
57     /**
58      * Checks if scenario or outline matches specified filter.
59      *
60      * @param ScenarioInterface $scenario Scenario or Outline node instance
61      *
62      * @return Boolean
63      */
64     public function isScenarioMatch(ScenarioInterface $scenario)
65     {
66         if ($this->filterMinLine <= $scenario->getLine() && $this->filterMaxLine >= $scenario->getLine()) {
67             return true;
68         }
69
70         if ($scenario instanceof OutlineNode && $scenario->hasExamples()) {
71             foreach ($scenario->getExampleTable()->getLines() as $line) {
72                 if ($this->filterMinLine <= $line && $this->filterMaxLine >= $line) {
73                     return true;
74                 }
75             }
76         }
77
78         return false;
79     }
80
81     /**
82      * Filters feature according to the filter.
83      *
84      * @param FeatureNode $feature
85      *
86      * @return FeatureNode
87      */
88     public function filterFeature(FeatureNode $feature)
89     {
90         $scenarios = array();
91         foreach ($feature->getScenarios() as $scenario) {
92             if (!$this->isScenarioMatch($scenario)) {
93                 continue;
94             }
95
96             if ($scenario instanceof OutlineNode && $scenario->hasExamples()) {
97                 $table = $scenario->getExampleTable()->getTable();
98                 $lines = array_keys($table);
99
100                 $filteredTable = array($lines[0] => $table[$lines[0]]);
101                 unset($table[$lines[0]]);
102
103                 foreach ($table as $line => $row) {
104                     if ($this->filterMinLine <= $line && $this->filterMaxLine >= $line) {
105                         $filteredTable[$line] = $row;
106                     }
107                 }
108
109                 $scenario = new OutlineNode(
110                     $scenario->getTitle(),
111                     $scenario->getTags(),
112                     $scenario->getSteps(),
113                     new ExampleTableNode($filteredTable, $scenario->getExampleTable()->getKeyword()),
114                     $scenario->getKeyword(),
115                     $scenario->getLine()
116                 );
117             }
118
119             $scenarios[] = $scenario;
120         }
121
122         return new FeatureNode(
123             $feature->getTitle(),
124             $feature->getDescription(),
125             $feature->getTags(),
126             $feature->getBackground(),
127             $scenarios,
128             $feature->getKeyword(),
129             $feature->getLanguage(),
130             $feature->getFile(),
131             $feature->getLine()
132         );
133     }
134 }