2 declare(strict_types=1);
42 $order = $reverseOrder ? -1 : 1;
44 if (self::SORT_BY_NAME ===
$sort) {
46 $leftRealPath = $left->
getPath();
47 $rightRealPath = $right->getPath();
49 return $order * strcmp($leftRealPath, $rightRealPath);
51 } elseif (self::SORT_BY_NAME_NATURAL ===
$sort) {
53 $leftRealPath = $left->
getPath();
54 $rightRealPath = $right->getPath();
56 return $order * strnatcmp($leftRealPath, $rightRealPath);
58 } elseif (self::SORT_BY_TYPE ===
$sort) {
60 if ($left->
isDir() && $right->isFile()) {
62 } elseif ($left->
isFile() && $right->isDir()) {
66 $leftRealPath = $left->
getPath();
67 $rightRealPath = $right->getPath();
69 return $order * strcmp($leftRealPath, $rightRealPath);
71 } elseif (self::SORT_BY_TIME ===
$sort) {
73 $leftTimestamp = $this->filesystem->getTimestamp($left->
getPath());
74 $rightTimestamp = $this->filesystem->getTimestamp($right->getPath());
76 return $order * ($leftTimestamp->getTimestamp() - $rightTimestamp->getTimestamp());
78 } elseif (self::SORT_BY_NONE ===
$sort) {
80 } elseif (is_callable(
$sort)) {
84 return -
$sort($left, $right);
88 throw new \InvalidArgumentException(
'The SortableIterator takes a PHP callable or a valid built-in sort algorithm as an argument.');
97 if (1 === $this->
sort) {
101 $array = iterator_to_array($this->iterator,
true);
102 if (-1 === $this->
sort) {
103 $array = array_reverse($array);
105 uasort($array, $this->
sort);
108 return new \ArrayIterator($array);
const SORT_BY_NAME_NATURAL
sort(\Closure $closure)
Sorts files and directories by an anonymous function.
Class FlySystemFileAccessTest.
__construct(Filesystem $filesystem, \Traversable $iterator, $sort, $reverseOrder=false)
Sortable constructor.