4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Finder\Iterator;
15 * MultiplePcreFilterIterator filters files using patterns (regexps, globs or strings).
17 * @author Fabien Potencier <fabien@symfony.com>
19 abstract class MultiplePcreFilterIterator extends FilterIterator
21 protected $matchRegexps = array();
22 protected $noMatchRegexps = array();
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
31 public function __construct(\Iterator $iterator, array $matchPatterns, array $noMatchPatterns)
33 foreach ($matchPatterns as $pattern) {
34 $this->matchRegexps[] = $this->toRegex($pattern);
37 foreach ($noMatchPatterns as $pattern) {
38 $this->noMatchRegexps[] = $this->toRegex($pattern);
41 parent::__construct($iterator);
45 * Checks whether the string is accepted by the regex filters.
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.
51 * @param string $string The string to be matched against filters
55 protected function isAccepted($string)
57 // should at least not match one rule to exclude
58 foreach ($this->noMatchRegexps as $regex) {
59 if (preg_match($regex, $string)) {
64 // should at least match one rule
65 if ($this->matchRegexps) {
66 foreach ($this->matchRegexps as $regex) {
67 if (preg_match($regex, $string)) {
75 // If there is no match rules, the file is accepted
80 * Checks whether the string is a regex.
84 * @return bool Whether the given string is a regex
86 protected function isRegex($str)
88 if (preg_match('/^(.{3,}?)[imsxuADU]*$/', $str, $m)) {
89 $start = substr($m[1], 0, 1);
90 $end = substr($m[1], -1);
92 if ($start === $end) {
93 return !preg_match('/[*?[:alnum:] \\\\]/', $start);
96 foreach (array(array('{', '}'), array('(', ')'), array('[', ']'), array('<', '>')) as $delimiters) {
97 if ($start === $delimiters[0] && $end === $delimiters[1]) {
107 * Converts string into regexp.
109 * @param string $str Pattern
111 * @return string regexp corresponding to a given string
113 abstract protected function toRegex($str);