Created
February 17, 2023 11:40
-
-
Save tomaskavalek/21086ec47c453d220ba5f0b69dce19c9 to your computer and use it in GitHub Desktop.
Sortable solution for DirectoryIterator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class SortableDirectoryIterator implements IteratorAggregate | |
{ | |
private $storage; | |
public function __construct($path) | |
{ | |
$this->storage = new ArrayObject(); | |
$files = new DirectoryIterator($path); | |
foreach ($files as $file) { | |
$this->storage->offsetSet($file->getFilename(), $file->getFileInfo()); | |
} | |
$this->storage->uksort( | |
function ($a, $b) { | |
return strcmp($a, $b); | |
} | |
); | |
} | |
public function getIterator() | |
{ | |
return $this->storage->getIterator(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment