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: * MultiplePcreFilterIterator filters files using patterns (regexps, globs or strings).
18: *
19: * @author Fabien Potencier <fabien@symfony.com>
20: */
21: abstract class MultiplePcreFilterIterator extends FilterIterator
22: {
23: protected $matchRegexps;
24: protected $noMatchRegexps;
25:
26: /**
27: * Constructor.
28: *
29: * @param \Iterator $iterator The Iterator to filter
30: * @param array $matchPatterns An array of patterns that need to match
31: * @param array $noMatchPatterns An array of patterns that need to not match
32: */
33: public function __construct(\Iterator $iterator, array $matchPatterns, array $noMatchPatterns)
34: {
35: $this->matchRegexps = array();
36: foreach ($matchPatterns as $pattern) {
37: $this->matchRegexps[] = $this->toRegex($pattern);
38: }
39:
40: $this->noMatchRegexps = array();
41: foreach ($noMatchPatterns as $pattern) {
42: $this->noMatchRegexps[] = $this->toRegex($pattern);
43: }
44:
45: parent::__construct($iterator);
46: }
47:
48: /**
49: * Checks whether the string is a regex.
50: *
51: * @param string $str
52: *
53: * @return Boolean Whether the given string is a regex
54: */
55: protected function isRegex($str)
56: {
57: return Expression::create($str)->isRegex();
58: }
59:
60: /**
61: * Converts string into regexp.
62: *
63: * @param string $str Pattern
64: *
65: * @return string regexp corresponding to a given string
66: */
67: abstract protected function toRegex($str);
68: }
69: