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();
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
29 public function __construct(\Iterator $iterator, array $matchPatterns, array $noMatchPatterns)
31 foreach ($matchPatterns as $pattern) {
32 $this->matchRegexps[] = $this->toRegex($pattern);
35 foreach ($noMatchPatterns as $pattern) {
36 $this->noMatchRegexps[] = $this->toRegex($pattern);
39 parent::__construct($iterator);
43 * Checks whether the string is accepted by the regex filters.
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.
49 * @param string $string The string to be matched against filters
53 protected function isAccepted($string)
55 // should at least not match one rule to exclude
56 foreach ($this->noMatchRegexps as $regex) {
57 if (preg_match($regex, $string)) {
62 // should at least match one rule
63 if ($this->matchRegexps) {
64 foreach ($this->matchRegexps as $regex) {
65 if (preg_match($regex, $string)) {
73 // If there is no match rules, the file is accepted
78 * Checks whether the string is a regex.
82 * @return bool Whether the given string is a regex
84 protected function isRegex($str)
86 if (preg_match('/^(.{3,}?)[imsxuADU]*$/', $str, $m)) {
87 $start = substr($m[1], 0, 1);
88 $end = substr($m[1], -1);
90 if ($start === $end) {
91 return !preg_match('/[*?[:alnum:] \\\\]/', $start);
94 foreach (array(array('{', '}'), array('(', ')'), array('[', ']'), array('<', '>')) as $delimiters) {
95 if ($start === $delimiters[0] && $end === $delimiters[1]) {
105 * Converts string into regexp.
107 * @param string $str Pattern
109 * @return string regexp corresponding to a given string
111 abstract protected function toRegex($str);