Skip to content

Commit

Permalink
fix: torrent-stat and torrent-list correct totals when limit
Browse files Browse the repository at this point in the history
Now limit and sort made before count totals.

Closes #21
  • Loading branch information
popstas committed Mar 7, 2016
1 parent 6e7f584 commit 9ff4ca4
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/Helpers/TableUtils.php
Expand Up @@ -50,15 +50,20 @@ private static function parseFilter($type, $filterString)
throw new \InvalidArgumentException('Unknown filter type');
}

public static function filterRows(array $rows, $filters)
/**
* @param array $rows
* @param array $filters
* @return bool
* @throws \RuntimeException
*/
public static function filterRows(array $rows, array $filters)
{
$filters = self::parseFilters($filters);

return array_filter($rows, function ($row) use ($filters) {
foreach ($filters as $columnKey => $filter) {
if (!isset($row[$columnKey])) {
throw new \RuntimeException('Column ' . $columnKey . ' not exists, cannot filter');
continue;
}
$columnValue = $row[$columnKey];

Expand Down Expand Up @@ -102,14 +107,16 @@ public static function sortRowsByColumnNumber(array $rows, $columnNumber)
return $rowsSorted;
}

public static function printTable(array $tableData, OutputInterface $output, $sortColumnNumber = 1, $limit = 0)
public static function limitRows(array $rows, $limit)
{
$tableData['rows'] = self::sortRowsByColumnNumber($tableData['rows'], $sortColumnNumber);

if ($limit && $limit < count($tableData['rows'])) {
$tableData['rows'] = array_slice($tableData['rows'], 0, $limit);
if ($limit && $limit < count($rows)) {
$rows = array_slice($rows, 0, $limit);
}
return $rows;
}

public static function printTable(array $tableData, OutputInterface $output)
{
$table = new Table($output);
$table->setHeaders($tableData['headers']);
$table->setRows($tableData['rows']);
Expand Down

0 comments on commit 9ff4ca4

Please sign in to comment.