fc8854047e3422953a85fbbdd8a08674f3d0032f
[yaffs-website] / vendor / symfony / finder / Iterator / MultiplePcreFilterIterator.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Finder\Iterator;
13
14 /**
15  * MultiplePcreFilterIterator filters files using patterns (regexps, globs or strings).
16  *
17  * @author Fabien Potencier <fabien@symfony.com>
18  */
19 abstract class MultiplePcreFilterIterator extends FilterIterator
20 {
21     protected $matchRegexps = array();
22     protected $noMatchRegexps = array();
23
24     /**
25      * @param \Iterator $iterator        The Iterator to filter
26      * @param array     $matchPatterns   An array of patterns that need to match
27      * @param array     $noMatchPatterns An array of patterns that need to not match
28      */
29     public function __construct(\Iterator $iterator, array $matchPatterns, array $noMatchPatterns)
30     {
31         foreach ($matchPatterns as $pattern) {
32             $this->matchRegexps[] = $this->toRegex($pattern);
33         }
34
35         foreach ($noMatchPatterns as $pattern) {
36             $this->noMatchRegexps[] = $this->toRegex($pattern);
37         }
38
39         parent::__construct($iterator);
40     }
41
42     /**
43      * Checks whether the string is accepted by the regex filters.
44      *
45      * If there is no regexps defined in the class, this method will accept the string.
46      * Such case can be handled by child classes before calling the method if they want to
47      * apply a different behavior.
48      *
49      * @param string $string The string to be matched against filters
50      *
51      * @return bool
52      */
53     protected function isAccepted($string)
54     {
55         // should at least not match one rule to exclude
56         foreach ($this->noMatchRegexps as $regex) {
57             if (preg_match($regex, $string)) {
58                 return false;
59             }
60         }
61
62         // should at least match one rule
63         if ($this->matchRegexps) {
64             foreach ($this->matchRegexps as $regex) {
65                 if (preg_match($regex, $string)) {
66                     return true;
67                 }
68             }
69
70             return false;
71         }
72
73         // If there is no match rules, the file is accepted
74         return true;
75     }
76
77     /**
78      * Checks whether the string is a regex.
79      *
80      * @param string $str
81      *
82      * @return bool Whether the given string is a regex
83      */
84     protected function isRegex($str)
85     {
86         if (preg_match('/^(.{3,}?)[imsxuADU]*$/', $str, $m)) {
87             $start = substr($m[1], 0, 1);
88             $end = substr($m[1], -1);
89
90             if ($start === $end) {
91                 return !preg_match('/[*?[:alnum:] \\\\]/', $start);
92             }
93
94             foreach (array(array('{', '}'), array('(', ')'), array('[', ']'), array('<', '>')) as $delimiters) {
95                 if ($start === $delimiters[0] && $end === $delimiters[1]) {
96                     return true;
97                 }
98             }
99         }
100
101         return false;
102     }
103
104     /**
105      * Converts string into regexp.
106      *
107      * @param string $str Pattern
108      *
109      * @return string regexp corresponding to a given string
110      */
111     abstract protected function toRegex($str);
112 }