| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <[email protected]> |
| 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 <[email protected]> |
| 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 fn (\SplFileInfo $a, \SplFileInfo $b) => $order * strcmp($a->getRealPath() ?: $a->getPathname(), $b->getRealPath() ?: $b->getPathname()); |
| 52 |
} elseif (self::SORT_BY_NAME_NATURAL === $sort) { |
| 53 |
$this->sort = static fn (\SplFileInfo $a, \SplFileInfo $b) => $order * strnatcmp($a->getRealPath() ?: $a->getPathname(), $b->getRealPath() ?: $b->getPathname()); |
| 54 |
} elseif (self::SORT_BY_NAME_CASE_INSENSITIVE === $sort) { |
| 55 |
$this->sort = static fn (\SplFileInfo $a, \SplFileInfo $b) => $order * strcasecmp($a->getRealPath() ?: $a->getPathname(), $b->getRealPath() ?: $b->getPathname()); |
| 56 |
} elseif (self::SORT_BY_NAME_NATURAL_CASE_INSENSITIVE === $sort) { |
| 57 |
$this->sort = static fn (\SplFileInfo $a, \SplFileInfo $b) => $order * strnatcasecmp($a->getRealPath() ?: $a->getPathname(), $b->getRealPath() ?: $b->getPathname()); |
| 58 |
} elseif (self::SORT_BY_TYPE === $sort) { |
| 59 |
$this->sort = static function (\SplFileInfo $a, \SplFileInfo $b) use ($order) { |
| 60 |
if ($a->isDir() && $b->isFile()) { |
| 61 |
return -$order; |
| 62 |
} elseif ($a->isFile() && $b->isDir()) { |
| 63 |
return $order; |
| 64 |
} |
| 65 |
|
| 66 |
return $order * strcmp($a->getRealPath() ?: $a->getPathname(), $b->getRealPath() ?: $b->getPathname()); |
| 67 |
}; |
| 68 |
} elseif (self::SORT_BY_ACCESSED_TIME === $sort) { |
| 69 |
$this->sort = static fn (\SplFileInfo $a, \SplFileInfo $b) => $order * ($a->getATime() - $b->getATime()); |
| 70 |
} elseif (self::SORT_BY_CHANGED_TIME === $sort) { |
| 71 |
$this->sort = static fn (\SplFileInfo $a, \SplFileInfo $b) => $order * ($a->getCTime() - $b->getCTime()); |
| 72 |
} elseif (self::SORT_BY_MODIFIED_TIME === $sort) { |
| 73 |
$this->sort = static fn (\SplFileInfo $a, \SplFileInfo $b) => $order * ($a->getMTime() - $b->getMTime()); |
| 74 |
} elseif (self::SORT_BY_EXTENSION === $sort) { |
| 75 |
$this->sort = static fn (\SplFileInfo $a, \SplFileInfo $b) => $order * strnatcmp($a->getExtension(), $b->getExtension()); |
| 76 |
} elseif (self::SORT_BY_SIZE === $sort) { |
| 77 |
$this->sort = static fn (\SplFileInfo $a, \SplFileInfo $b) => $order * ($a->getSize() - $b->getSize()); |
| 78 |
} elseif (self::SORT_BY_NONE === $sort) { |
| 79 |
$this->sort = $order; |
| 80 |
} elseif (\is_callable($sort)) { |
| 81 |
$this->sort = $reverseOrder ? static fn (\SplFileInfo $a, \SplFileInfo $b) => -$sort($a, $b) : $sort(...); |
| 82 |
} else { |
| 83 |
throw new \InvalidArgumentException('The SortableIterator takes a PHP callable or a valid built-in sort algorithm as an argument.'); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
public function getIterator(): \Traversable |
| 88 |
{ |
| 89 |
if (1 === $this->sort) { |
| 90 |
yield from $this->iterator; |
| 91 |
|
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
$keys = $values = []; |
| 96 |
foreach ($this->iterator as $key => $value) { |
| 97 |
$keys[] = $key; |
| 98 |
$values[] = $value; |
| 99 |
} |
| 100 |
|
| 101 |
if (-1 === $this->sort) { |
| 102 |
for ($i = \count($values) - 1; $i >= 0; --$i) { |
| 103 |
yield $keys[$i] => $values[$i]; |
| 104 |
} |
| 105 |
|
| 106 |
return; |
| 107 |
} |
| 108 |
|
| 109 |
uasort($values, $this->sort); |
| 110 |
|
| 111 |
foreach ($values as $i => $v) { |
| 112 |
yield $keys[$i] => $v; |
| 113 |
} |
| 114 |
} |
| 115 |
} |