Skip to content

Commit

Permalink
fix: current comment of FILETREE_SORT
Browse files Browse the repository at this point in the history
  • Loading branch information
Rhilip committed Nov 4, 2023
1 parent 52eafca commit 284800b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
14 changes: 7 additions & 7 deletions src/TorrentFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]);
Expand All @@ -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;
Expand Down

0 comments on commit 284800b

Please sign in to comment.