ILIAS  release_7 Revision v7.30-3-g800a261c036
SortableIterator.php
Go to the documentation of this file.
1<?php
2declare(strict_types=1);
3
5
8
15{
16 const SORT_BY_NONE = 0;
17 const SORT_BY_NAME = 1;
18 const SORT_BY_TYPE = 2;
20 const SORT_BY_TIME = 5;
21
23 private $filesystem;
24
26 private $iterator;
27
29 private $sort;
30
38 public function __construct(Filesystem $filesystem, \Traversable $iterator, $sort, $reverseOrder = false)
39 {
40 $this->filesystem = $filesystem;
41 $this->iterator = $iterator;
42 $order = $reverseOrder ? -1 : 1;
43
44 if (self::SORT_BY_NAME === $sort) {
45 $this->sort = function (Metadata $left, Metadata $right) use ($order) {
46 $leftRealPath = $left->getPath();
47 $rightRealPath = $right->getPath();
48
49 return $order * strcmp($leftRealPath, $rightRealPath);
50 };
51 } elseif (self::SORT_BY_NAME_NATURAL === $sort) {
52 $this->sort = function (Metadata $left, Metadata $right) use ($order) {
53 $leftRealPath = $left->getPath();
54 $rightRealPath = $right->getPath();
55
56 return $order * strnatcmp($leftRealPath, $rightRealPath);
57 };
58 } elseif (self::SORT_BY_TYPE === $sort) {
59 $this->sort = function (Metadata $left, Metadata $right) use ($order) {
60 if ($left->isDir() && $right->isFile()) {
61 return -$order;
62 } elseif ($left->isFile() && $right->isDir()) {
63 return $order;
64 }
65
66 $leftRealPath = $left->getPath();
67 $rightRealPath = $right->getPath();
68
69 return $order * strcmp($leftRealPath, $rightRealPath);
70 };
71 } elseif (self::SORT_BY_TIME === $sort) {
72 $this->sort = function (Metadata $left, Metadata $right) use ($order) {
73 $leftTimestamp = $this->filesystem->getTimestamp($left->getPath());
74 $rightTimestamp = $this->filesystem->getTimestamp($right->getPath());
75
76 return $order * ($leftTimestamp->getTimestamp() - $rightTimestamp->getTimestamp());
77 };
78 } elseif (self::SORT_BY_NONE === $sort) {
79 $this->sort = $order;
80 } elseif (is_callable($sort)) {
81 $this->sort = $sort;
82 if ($reverseOrder) {
83 $this->sort = function (Metadata $left, Metadata $right) use ($sort) {
84 return -$sort($left, $right);
85 };
86 }
87 } else {
88 throw new \InvalidArgumentException('The SortableIterator takes a PHP callable or a valid built-in sort algorithm as an argument.');
89 }
90 }
91
95 public function getIterator()
96 {
97 if (1 === $this->sort) {
98 return $this->iterator;
99 }
100
101 $array = iterator_to_array($this->iterator, true);
102 if (-1 === $this->sort) {
103 $array = array_reverse($array);
104 } else {
105 uasort($array, $this->sort);
106 }
107
108 return new \ArrayIterator($array);
109 }
110}
An exception for terminatinating execution or to throw for unit testing.
Class Metadata This class holds all default metadata send by the filesystem adapters.
Definition: Metadata.php:17
getPath()
The path to the file or directory.
Definition: Metadata.php:53
isFile()
The path is a file.
Definition: Metadata.php:87
isDir()
The path is a directory.
Definition: Metadata.php:77
sort(\Closure $closure)
Sorts files and directories by an anonymous function.
Definition: Finder.php:272
__construct(Filesystem $filesystem, \Traversable $iterator, $sort, $reverseOrder=false)
Sortable constructor.
Interface Filesystem The filesystem interface provides the public interface for the Filesystem servic...
Definition: Filesystem.php:22
Class FlySystemFileAccessTest \Provider\FlySystem @runTestsInSeparateProcesses @preserveGlobalState d...