Yaffs site version 1.1
[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      * Constructor.
26      *
27      * @param \Iterator $iterator        The Iterator to filter
28      * @param array     $matchPatterns   An array of patterns that need to match
29      * @param array     $noMatchPatterns An array of patterns that need to not match
30      */
31     public function __construct(\Iterator $iterator, array $matchPatterns, array $noMatchPatterns)
32     {
33         foreach ($matchPatterns as $pattern) {
34             $this->matchRegexps[] = $this->toRegex($pattern);
35         }
36
37         foreach ($noMatchPatterns as $pattern) {
38             $this->noMatchRegexps[] = $this->toRegex($pattern);
39         }
40
41         parent::__construct($iterator);
42     }
43
44     /**
45      * Checks whether the string is accepted by the regex filters.
46      *
47      * If there is no regexps defined in the class, this method will accept the string.
48      * Such case can be handled by child classes before calling the method if they want to
49      * apply a different behavior.
50      *
51      * @param string $string The string to be matched against filters
52      *
53      * @return bool
54      */
55     protected function isAccepted($string)
56     {
57         // should at least not match one rule to exclude
58         foreach ($this->noMatchRegexps as $regex) {
59             if (preg_match($regex, $string)) {
60                 return false;
61             }
62         }
63
64         // should at least match one rule
65         if ($this->matchRegexps) {
66             foreach ($this->matchRegexps as $regex) {
67                 if (preg_match($regex, $string)) {
68                     return true;
69                 }
70             }
71
72             return false;
73         }
74
75         // If there is no match rules, the file is accepted
76         return true;
77     }
78
79     /**
80      * Checks whether the string is a regex.
81      *
82      * @param string $str
83      *
84      * @return bool Whether the given string is a regex
85      */
86     protected function isRegex($str)
87     {
88         if (preg_match('/^(.{3,}?)[imsxuADU]*$/', $str, $m)) {
89             $start = substr($m[1], 0, 1);
90             $end = substr($m[1], -1);
91
92             if ($start === $end) {
93                 return !preg_match('/[*?[:alnum:] \\\\]/', $start);
94             }
95
96             foreach (array(array('{', '}'), array('(', ')'), array('[', ']'), array('<', '>')) as $delimiters) {
97                 if ($start === $delimiters[0] && $end === $delimiters[1]) {
98                     return true;
99                 }
100             }
101         }
102
103         return false;
104     }
105
106     /**
107      * Converts string into regexp.
108      *
109      * @param string $str Pattern
110      *
111      * @return string regexp corresponding to a given string
112      */
113     abstract protected function toRegex($str);
114 }