Skip to content

Commit

Permalink
[Finder] refactored SortableIterator
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed May 9, 2011
1 parent 50c1cce commit e1c5276
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
33 changes: 21 additions & 12 deletions src/Symfony/Component/Finder/Iterator/SortableIterator.php
Expand Up @@ -16,27 +16,30 @@
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class SortableIterator extends \ArrayIterator
class SortableIterator implements \IteratorAggregate
{
const SORT_BY_NAME = 1;
const SORT_BY_TYPE = 2;

private $iterator;
private $sort;

/**
* Constructor.
*
* @param \Iterator $iterator The Iterator to filter
* @param integer|\Closure $sort The sort type (SORT_BY_NAME, SORT_BY_TYPE, or a \Closure instance)
* @param \Traversable $iterator The Iterator to filter
* @param integer|callback $sort The sort type (SORT_BY_NAME, SORT_BY_TYPE, or a PHP callback)
*/
public function __construct(\Iterator $iterator, $sort)
public function __construct(\Traversable $iterator, $sort)
{
$this->iterator = $iterator;

if (self::SORT_BY_NAME === $sort) {
$sort = function ($a, $b)
{
$this->sort = function ($a, $b) {
return strcmp($a->getRealpath(), $b->getRealpath());
};
} elseif (self::SORT_BY_TYPE === $sort) {
$sort = function ($a, $b)
{
$this->sort = function ($a, $b) {
if ($a->isDir() && $b->isFile()) {
return -1;
} elseif ($a->isFile() && $b->isDir()) {
Expand All @@ -45,12 +48,18 @@ public function __construct(\Iterator $iterator, $sort)

return strcmp($a->getRealpath(), $b->getRealpath());
};
} elseif (!$sort instanceof \Closure) {
throw new \InvalidArgumentException(sprintf('The SortableIterator takes a \Closure or a valid built-in sort algorithm as an argument (%s given).', $sort));
} elseif (is_callable($sort)) {
$this->sort = $sort;
} else {
throw new \InvalidArgumentException('The SortableIterator takes a PHP callback or a valid built-in sort algorithm as an argument.');
}
}

parent::__construct(iterator_to_array($iterator));
public function getIterator()
{
$array = iterator_to_array($this->iterator, true);
uasort($array, $this->sort);

$this->uasort($sort);
return new \ArrayIterator($array);
}
}
Expand Up @@ -15,7 +15,7 @@

abstract class IteratorTestCase extends \PHPUnit_Framework_TestCase
{
protected function assertIterator($expected, \Iterator $iterator)
protected function assertIterator($expected, \Traversable $iterator)
{
$values = array_map(function (\SplFileInfo $fileinfo) { return $fileinfo->getPathname(); }, iterator_to_array($iterator));

Expand All @@ -25,7 +25,7 @@ protected function assertIterator($expected, \Iterator $iterator)
$this->assertEquals($expected, array_values($values));
}

protected function assertOrderedIterator($expected, \Iterator $iterator)
protected function assertOrderedIterator($expected, \Traversable $iterator)
{
$values = array_map(function (\SplFileInfo $fileinfo) { return $fileinfo->getPathname(); }, iterator_to_array($iterator));

Expand Down

0 comments on commit e1c5276

Please sign in to comment.