/vendor/symfony/src/Symfony/Component/Finder/Iterator/FilenameFilterIterator.php
PHP | 92 lines | 53 code | 12 blank | 27 comment | 8 complexity | b4e980d96ade383af662297051b5e39b MD5 | raw file
Possible License(s): Apache-2.0, JSON, LGPL-2.1, LGPL-2.0, LGPL-3.0, BSD-3-Clause, BSD-2-Clause
- <?php
-
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
- namespace Symfony\Component\Finder\Iterator;
-
- use Symfony\Component\Finder\Glob;
-
- /**
- * FilenameFilterIterator filters files by patterns (a regexp, a glob, or a string).
- *
- * @author Fabien Potencier <fabien@symfony.com>
- */
- class FilenameFilterIterator extends \FilterIterator
- {
- private $matchRegexps;
- private $noMatchRegexps;
-
- /**
- * Constructor.
- *
- * @param \Iterator $iterator The Iterator to filter
- * @param array $matchPatterns An array of patterns that need to match
- * @param array $noMatchPatterns An array of patterns that need to not match
- */
- public function __construct(\Iterator $iterator, array $matchPatterns, array $noMatchPatterns)
- {
- $this->matchRegexps = array();
- foreach ($matchPatterns as $pattern) {
- $this->matchRegexps[] = $this->toRegex($pattern);
- }
-
- $this->noMatchRegexps = array();
- foreach ($noMatchPatterns as $pattern) {
- $this->noMatchRegexps[] = $this->toRegex($pattern);
- }
-
- parent::__construct($iterator);
- }
-
- /**
- * Filters the iterator values.
- *
- * @return Boolean true if the value should be kept, false otherwise
- */
- public function accept()
- {
- // should at least match one rule
- if ($this->matchRegexps) {
- $match = false;
- foreach ($this->matchRegexps as $regex) {
- if (preg_match($regex, $this->getFilename())) {
- $match = true;
- break;
- }
- }
- } else {
- $match = true;
- }
-
- // should at least not match one rule to exclude
- if ($this->noMatchRegexps) {
- $exclude = false;
- foreach ($this->noMatchRegexps as $regex) {
- if (preg_match($regex, $this->getFilename())) {
- $exclude = true;
- break;
- }
- }
- } else {
- $exclude = false;
- }
-
- return $match && !$exclude;
- }
-
- private function toRegex($str)
- {
- if (preg_match('/^([^a-zA-Z0-9\\\\]).+?\\1[ims]?$/', $str)) {
- return $str;
- }
-
- return Glob::toRegex($str);
- }
- }