Security update for permissions_by_term
[yaffs-website] / vendor / behat / gherkin / src / Behat / Gherkin / Filter / TagFilter.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\FeatureNode;
14 use Behat\Gherkin\Node\ScenarioInterface;
15
16 /**
17  * Filters scenarios by feature/scenario tag.
18  *
19  * @author Konstantin Kudryashov <ever.zet@gmail.com>
20  */
21 class TagFilter extends ComplexFilter
22 {
23     protected $filterString;
24
25     /**
26      * Initializes filter.
27      *
28      * @param string $filterString Name filter string
29      */
30     public function __construct($filterString)
31     {
32         $this->filterString = trim($filterString);
33     }
34
35     /**
36      * Checks if Feature matches specified filter.
37      *
38      * @param FeatureNode $feature Feature instance
39      *
40      * @return Boolean
41      */
42     public function isFeatureMatch(FeatureNode $feature)
43     {
44         return $this->isTagsMatchCondition($feature->getTags());
45     }
46
47     /**
48      * Checks if scenario or outline matches specified filter.
49      *
50      * @param FeatureNode       $feature  Feature node instance
51      * @param ScenarioInterface $scenario Scenario or Outline node instance
52      *
53      * @return Boolean
54      */
55     public function isScenarioMatch(FeatureNode $feature, ScenarioInterface $scenario)
56     {
57         return $this->isTagsMatchCondition(array_merge($feature->getTags(), $scenario->getTags()));
58     }
59
60     /**
61      * Checks that node matches condition.
62      *
63      * @param string[] $tags
64      *
65      * @return Boolean
66      */
67     protected function isTagsMatchCondition($tags)
68     {
69         $satisfies = true;
70
71         foreach (explode('&&', $this->filterString) as $andTags) {
72             $satisfiesComma = false;
73
74             foreach (explode(',', $andTags) as $tag) {
75                 $tag = str_replace('@', '', trim($tag));
76
77                 if ('~' === $tag[0]) {
78                     $tag = mb_substr($tag, 1, mb_strlen($tag, 'utf8') - 1, 'utf8');
79                     $satisfiesComma = !in_array($tag, $tags) || $satisfiesComma;
80                 } else {
81                     $satisfiesComma = in_array($tag, $tags) || $satisfiesComma;
82                 }
83             }
84
85             $satisfies = (false !== $satisfiesComma && $satisfies && $satisfiesComma) || false;
86         }
87
88         return $satisfies;
89     }
90 }