public static function printTorrentsTable(array $torrentList, OutputInterface $output, $sortColumnNumber = 1, $limit = 0) { $data = self::buildTableData($torrentList); $data['rows'] = TableUtils::sortRowsByColumnNumber($data['rows'], $sortColumnNumber); $data['rows'] = TableUtils::limitRows($data['rows'], $limit); $data['totals'] = ['Total: ' . count($data['rows']), '', '', self::sumArrayField($data['rows'], 3), self::sumArrayField($data['rows'], 4), '']; TableUtils::printTable($data, $output); }
public function testSortRowsByColumnNumber() { $data = TorrentListUtils::buildTableData($this->expectedTorrentList); $rows = $data['rows']; $sortedRows = TableUtils::sortRowsByColumnNumber($rows, 2); $sortedIds = TorrentListUtils::getArrayField($sortedRows, 1); $this->assertEquals([1, 2, 3, 4], $sortedIds); $sortedRows = TableUtils::sortRowsByColumnNumber($rows, -2); $sortedIds = TorrentListUtils::getArrayField($sortedRows, 1); $this->assertEquals([4, 3, 2, 1], $sortedIds); }
private function buildTableData(array $torrentList, $sort, $limit) { $rows = []; foreach ($torrentList as $torrent) { $rows[] = [$torrent[Torrent\Get::NAME], $torrent[Torrent\Get::ID], $torrent['age'], TorrentUtils::getSizeInGb($torrent[Torrent\Get::TOTAL_SIZE]), TorrentUtils::getSizeInGb($torrent['uploaded']), $torrent['per_day'], $torrent['profit']]; } $rows = TableUtils::sortRowsByColumnNumber($rows, $sort); $rows = TableUtils::limitRows($rows, $limit); return ['headers' => ['Name', 'Id', 'Age, days', 'Size, GB', 'Uploaded, GB', 'Per day, GB', 'Profit, %'], 'rows' => $rows, 'totals' => ['Total: ' . count($rows), '', '', TorrentListUtils::sumArrayField($rows, 3), TorrentListUtils::sumArrayField($rows, 4), TorrentListUtils::sumArrayField($rows, 5), TorrentListUtils::sumArrayField($rows, 6)]]; }