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: use Symfony\Component\Finder\Expression\Expression;
15:
16: /**
17: * FilenameFilterIterator filters files by patterns (a regexp, a glob, or a string).
18: *
19: * @author Fabien Potencier <fabien@symfony.com>
20: */
21: class FilenameFilterIterator extends MultiplePcreFilterIterator
22: {
23:
24: /**
25: * Filters the iterator values.
26: *
27: * @return Boolean true if the value should be kept, false otherwise
28: */
29: public function accept()
30: {
31: $filename = $this->current()->getFilename();
32:
33: // should at least not match one rule to exclude
34: foreach ($this->noMatchRegexps as $regex) {
35: if (preg_match($regex, $filename)) {
36: return false;
37: }
38: }
39:
40: // should at least match one rule
41: $match = true;
42: if ($this->matchRegexps) {
43: $match = false;
44: foreach ($this->matchRegexps as $regex) {
45: if (preg_match($regex, $filename)) {
46: return true;
47: }
48: }
49: }
50:
51: return $match;
52: }
53:
54: /**
55: * Converts glob to regexp.
56: *
57: * PCRE patterns are left unchanged.
58: * Glob strings are transformed with Glob::toRegex().
59: *
60: * @param string $str Pattern: glob or regexp
61: *
62: * @return string regexp corresponding to a given glob or regexp
63: */
64: protected function toRegex($str)
65: {
66: return Expression::create($str)->getRegex()->render();
67: }
68: }
69: