diff --git a/README.md b/README.md index ff0b49f..4439c48 100644 --- a/README.md +++ b/README.md @@ -217,10 +217,10 @@ $fileList = $torrent->getFileList(); * - TorrentFile::FILETREE_SORT_NORMAL : not sort, also means sort by torrent file parsed order * - TorrentFile::FILETREE_SORT_STRING : sort by filename ASC ("natural ordering" and "case-insensitively") * - TorrentFile::FILETREE_SORT_FOLDER : sort by filetype (first folder, then file) - * - TorrentFile::FILETREE_SORT_NATURAL: sort by both filetype and filename ( same as `TorrentFile::FILETREE_SORT_NAME | TorrentFile::FILETREE_SORT_FOLDER` ) + * - TorrentFile::FILETREE_SORT_NATURAL: sort by both filetype and filename ( same as `TorrentFile::FILETREE_SORT_STRING | TorrentFile::FILETREE_SORT_FOLDER` ) * */ -$fileTree = $torrent->getFileTree(?$sortType = self::FILETREE_SORT_NORMAL); +$fileTree = $torrent->getFileTree(?$sortType = TorrentFile::FILETREE_SORT_NORMAL); // 6. Other method $torrent->cleanCache(); diff --git a/src/TorrentFile.php b/src/TorrentFile.php index fc02505..df53bc2 100644 --- a/src/TorrentFile.php +++ b/src/TorrentFile.php @@ -40,7 +40,7 @@ class TorrentFile public const FILETREE_SORT_NORMAL = 0x00; public const FILETREE_SORT_STRING = 0x01; public const FILETREE_SORT_FOLDER = 0x10; - public const FILETREE_SORT_NATURAL = 0x11; // same as self::FILETREE_SORT_NAME | self::FILETREE_SORT_FOLDER + public const FILETREE_SORT_NATURAL = 0x11; // same as `self::FILETREE_SORT_STRING | self::FILETREE_SORT_FOLDER` // store torrent dict private $data; @@ -700,16 +700,16 @@ public function getFileList() return $this->parse()['files']; } - private static function sortFileTreeRecursive(array &$fileTree, $sortByName = false, $sortByFolder = false): array + private static function sortFileTreeRecursive(array &$fileTree, $sortByString = false, $sortByFolder = false): array { - if ($sortByName) { + if ($sortByString) { ksort($fileTree, SORT_NATURAL | SORT_FLAG_CASE); } $isoFile = []; foreach ($fileTree as $key => &$item) { if (is_array($item)) { - $fileTree[$key] = self::sortFileTreeRecursive($item, $sortByName, $sortByFolder); + $fileTree[$key] = self::sortFileTreeRecursive($item, $sortByString, $sortByFolder); } else if ($sortByFolder) { $isoFile[$key] = $item; unset($fileTree[$key]); @@ -734,11 +734,11 @@ public function getFileTree($sortType = self::FILETREE_SORT_NORMAL) { $fileTree = $this->parse()['fileTree']; - $sortByName = ($sortType & self::FILETREE_SORT_STRING) === self::FILETREE_SORT_STRING; + $sortByString = ($sortType & self::FILETREE_SORT_STRING) === self::FILETREE_SORT_STRING; $sortByFolder = ($sortType & self::FILETREE_SORT_FOLDER) === self::FILETREE_SORT_FOLDER; - if ($sortByName || $sortByFolder) { - self::sortFileTreeRecursive($fileTree, $sortByName, $sortByFolder); + if ($sortByString || $sortByFolder) { + self::sortFileTreeRecursive($fileTree, $sortByString, $sortByFolder); } return $fileTree;