1<?php 2declare(strict_types=1); 3 4namespace ILIAS\Filesystem\Finder\Iterator; 5 6use ILIAS\Filesystem\Filesystem; 7use ILIAS\Filesystem\Finder\Comparator\DateComparator; 8use ILIAS\Filesystem\DTO\Metadata; 9 10/** 11 * Class DateRangeFilterIterator 12 * @package ILIAS\Filesystem\Finder\Iterator 13 * @author Michael Jansen <mjansen@databay.de> 14 */ 15class DateRangeFilterIterator extends \FilterIterator 16{ 17 /** @var FileSystem */ 18 private $filesystem; 19 20 /** @var DateComparator[] */ 21 private $comparators = []; 22 23 /** 24 * @param Filesystem $filesystem 25 * @param \Iterator $iterator The Iterator to filter 26 * @param DateComparator[] $comparators An array of DateComparator instances 27 */ 28 public function __construct(FileSystem $filesystem, \Iterator $iterator, array $comparators) 29 { 30 array_walk($comparators, function ($comparator) { 31 if (!($comparator instanceof DateComparator)) { 32 if (is_object($comparator)) { 33 throw new \InvalidArgumentException(sprintf( 34 'Invalid comparator given: %s', 35 get_class($comparator) 36 )); 37 } 38 39 throw new \InvalidArgumentException(sprintf('Invalid comparator given: %s', gettype($comparator))); 40 } 41 }); 42 43 $this->filesystem = $filesystem; 44 $this->comparators = $comparators; 45 46 parent::__construct($iterator); 47 } 48 49 /** 50 * @inheritdoc 51 */ 52 public function accept() 53 { 54 /** @var Metadata $metadata */ 55 $metadata = $this->current(); 56 if (!$this->filesystem->has($metadata->getPath())) { 57 return false; 58 } 59 60 $timestamp = $this->filesystem->getTimestamp($metadata->getPath()); 61 foreach ($this->comparators as $compare) { 62 if (!$compare->test($timestamp->format('U'))) { 63 return false; 64 } 65 } 66 67 return true; 68 } 69} 70