| 148 |
lars |
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 |
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 |
* @implements \IteratorAggregate<string, \SplFileInfo>
|
|
|
20 |
*/
|
|
|
21 |
class SortableIterator implements \IteratorAggregate
|
|
|
22 |
{
|
|
|
23 |
public const SORT_BY_NONE = 0;
|
|
|
24 |
public const SORT_BY_NAME = 1;
|
|
|
25 |
public const SORT_BY_TYPE = 2;
|
|
|
26 |
public const SORT_BY_ACCESSED_TIME = 3;
|
|
|
27 |
public const SORT_BY_CHANGED_TIME = 4;
|
|
|
28 |
public const SORT_BY_MODIFIED_TIME = 5;
|
|
|
29 |
public const SORT_BY_NAME_NATURAL = 6;
|
|
|
30 |
public const SORT_BY_NAME_CASE_INSENSITIVE = 7;
|
|
|
31 |
public const SORT_BY_NAME_NATURAL_CASE_INSENSITIVE = 8;
|
|
|
32 |
public const SORT_BY_EXTENSION = 9;
|
|
|
33 |
public const SORT_BY_SIZE = 10;
|
|
|
34 |
|
|
|
35 |
/** @var \Traversable<string, \SplFileInfo> */
|
|
|
36 |
private \Traversable $iterator;
|
|
|
37 |
private \Closure|int $sort;
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* @param \Traversable<string, \SplFileInfo> $iterator
|
|
|
41 |
* @param int|callable $sort The sort type (SORT_BY_NAME, SORT_BY_TYPE, or a PHP callback)
|
|
|
42 |
*
|
|
|
43 |
* @throws \InvalidArgumentException
|
|
|
44 |
*/
|
|
|
45 |
public function __construct(\Traversable $iterator, int|callable $sort, bool $reverseOrder = false)
|
|
|
46 |
{
|
|
|
47 |
$this->iterator = $iterator;
|
|
|
48 |
$order = $reverseOrder ? -1 : 1;
|
|
|
49 |
|
|
|
50 |
if (self::SORT_BY_NAME === $sort) {
|
|
|
51 |
$this->sort = static function (\SplFileInfo $a, \SplFileInfo $b) use ($order) {
|
|
|
52 |
return $order * strcmp($a->getRealPath() ?: $a->getPathname(), $b->getRealPath() ?: $b->getPathname());
|
|
|
53 |
};
|
|
|
54 |
} elseif (self::SORT_BY_NAME_NATURAL === $sort) {
|
|
|
55 |
$this->sort = static function (\SplFileInfo $a, \SplFileInfo $b) use ($order) {
|
|
|
56 |
return $order * strnatcmp($a->getRealPath() ?: $a->getPathname(), $b->getRealPath() ?: $b->getPathname());
|
|
|
57 |
};
|
|
|
58 |
} elseif (self::SORT_BY_NAME_CASE_INSENSITIVE === $sort) {
|
|
|
59 |
$this->sort = static function (\SplFileInfo $a, \SplFileInfo $b) use ($order) {
|
|
|
60 |
return $order * strcasecmp($a->getRealPath() ?: $a->getPathname(), $b->getRealPath() ?: $b->getPathname());
|
|
|
61 |
};
|
|
|
62 |
} elseif (self::SORT_BY_NAME_NATURAL_CASE_INSENSITIVE === $sort) {
|
|
|
63 |
$this->sort = static function (\SplFileInfo $a, \SplFileInfo $b) use ($order) {
|
|
|
64 |
return $order * strnatcasecmp($a->getRealPath() ?: $a->getPathname(), $b->getRealPath() ?: $b->getPathname());
|
|
|
65 |
};
|
|
|
66 |
} elseif (self::SORT_BY_TYPE === $sort) {
|
|
|
67 |
$this->sort = static function (\SplFileInfo $a, \SplFileInfo $b) use ($order) {
|
|
|
68 |
if ($a->isDir() && $b->isFile()) {
|
|
|
69 |
return -$order;
|
|
|
70 |
} elseif ($a->isFile() && $b->isDir()) {
|
|
|
71 |
return $order;
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
return $order * strcmp($a->getRealPath() ?: $a->getPathname(), $b->getRealPath() ?: $b->getPathname());
|
|
|
75 |
};
|
|
|
76 |
} elseif (self::SORT_BY_ACCESSED_TIME === $sort) {
|
|
|
77 |
$this->sort = static function (\SplFileInfo $a, \SplFileInfo $b) use ($order) {
|
|
|
78 |
return $order * ($a->getATime() - $b->getATime());
|
|
|
79 |
};
|
|
|
80 |
} elseif (self::SORT_BY_CHANGED_TIME === $sort) {
|
|
|
81 |
$this->sort = static function (\SplFileInfo $a, \SplFileInfo $b) use ($order) {
|
|
|
82 |
return $order * ($a->getCTime() - $b->getCTime());
|
|
|
83 |
};
|
|
|
84 |
} elseif (self::SORT_BY_MODIFIED_TIME === $sort) {
|
|
|
85 |
$this->sort = static function (\SplFileInfo $a, \SplFileInfo $b) use ($order) {
|
|
|
86 |
return $order * ($a->getMTime() - $b->getMTime());
|
|
|
87 |
};
|
|
|
88 |
} elseif (self::SORT_BY_EXTENSION === $sort) {
|
|
|
89 |
$this->sort = static function (\SplFileInfo $a, \SplFileInfo $b) use ($order) {
|
|
|
90 |
return $order * strnatcmp($a->getExtension(), $b->getExtension());
|
|
|
91 |
};
|
|
|
92 |
} elseif (self::SORT_BY_SIZE === $sort) {
|
|
|
93 |
$this->sort = static function (\SplFileInfo $a, \SplFileInfo $b) use ($order) {
|
|
|
94 |
return $order * ($a->getSize() - $b->getSize());
|
|
|
95 |
};
|
|
|
96 |
} elseif (self::SORT_BY_NONE === $sort) {
|
|
|
97 |
$this->sort = $order;
|
|
|
98 |
} elseif (\is_callable($sort)) {
|
|
|
99 |
$this->sort = $reverseOrder ? static function (\SplFileInfo $a, \SplFileInfo $b) use ($sort) { return -$sort($a, $b); } : $sort(...);
|
|
|
100 |
} else {
|
|
|
101 |
throw new \InvalidArgumentException('The SortableIterator takes a PHP callable or a valid built-in sort algorithm as an argument.');
|
|
|
102 |
}
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
public function getIterator(): \Traversable
|
|
|
106 |
{
|
|
|
107 |
if (1 === $this->sort) {
|
|
|
108 |
return $this->iterator;
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
$array = iterator_to_array($this->iterator, true);
|
|
|
112 |
|
|
|
113 |
if (-1 === $this->sort) {
|
|
|
114 |
$array = array_reverse($array);
|
|
|
115 |
} else {
|
|
|
116 |
uasort($array, $this->sort);
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
return new \ArrayIterator($array);
|
|
|
120 |
}
|
|
|
121 |
}
|