29-03-2022
This commit is contained in:
@@ -234,4 +234,38 @@ function host() {
|
||||
return $host;
|
||||
}
|
||||
|
||||
|
||||
class AdvancedFilesystemIterator extends ArrayIterator
|
||||
{
|
||||
public function __construct(string $path, int $flags = FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS)
|
||||
{
|
||||
parent::__construct(iterator_to_array(new FilesystemIterator($path, $flags)));
|
||||
}
|
||||
|
||||
public function __call(string $name, array $arguments)
|
||||
{
|
||||
if (preg_match('/^sortBy(.*)/', $name, $m)) return $this->sort('get' . $m[1]);
|
||||
throw new MemberAccessException('Method ' . $methodName . ' not exists');
|
||||
}
|
||||
|
||||
public function sort($method)
|
||||
{
|
||||
if (!method_exists('SplFileInfo', $method)) throw new InvalidArgumentException(sprintf('Method "%s" does not exist in SplFileInfo', $method));
|
||||
|
||||
$this->uasort(function(SplFileInfo $a, SplFileInfo $b) use ($method) { return (is_string($a->$method()) ? strnatcmp($a->$method(), $b->$method()) : $b->$method() - $a->$method()); });
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function limit(int $offset = 0, int $limit = -1)
|
||||
{
|
||||
return parent::__construct(iterator_to_array(new LimitIterator($this, $offset, $limit))) ?? $this;
|
||||
}
|
||||
|
||||
public function match(string $regex, int $mode = RegexIterator::MATCH, int $flags = 0, int $preg_flags = 0)
|
||||
{
|
||||
return parent::__construct(iterator_to_array(new RegexIterator($this, $regex, $mode, $flags, $preg_flags))) ?? $this;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user