Skip to content

[Finder] Added a way to inverse a previous sorting #27967

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Finder/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
-----

* added $useNaturalSort option to Finder::sortByName() method
* added `Finder::reverseSorting` to reverse the sorting

4.0.0
-----
Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Component/Finder/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class Finder implements \IteratorAggregate, \Countable
private $depths = array();
private $sizes = array();
private $followLinks = false;
private $reverseSorting = false;
private $sort = false;
private $ignore = 0;
private $dirs = array();
Expand Down Expand Up @@ -460,6 +461,18 @@ public function sortByAccessedTime()
return $this;
}

/**
* Reverses the sorting.
*
* @return $this
*/
public function reverseSorting()
{
$this->reverseSorting = true;

return $this;
}

/**
* Sorts files and directories by the last inode changed time.
*
Expand Down Expand Up @@ -739,6 +752,11 @@ private function searchInDirectory(string $dir): \Iterator
$iterator = $iteratorAggregate->getIterator();
}

if ($this->reverseSorting) {
$iteratorAggregate = new Iterator\ReverseSortingIterator($iterator);
$iterator = $iteratorAggregate->getIterator();
}

return $iterator;
}

Expand Down
32 changes: 32 additions & 0 deletions src/Symfony/Component/Finder/Iterator/ReverseSortingIterator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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;

/**
* Reverse the order of a previous iterator.
*
* @author Grégoire Pineau <lyrixx@lyrixx.info>
*/
class ReverseSortingIterator implements \IteratorAggregate
{
private $iterator;

public function __construct(\Traversable $iterator)
{
$this->iterator = $iterator;
}

public function getIterator()
{
return new \ArrayIterator(array_reverse(iterator_to_array($this->iterator, true)));
}
}
24 changes: 24 additions & 0 deletions src/Symfony/Component/Finder/Tests/FinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,30 @@ public function testSortByModifiedTime()
)), $finder->in(self::$tmpDir)->getIterator());
}

public function testReverseSorting()
{
$finder = $this->buildFinder();
$this->assertSame($finder, $finder->sortByName());
$this->assertSame($finder, $finder->reverseSorting());
$this->assertOrderedIteratorInForeach($this->toAbsolute(array(
'toto',
'test.py',
'test.php',
'qux_2_0.php',
'qux_12_0.php',
'qux_10_2.php',
'qux_1002_0.php',
'qux_1000_1.php',
'qux_0_1.php',
'qux/baz_1_2.py',
'qux/baz_100_1.py',
'qux',
'foo/bar.tmp',
'foo bar',
'foo',
)), $finder->in(self::$tmpDir)->getIterator());
}

public function testSortByNameNatural()
{
$finder = $this->buildFinder();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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\Tests\Iterator;

use Symfony\Component\Finder\Iterator\ReverseSortingIterator;

class ReverseSortingIteratorTest extends IteratorTestCase
{
public function test()
{
$iterator = new ReverseSortingIterator(new MockFileListIterator(array(
'a.txt',
'b.yaml',
'c.php',
)));

$result = iterator_to_array($iterator);
$this->assertCount(3, $iterator);
$this->assertSame('c.php', $result[0]->getFilename());
$this->assertSame('b.yaml', $result[1]->getFilename());
$this->assertSame('a.txt', $result[2]->getFilename());
}
}