vendor/symfony/finder/Iterator/CustomFilterIterator.php line 49

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Finder\Iterator;
  11. /**
  12.  * CustomFilterIterator filters files by applying anonymous functions.
  13.  *
  14.  * The anonymous function receives a \SplFileInfo and must return false
  15.  * to remove files.
  16.  *
  17.  * @author Fabien Potencier <fabien@symfony.com>
  18.  */
  19. class CustomFilterIterator extends FilterIterator
  20. {
  21.     private $filters = [];
  22.     /**
  23.      * @param \Iterator  $iterator The Iterator to filter
  24.      * @param callable[] $filters  An array of PHP callbacks
  25.      *
  26.      * @throws \InvalidArgumentException
  27.      */
  28.     public function __construct(\Iterator $iterator, array $filters)
  29.     {
  30.         foreach ($filters as $filter) {
  31.             if (!\is_callable($filter)) {
  32.                 throw new \InvalidArgumentException('Invalid PHP callback.');
  33.             }
  34.         }
  35.         $this->filters $filters;
  36.         parent::__construct($iterator);
  37.     }
  38.     /**
  39.      * Filters the iterator values.
  40.      *
  41.      * @return bool true if the value should be kept, false otherwise
  42.      */
  43.     public function accept()
  44.     {
  45.         $fileinfo $this->current();
  46.         foreach ($this->filters as $filter) {
  47.             if (false === \call_user_func($filter$fileinfo)) {
  48.                 return false;
  49.             }
  50.         }
  51.         return true;
  52.     }
  53. }