Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #27967 [Finder] Added a way to inverse a previous sorting (ly…
…rixx) This PR was merged into the 4.2-dev branch. Discussion ---------- [Finder] Added a way to inverse a previous sorting | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | --- Sometimes, it's useful to inverse the previous sorting. For exemple when you want to display the most recent uploaded files Commits ------- 3cd0dca [Finder] Added a way to inverse a previous sorting
- Loading branch information
Showing
5 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/Symfony/Component/Finder/Iterator/ReverseSortingIterator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/Symfony/Component/Finder/Tests/Iterator/ReverseSortingIteratorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |