Skip to content

Commit

Permalink
pref: simple sortFileTreeRecursive function
Browse files Browse the repository at this point in the history
  • Loading branch information
Rhilip committed Nov 5, 2023
1 parent 3099d9e commit d5b51d0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
12 changes: 6 additions & 6 deletions src/TorrentFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ public function getFileList()
return $this->parse()['files'];
}

private static function sortFileTreeRecursive(array &$fileTree, $sortByString = false, $sortByFolder = false): array
private static function sortFileTreeRecursive(array &$fileTree, $sortByString = false, $sortByFolder = false)
{
if ($sortByString) {
ksort($fileTree, SORT_NATURAL | SORT_FLAG_CASE);
Expand All @@ -709,14 +709,15 @@ private static function sortFileTreeRecursive(array &$fileTree, $sortByString =
$isoFile = [];
foreach ($fileTree as $key => &$item) {
if (is_array($item)) {
$fileTree[$key] = self::sortFileTreeRecursive($item, $sortByString, $sortByFolder);
self::sortFileTreeRecursive($item, $sortByString, $sortByFolder);
} elseif ($sortByFolder) {
$isoFile[$key] = $item;
unset($fileTree[$key]);
}
}
$fileTree = array_merge($fileTree, $isoFile);
return $fileTree;
if ($sortByFolder && !empty($isoFile)) {
$fileTree = array_merge($fileTree, $isoFile);
}
}

/**
Expand All @@ -726,7 +727,7 @@ private static function sortFileTreeRecursive(array &$fileTree, $sortByString =
* "directory" => [
* "filename2" => 2345
* ],
* "filename1" => 123
* "filename1" => 123 // 123 is file size
* ]
* ]
*/
Expand All @@ -736,7 +737,6 @@ public function getFileTree($sortType = self::FILETREE_SORT_NORMAL)

$sortByString = ($sortType & self::FILETREE_SORT_STRING) === self::FILETREE_SORT_STRING;
$sortByFolder = ($sortType & self::FILETREE_SORT_FOLDER) === self::FILETREE_SORT_FOLDER;

if ($sortByString || $sortByFolder) {
self::sortFileTreeRecursive($fileTree, $sortByString, $sortByFolder);
}
Expand Down
14 changes: 9 additions & 5 deletions tests/TorrentFileTreeSortTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class TorrentFileTreeSortTest extends TestCase

protected function setUp(): void
{
$this->torrent = TorrentFile::load("tests/asserts/test_tree_sort.torrent");
$this->torrent = TorrentFile::load("tests/asserts/test-tree-sort.torrent");
$this->torrent->parse();
}

Expand All @@ -28,7 +28,8 @@ public function testGetFileTreeByParse()
]
]
]),
json_encode($this->torrent->getFileTree()));
json_encode($this->torrent->getFileTree())
);
}

public function testGetFileTreeByString()
Expand All @@ -46,7 +47,8 @@ public function testGetFileTreeByString()
],
]
]),
json_encode($this->torrent->getFileTree(TorrentFile::FILETREE_SORT_STRING)));
json_encode($this->torrent->getFileTree(TorrentFile::FILETREE_SORT_STRING))
);
}

public function testGetFileTreeByFolder()
Expand All @@ -64,7 +66,8 @@ public function testGetFileTreeByFolder()
'file.txt' => 1048576
]
]),
json_encode($this->torrent->getFileTree(TorrentFile::FILETREE_SORT_FOLDER)));
json_encode($this->torrent->getFileTree(TorrentFile::FILETREE_SORT_FOLDER))
);
}

public function testGetFileTreeByNatural()
Expand All @@ -82,6 +85,7 @@ public function testGetFileTreeByNatural()
'file.txt' => 1048576
]
]),
json_encode($this->torrent->getFileTree(TorrentFile::FILETREE_SORT_NATURAL)));
json_encode($this->torrent->getFileTree(TorrentFile::FILETREE_SORT_NATURAL))
);
}
}
File renamed without changes.

0 comments on commit d5b51d0

Please sign in to comment.