Skip to content

Commit

Permalink
[Finder] added a data range filter
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed May 14, 2010
1 parent c757616 commit b6852c3
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 6 deletions.
63 changes: 57 additions & 6 deletions src/Symfony/Components/Finder/Finder.php
Expand Up @@ -36,13 +36,15 @@ class Finder implements \IteratorAggregate
protected $notNames = array();
protected $exclude = array();
protected $filters = array();
protected $mindepth = 0;
protected $maxdepth = INF;
protected $minDepth = 0;
protected $maxDepth = INF;
protected $sizes = array();
protected $followLinks = false;
protected $sort = false;
protected $ignoreVCS = true;
protected $dirs = array();
protected $minDate = false;
protected $maxDate = false;

/**
* Restricts the matching to directories only.
Expand Down Expand Up @@ -81,7 +83,7 @@ public function files()
*/
public function maxDepth($level)
{
$this->maxdepth = (double) $level;
$this->maxDepth = (double) $level;

return $this;
}
Expand All @@ -99,7 +101,52 @@ public function maxDepth($level)
*/
public function minDepth($level)
{
$this->mindepth = (integer) $level;
$this->minDepth = (integer) $level;

return $this;
}

/**
* Sets the maximum date (last modified) for a file or directory.
*
* The date must be something that strtotime() is able to parse:
*
* $finder->maxDate('yesterday');
* $finder->maxDate('2 days ago');
* $finder->maxDate('now - 2 hours');
* $finder->maxDate('2005-10-15');
*
* @param string $date A date
*
* @return Symfony\Components\Finder The current Finder instance
*
* @see Symfony\Components\Finder\Iterator\DateRangeFilterIterator
*/
public function maxDate($date)
{
if (false === $this->maxDate = @strtotime($date)) {
throw new \InvalidArgumentException(sprintf('"%s" is not a valid date'));
}

return $this;
}

/**
* Sets the minimum date (last modified) for a file or a directory.
*
* The date must be something that strtotime() is able to parse (@see maxDate()).
*
* @param string $date A date
*
* @return Symfony\Components\Finder The current Finder instance
*
* @see Symfony\Components\Finder\Iterator\DateRangeFilterIterator
*/
public function minDate($date)
{
if (false === $this->minDate = @strtotime($date)) {
throw new \InvalidArgumentException(sprintf('"%s" is not a valid date'));
}

return $this;
}
Expand Down Expand Up @@ -339,8 +386,8 @@ protected function searchInDirectory($dir)

$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir, $flags), \RecursiveIteratorIterator::SELF_FIRST);

if ($this->mindepth > 0 || $this->maxdepth < INF) {
$iterator = new Iterator\LimitDepthFilterIterator($iterator, $this->mindepth, $this->maxdepth);
if ($this->minDepth > 0 || $this->maxDepth < INF) {
$iterator = new Iterator\LimitDepthFilterIterator($iterator, $this->minDepth, $this->maxDepth);
}

if ($this->mode) {
Expand All @@ -363,6 +410,10 @@ protected function searchInDirectory($dir)
$iterator = new Iterator\SizeRangeFilterIterator($iterator, $this->sizes);
}

if (false !== $this->minDate || false !== $this->maxDate) {
$iterator = new Iterator\DateRangeFilterIterator($iterator, $this->minDate, $this->maxDate);
}

if ($this->filters) {
$iterator = new Iterator\CustomFilterIterator($iterator, $this->filters);
}
Expand Down
66 changes: 66 additions & 0 deletions src/Symfony/Components/Finder/Iterator/DateRangeFilterIterator.php
@@ -0,0 +1,66 @@
<?php

namespace Symfony\Components\Finder\Iterator;

/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

/**
* DateRangeFilterIterator filters out files that are not in the given date range (last modified dates).
*
* @package Symfony
* @subpackage Components_Finder
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class DateRangeFilterIterator extends \FilterIterator
{
protected $minDate = false;
protected $maxDate = false;

/**
* Constructor.
*
* @param \Iterator $iterator The Iterator to filter
* @param integer $minDate The minimum date
* @param integer $maxDate The maximum date
*/
public function __construct(\Iterator $iterator, $minDate = false, $maxDate = false)
{
$this->minDate = $minDate;
$this->maxDate = $maxDate;

parent::__construct($iterator);
}

/**
* Filters the iterator values.
*
* @return Boolean true if the value should be kept, false otherwise
*/
public function accept()
{
$fileinfo = $this->getInnerIterator()->current();

if (!$fileinfo->isFile()) {
return true;
}

$filedate = $fileinfo->getMTime();

if (
(false !== $this->minDate && $filedate < $this->minDate)
||
(false !== $this->maxDate && $filedate > $this->maxDate)
) {
return false;
}

return true;
}
}

0 comments on commit b6852c3

Please sign in to comment.