Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit36b151f

Browse files
committed
[Finder] Added a way to inverse a previous sorting
Sometimes, it's useful to inverse the previous sorting.For exemple when you want to display the most recent uploaded files
1 parent18ba2a8 commit36b151f

File tree

5 files changed

+99
-0
lines changed

5 files changed

+99
-0
lines changed

‎src/Symfony/Component/Finder/CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
-----
66

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

910
4.0.0
1011
-----

‎src/Symfony/Component/Finder/Finder.php‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class Finder implements \IteratorAggregate, \Countable
4848
private$depths =array();
4949
private$sizes =array();
5050
private$followLinks =false;
51+
private$reverseSorting =false;
5152
private$sort =false;
5253
private$ignore =0;
5354
private$dirs =array();
@@ -460,6 +461,18 @@ public function sortByAccessedTime()
460461
return$this;
461462
}
462463

464+
/**
465+
* Reverse the sorting.
466+
*
467+
* @return $this
468+
*/
469+
publicfunctionreverseSorting()
470+
{
471+
$this->reverseSorting =true;
472+
473+
return$this;
474+
}
475+
463476
/**
464477
* Sorts files and directories by the last inode changed time.
465478
*
@@ -739,6 +752,10 @@ private function searchInDirectory(string $dir): \Iterator
739752
$iterator =$iteratorAggregate->getIterator();
740753
}
741754

755+
if ($this->reverseSorting) {
756+
$iterator =newIterator\ReverseSortingIterator($iterator);
757+
}
758+
742759
return$iterator;
743760
}
744761

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\Finder\Iterator;
13+
14+
/**
15+
* Reverse the order of a previous iterator.
16+
*
17+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
18+
*/
19+
class ReverseSortingIteratorextends \ArrayIterator
20+
{
21+
publicfunction__construct(iterable$iterator)
22+
{
23+
parent::__construct(array_reverse(iterator_to_array($iterator)));
24+
}
25+
}

‎src/Symfony/Component/Finder/Tests/FinderTest.php‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,30 @@ public function testSortByModifiedTime()
611611
)),$finder->in(self::$tmpDir)->getIterator());
612612
}
613613

614+
publicfunctiontestReverseSorting()
615+
{
616+
$finder =$this->buildFinder();
617+
$this->assertSame($finder,$finder->sortByName());
618+
$this->assertSame($finder,$finder->reverseSorting());
619+
$this->assertOrderedIteratorInForeach($this->toAbsolute(array(
620+
'toto',
621+
'test.py',
622+
'test.php',
623+
'qux_2_0.php',
624+
'qux_12_0.php',
625+
'qux_10_2.php',
626+
'qux_1002_0.php',
627+
'qux_1000_1.php',
628+
'qux_0_1.php',
629+
'qux/baz_1_2.py',
630+
'qux/baz_100_1.py',
631+
'qux',
632+
'foo/bar.tmp',
633+
'foo bar',
634+
'foo',
635+
)),$finder->in(self::$tmpDir)->getIterator());
636+
}
637+
614638
publicfunctiontestSortByNameNatural()
615639
{
616640
$finder =$this->buildFinder();
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\Finder\Tests\Iterator;
13+
14+
useSymfony\Component\Finder\Iterator\ReverseSortingIterator;
15+
16+
class ReverseSortingIteratorTestextends IteratorTestCase
17+
{
18+
publicfunctiontest()
19+
{
20+
$iterator =newReverseSortingIterator(newMockFileListIterator(array(
21+
'a.txt',
22+
'b.yaml',
23+
'c.php',
24+
)));
25+
26+
$result =iterator_to_array($iterator);
27+
$this->assertCount(3,$iterator);
28+
$this->assertSame('c.php',$result[0]->getFilename());
29+
$this->assertSame('b.yaml',$result[1]->getFilename());
30+
$this->assertSame('a.txt',$result[2]->getFilename());
31+
}
32+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp