Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mattab committed Dec 5, 2014
1 parent 7ab9fdd commit 3d8f3ce
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
48 changes: 18 additions & 30 deletions core/DataTable/Filter/Sort.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,8 @@ public function setOrder($order)
*/
public function numberSort($a, $b)
{
$valA = $a->getColumn($this->columnToSort);
$valB = $b->getColumn($this->columnToSort);

if ($valA === false) {
$valA = null;
}

if ($valB === false) {
$valB = null;
}
$valA = $this->getColumnValue($a);
$valB = $this->getColumnValue($b);

return !isset($valA)
&& !isset($valB)
Expand Down Expand Up @@ -118,16 +110,8 @@ public function numberSort($a, $b)
*/
function naturalSort($a, $b)
{
$valA = $a->getColumn($this->columnToSort);
$valB = $b->getColumn($this->columnToSort);

if ($valA === false) {
$valA = null;
}

if ($valB === false) {
$valB = null;
}
$valA = $this->getColumnValue($a);
$valB = $this->getColumnValue($b);

return !isset($valA)
&& !isset($valB)
Expand All @@ -153,16 +137,8 @@ function naturalSort($a, $b)
*/
function sortString($a, $b)
{
$valA = $a->getColumn($this->columnToSort);
$valB = $b->getColumn($this->columnToSort);

if ($valA === false) {
$valA = null;
}

if ($valB === false) {
$valB = null;
}
$valA = $this->getColumnValue($a);
$valB = $this->getColumnValue($b);

return !isset($valA)
&& !isset($valB)
Expand All @@ -179,6 +155,18 @@ function sortString($a, $b)
);
}

protected function getColumnValue(Row $table )
{
$value = $table->getColumn($this->columnToSort);

if ($value === false
|| is_array($value)
) {
return null;
}
return $value;
}

/**
* Sets the column to be used for sorting
*
Expand Down
19 changes: 19 additions & 0 deletions tests/PHPUnit/Unit/DataTable/Filter/SortTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
use Piwik\DataTable;
use Piwik\DataTable\Row;

/**
* @group SortTest
*/
class DataTable_Filter_SortTest extends \PHPUnit_Framework_TestCase
{
/**
Expand Down Expand Up @@ -169,4 +172,20 @@ public function testFilterSortNumeric()
$filter->filter($table);
$this->assertTrue(DataTable::isEqual($table, $expectedtableReverse));
}

public function test_sortingArrayValues_doesNotError()
{
$table = new DataTable();
$table->addRowsFromArray(array(
array(Row::COLUMNS => array('label' => 'ask', 'count_array' => array(100, 1, 2) )),
array(Row::COLUMNS => array('label' => 'nintendo', 'count_array' => array(0, 'hello'))),
array(Row::COLUMNS => array('label' => 'yahoo', 'count_array' => array(10, 'test'))
)));

$tableOriginal = clone $table;

$filter = new Sort($table, 'count_array', 'desc');
$filter->filter($table);
$this->assertTrue(DataTable::isEqual($tableOriginal, $table));
}
}

0 comments on commit 3d8f3ce

Please sign in to comment.