Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
SortableIterator.php
01 <?php
02
03 /*
04 * This file is part of the Symfony package.
05 *
06 * (c) Fabien Potencier <fabien@symfony.com>
07 *
08 * For the full copyright and license information, please view the LICENSE
09 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Finder\Iterator;
13
14 /**
15 * SortableIterator applies a sort on a given Iterator.
16 *
17 * @author Fabien Potencier <fabien@symfony.com>
18 */
19 class SortableIterator implements \IteratorAggregate
20 {
21 const SORT_BY_NAME = 1;
22 const SORT_BY_TYPE = 2;
23 const SORT_BY_ACCESSED_TIME = 3;
24 const SORT_BY_CHANGED_TIME = 4;
25 const SORT_BY_MODIFIED_TIME = 5;
26
27 private $iterator;
28 private $sort;
29
30 /**
31 * @param \Traversable $iterator The Iterator to filter
32 * @param int|callable $sort The sort type (SORT_BY_NAME, SORT_BY_TYPE, or a PHP callback)
33 *
34 * @throws \InvalidArgumentException
35 */
36 public function __construct(\Traversable $iterator, $sort)
37 {
38 $this->iterator = $iterator;
39
40 if (self::SORT_BY_NAME === $sort) {
41 $this->sort = static function ($a, $b) {
42 return strcmp($a->getRealPath() ?: $a->getPathname(), $b->getRealPath() ?: $b->getPathname());
43 };
44 } elseif (self::SORT_BY_TYPE === $sort) {
45 $this->sort = static function ($a, $b) {
46 if ($a->isDir() && $b->isFile()) {
47 return -1;
48 } elseif ($a->isFile() && $b->isDir()) {
49 return 1;
50 }
51
52 return strcmp($a->getRealPath() ?: $a->getPathname(), $b->getRealPath() ?: $b->getPathname());
53 };
54 } elseif (self::SORT_BY_ACCESSED_TIME === $sort) {
55 $this->sort = static function ($a, $b) {
56 return $a->getATime() - $b->getATime();
57 };
58 } elseif (self::SORT_BY_CHANGED_TIME === $sort) {
59 $this->sort = static function ($a, $b) {
60 return $a->getCTime() - $b->getCTime();
61 };
62 } elseif (self::SORT_BY_MODIFIED_TIME === $sort) {
63 $this->sort = static function ($a, $b) {
64 return $a->getMTime() - $b->getMTime();
65 };
66 } elseif (\is_callable($sort)) {
67 $this->sort = $sort;
68 } else {
69 throw new \InvalidArgumentException('The SortableIterator takes a PHP callable or a valid built-in sort algorithm as an argument.');
70 }
71 }
72
73 public function getIterator()
74 {
75 $array = iterator_to_array($this->iterator, true);
76 uasort($array, $this->sort);
77
78 return new \ArrayIterator($array);
79 }
80 }
81