ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
SortableIterator.php
Go to the documentation of this file.
1 <?php
2 declare(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 }
getPath()
The path to the file or directory.
Definition: Metadata.php:62
isDir()
The path is a directory.
Definition: Metadata.php:92
isFile()
The path is a file.
Definition: Metadata.php:104
sort(\Closure $closure)
Sorts files and directories by an anonymous function.
Definition: Finder.php:278
Class FlySystemFileAccessTest.
__construct(Filesystem $filesystem, \Traversable $iterator, $sort, $reverseOrder=false)
Sortable constructor.