This repository was archived by the owner on Jul 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFinder.php
86 lines (73 loc) · 1.85 KB
/
Finder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php declare (strict_types = 1);
namespace Wavevision\Utils;
use ArrayIterator;
use Iterator;
use Nette\Utils\Finder as NetteFinder;
use SplFileInfo;
use function is_callable;
use function iterator_to_array;
class Finder extends NetteFinder
{
public const CASE_INSENSITIVE = 'CASE_INSENSITIVE';
public const CASE_SENSITIVE = 'CASE_SENSITIVE';
public const ORDER_ASC = 'ASC';
public const ORDER_DESC = 'DESC';
/**
* @var callable
*/
private $sort;
/**
* @return Iterator<SplFileInfo>
*/
public function getIterator(): Iterator
{
$iterator = parent::getIterator();
if (!is_callable($this->sort)) {
return $iterator;
}
$iterator = new ArrayIterator(iterator_to_array($iterator));
$iterator->uasort($this->sort);
return $iterator;
}
/**
* @return Finder<SplFileInfo>
*/
public function setSort(callable $sort): self
{
$this->sort = $sort;
return $this;
}
/**
* @return Finder<SplFileInfo>
*/
public function sortByMTime(string $order = self::ORDER_DESC): self
{
$this->sort = function (SplFileInfo $f1, SplFileInfo $f2) use ($order): int {
if ($order === self::ORDER_DESC) {
return $f2->getMTime() - $f1->getMTime();
}
return $f1->getMTime() - $f2->getMTime();
};
return $this;
}
/**
* @return Finder<SplFileInfo>
*/
public function sortByName(string $order = self::ORDER_ASC, string $case = self::CASE_INSENSITIVE): self
{
$fn = $case === self::CASE_INSENSITIVE ? 'strcasecmp' : 'strcmp';
$this->sort = function (SplFileInfo $f1, SplFileInfo $f2) use ($fn, $order): int {
if ($order === self::ORDER_ASC) {
return $fn(
Strings::removeAccentedChars($f1->getFilename()),
Strings::removeAccentedChars($f2->getFilename())
);
}
return $fn(
Strings::removeAccentedChars($f2->getFilename()),
Strings::removeAccentedChars($f1->getFilename())
);
};
return $this;
}
}