Security update for permissions_by_term
[yaffs-website] / vendor / behat / behat / src / Behat / Behat / Definition / Pattern / Policy / RegexPatternPolicy.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\Definition\Pattern\Policy;
12
13 use Behat\Behat\Definition\Exception\InvalidPatternException;
14 use Behat\Behat\Definition\Pattern\Pattern;
15 use Behat\Transliterator\Transliterator;
16
17 /**
18  * Defines a way to handle regex patterns.
19  *
20  * @author Konstantin Kudryashov <ever.zet@gmail.com>
21  */
22 final class RegexPatternPolicy implements PatternPolicy
23 {
24     /**
25      * @var string[string]
26      */
27     private static $replacePatterns = array(
28         "/(?<=\W|^)\\\'(?:((?!\\').)*)\\\'(?=\W|$)/" => "'([^']*)'", // Single quoted strings
29         '/(?<=\W|^)\"(?:[^\"]*)\"(?=\W|$)/'          => "\"([^\"]*)\"", // Double quoted strings
30         '/(?<=\W|^)(\d+)(?=\W|$)/'                   => "(\\d+)", // Numbers
31     );
32
33     /**
34      * {@inheritdoc}
35      */
36     public function supportsPatternType($type)
37     {
38         return 'regex' === $type;
39     }
40
41     /**
42      * {@inheritdoc}
43      */
44     public function generatePattern($stepText)
45     {
46         $canonicalText = $this->generateCanonicalText($stepText);
47         $stepRegex = $this->generateRegex($stepText);
48         $placeholderCount = $this->countPlaceholders($stepText, $stepRegex);
49
50         return new Pattern($canonicalText, '/^' . $stepRegex . '$/', $placeholderCount);
51     }
52
53     /**
54      * {@inheritdoc}
55      */
56     public function supportsPattern($pattern)
57     {
58         return (bool) preg_match('/^(?:\\{.*\\}|([~\\/#`]).*\1)[imsxADSUXJu]*$/s', $pattern);
59     }
60
61     /**
62      * {@inheritdoc}
63      */
64     public function transformPatternToRegex($pattern)
65     {
66         if (false === @preg_match($pattern, 'anything')) {
67             $error = error_get_last();
68             $errorMessage = isset($error['message']) ? $error['message'] : '';
69
70             throw new InvalidPatternException(sprintf('The regex `%s` is invalid: %s', $pattern, $errorMessage));
71         }
72
73         return $pattern;
74     }
75
76     /**
77      * Generates regex from step text.
78      *
79      * @param string $stepText
80      *
81      * @return string
82      */
83     private function generateRegex($stepText)
84     {
85         return preg_replace(
86             array_keys(self::$replacePatterns),
87             array_values(self::$replacePatterns),
88             $this->escapeStepText($stepText)
89         );
90     }
91
92     /**
93      * Generates canonical text for step text.
94      *
95      * @param string $stepText
96      *
97      * @return string
98      */
99     private function generateCanonicalText($stepText)
100     {
101         $canonicalText = preg_replace(array_keys(self::$replacePatterns), '', $stepText);
102         $canonicalText = Transliterator::transliterate($canonicalText, ' ');
103         $canonicalText = preg_replace('/[^a-zA-Z\_\ ]/', '', $canonicalText);
104         $canonicalText = str_replace(' ', '', ucwords($canonicalText));
105
106         return $canonicalText;
107     }
108
109     /**
110      * Counts regex placeholders using provided text.
111      *
112      * @param string $stepText
113      * @param string $stepRegex
114      *
115      * @return integer
116      */
117     private function countPlaceholders($stepText, $stepRegex)
118     {
119         preg_match('/^' . $stepRegex . '$/', $stepText, $matches);
120
121         return count($matches) ? count($matches) - 1 : 0;
122     }
123
124     /**
125      * Returns escaped step text.
126      *
127      * @param string $stepText
128      *
129      * @return string
130      */
131     private function escapeStepText($stepText)
132     {
133         return preg_replace('/([\/\[\]\(\)\\\^\$\.\|\?\*\+\'])/', '\\\\$1', $stepText);
134     }
135 }