Skip to content

Commit

Permalink
Merge pull request #229 from kokspflanze/bugfix/type_number_filter
Browse files Browse the repository at this point in the history
bugfix for Type\Number filter with comma
  • Loading branch information
ThaDafinser committed Mar 1, 2016
2 parents a45e4d3 + ca7d467 commit 280ae8c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 21 deletions.
33 changes: 18 additions & 15 deletions src/ZfcDatagrid/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,14 @@ private function setColumnOperator($inputFilterValue, $defaultOperator = self::L
if (substr($inputFilterValue, 0, 2) == '=(') {
$operator = self::IN;
$value = substr($inputFilterValue, 2);
if (substr($value, - 1) == ')') {
$value = substr($value, 0, - 1);
if (substr($value, -1) == ')') {
$value = substr($value, 0, -1);
}
} elseif (substr($inputFilterValue, 0, 3) == '!=(') {
$operator = self::NOT_IN;
$value = substr($inputFilterValue, 3);
if (substr($value, - 1) == ')') {
$value = substr($value, 0, - 1);
if (substr($value, -1) == ')') {
$value = substr($value, 0, -1);
}
} elseif (substr($inputFilterValue, 0, 2) == '!=' || substr($inputFilterValue, 0, 2) == '<>') {
$operator = self::NOT_EQUAL;
Expand All @@ -122,42 +122,42 @@ private function setColumnOperator($inputFilterValue, $defaultOperator = self::L
$value = trim(substr($inputFilterValue, 1));
}

if (substr($inputFilterValue, 0, 2) == '!~' || (substr($value, 0, 1) == '%' || substr($value, - 1) == '%' || substr($value, 0, 1) == '*' || substr($value, - 1) == '*')) {
if (substr($inputFilterValue, 0, 2) == '!~' || (substr($value, 0, 1) == '%' || substr($value, -1) == '%' || substr($value, 0, 1) == '*' || substr($value, -1) == '*')) {
// NOT LIKE
if ((substr($value, 0, 1) == '*' && substr($value, - 1) == '*') || (substr($value, 0, 1) == '%' && substr($value, - 1) == '%')) {
if ((substr($value, 0, 1) == '*' && substr($value, -1) == '*') || (substr($value, 0, 1) == '%' && substr($value, -1) == '%')) {
$operator = self::NOT_LIKE;
$value = substr($value, 1);
$value = substr($value, 0, - 1);
$value = substr($value, 0, -1);
} elseif (substr($value, 0, 1) == '*' || substr($value, 0, 1) == '%') {
$operator = self::NOT_LIKE_LEFT;
$value = substr($value, 1);
} elseif (substr($value, - 1) == '*' || substr($value, - 1) == '%') {
} elseif (substr($value, -1) == '*' || substr($value, -1) == '%') {
$operator = self::NOT_LIKE_RIGHT;
$value = substr($value, 0, - 1);
$value = substr($value, 0, -1);
} else {
$operator = self::NOT_LIKE;
}
} else {
// NOT EQUAL
$operator = self::NOT_EQUAL;
}
} elseif (substr($inputFilterValue, 0, 1) == '~' || substr($inputFilterValue, 0, 1) == '%' || substr($inputFilterValue, - 1) == '%' || substr($inputFilterValue, 0, 1) == '*' || substr($inputFilterValue, - 1) == '*') {
} elseif (substr($inputFilterValue, 0, 1) == '~' || substr($inputFilterValue, 0, 1) == '%' || substr($inputFilterValue, -1) == '%' || substr($inputFilterValue, 0, 1) == '*' || substr($inputFilterValue, -1) == '*') {
// LIKE
if (substr($inputFilterValue, 0, 1) == '~') {
$value = substr($inputFilterValue, 1);
}
$value = trim($value);

if ((substr($value, 0, 1) == '*' && substr($value, - 1) == '*') || (substr($value, 0, 1) == '%' && substr($value, - 1) == '%')) {
if ((substr($value, 0, 1) == '*' && substr($value, -1) == '*') || (substr($value, 0, 1) == '%' && substr($value, -1) == '%')) {
$operator = self::LIKE;
$value = substr($value, 1);
$value = substr($value, 0, - 1);
$value = substr($value, 0, -1);
} elseif (substr($value, 0, 1) == '*' || substr($value, 0, 1) == '%') {
$operator = self::LIKE_LEFT;
$value = substr($value, 1);
} elseif (substr($value, - 1) == '*' || substr($value, - 1) == '%') {
} elseif (substr($value, -1) == '*' || substr($value, -1) == '%') {
$operator = self::LIKE_RIGHT;
$value = substr($value, 0, - 1);
$value = substr($value, 0, -1);
} else {
$operator = self::LIKE;
}
Expand Down Expand Up @@ -196,9 +196,12 @@ private function setColumnOperator($inputFilterValue, $defaultOperator = self::L
$columnType = $this->getColumn()->getType();
if ($columnType instanceof Column\Type\DateTime && $columnType->isDaterangePickerEnabled() === true) {
$value = explode(' - ', $value);
} elseif (! is_array($value)) {
} elseif (!$columnType instanceof Column\Type\Number && !is_array($value)) {
$value = explode(',', $value);
} elseif (!is_array($value)) {
$value = [$value];
}

foreach ($value as &$val) {
$val = trim($val);
}
Expand Down
2 changes: 1 addition & 1 deletion src/ZfcDatagrid/Renderer/JqGrid/View/Helper/Columns.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function __invoke(array $columns)
foreach ($column->getStyles() as $style) {
/** @var \ZfcDatagrid\Column\Style\Align $style */
if (get_class($style) == 'ZfcDatagrid\Column\Style\Align') {
$options['align'] = $style->getAlignment();
$options['align'] = $style->getAlignment();
$alignAlreadyDefined = true;
break;
}
Expand Down
25 changes: 25 additions & 0 deletions tests/ZfcDatagridTest/FilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
namespace ZfcDatagridTest;

use PHPUnit_Framework_TestCase;
use ZfcDatagrid\Column\Type\Number;
use ZfcDatagrid\Filter;

/**
* @covers ZfcDatagrid\Filter
*/
class FilterTest extends PHPUnit_Framework_TestCase
{
/** @var \ZfcDatagrid\Column\AbstractColumn|\PHPUnit_Framework_MockObject_MockObject */
private $column;

public function setUp()
Expand Down Expand Up @@ -385,6 +387,29 @@ public function testBetween()
], $filter->getValues());
}

public function testIsArrayComma()
{
$filter = new Filter();
$filter->setFromColumn($this->column, '=2,5');

$this->assertEquals(Filter::EQUAL, $filter->getOperator());
$this->assertEquals([2, 5], $filter->getValues());
}

public function testIsArrayCommaWithNumber()
{
$number = new Number();
$number->setLocale('en');

$this->column->setType($number);

$filter = new Filter();
$filter->setFromColumn($this->column, '=2,5');

$this->assertEquals(Filter::EQUAL, $filter->getOperator());
$this->assertSame(['2,5'], $filter->getValues());
}

public function testIsApplyLike()
{
$filter = new Filter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ public function testTranslate()
$helper->setServiceLocator($sm);

$reflection = new \ReflectionClass($helper);
$method = $reflection->getMethod('translate');
$method = $reflection->getMethod('translate');
$method->setAccessible(true);

$result = $method->invokeArgs($helper, ['test']);
Expand All @@ -256,21 +256,19 @@ public function testTranslateWithMockedTranslator()
// Configure the stub.
$translator->method('translate')
->will($this->returnValueMap([
['test', 'default', null, 'translate']
['test', 'default', null, 'translate'],
]));

$sm->setService('translator', $translator);
$helper = new Helper\Columns();
$helper->setServiceLocator($sm);

$reflection = new \ReflectionClass($helper);
$method = $reflection->getMethod('translate');
$method = $reflection->getMethod('translate');
$method->setAccessible(true);

$result = $method->invokeArgs($helper, ['test']);

$this->assertEquals('translate', $result);
}


}

0 comments on commit 280ae8c

Please sign in to comment.