From 52c8080b2ea1021a521004f5b1d117bf5dc52098 Mon Sep 17 00:00:00 2001 From: KoKsPfLaNzE Date: Mon, 29 Feb 2016 21:32:39 +0100 Subject: [PATCH 1/4] bugfix for Type\Number filter with comma also run php-cs-fixer --- src/ZfcDatagrid/Filter.php | 42 ++++++++++++------- .../Renderer/JqGrid/View/Helper/Columns.php | 2 +- tests/ZfcDatagridTest/FilterTest.php | 22 ++++++++++ .../JqGrid/View/Helper/ColumnsTest.php | 8 ++-- 4 files changed, 53 insertions(+), 21 deletions(-) diff --git a/src/ZfcDatagrid/Filter.php b/src/ZfcDatagrid/Filter.php index 4b6bda39..9fc46e73 100644 --- a/src/ZfcDatagrid/Filter.php +++ b/src/ZfcDatagrid/Filter.php @@ -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; @@ -122,18 +122,22 @@ 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; } @@ -141,23 +145,28 @@ private function setColumnOperator($inputFilterValue, $defaultOperator = self::L // 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; } @@ -196,9 +205,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); } diff --git a/src/ZfcDatagrid/Renderer/JqGrid/View/Helper/Columns.php b/src/ZfcDatagrid/Renderer/JqGrid/View/Helper/Columns.php index fdd099d1..1b3ecae4 100644 --- a/src/ZfcDatagrid/Renderer/JqGrid/View/Helper/Columns.php +++ b/src/ZfcDatagrid/Renderer/JqGrid/View/Helper/Columns.php @@ -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; } diff --git a/tests/ZfcDatagridTest/FilterTest.php b/tests/ZfcDatagridTest/FilterTest.php index 17302f9f..355e33a0 100644 --- a/tests/ZfcDatagridTest/FilterTest.php +++ b/tests/ZfcDatagridTest/FilterTest.php @@ -2,6 +2,7 @@ namespace ZfcDatagridTest; use PHPUnit_Framework_TestCase; +use ZfcDatagrid\Column\Type\Number; use ZfcDatagrid\Filter; /** @@ -9,6 +10,7 @@ */ class FilterTest extends PHPUnit_Framework_TestCase { + /** @var \ZfcDatagrid\Column\AbstractColumn|\PHPUnit_Framework_MockObject_MockObject */ private $column; public function setUp() @@ -385,6 +387,26 @@ 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() + { + $this->column->setType(new Number()); + + $filter = new Filter(); + $filter->setFromColumn($this->column, '=2,5'); + + $this->assertEquals(Filter::EQUAL, $filter->getOperator()); + $this->assertEquals([2.5], $filter->getValues()); + } + public function testIsApplyLike() { $filter = new Filter(); diff --git a/tests/ZfcDatagridTest/Renderer/JqGrid/View/Helper/ColumnsTest.php b/tests/ZfcDatagridTest/Renderer/JqGrid/View/Helper/ColumnsTest.php index 9ba37adb..294043b7 100644 --- a/tests/ZfcDatagridTest/Renderer/JqGrid/View/Helper/ColumnsTest.php +++ b/tests/ZfcDatagridTest/Renderer/JqGrid/View/Helper/ColumnsTest.php @@ -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']); @@ -256,7 +256,7 @@ public function testTranslateWithMockedTranslator() // Configure the stub. $translator->method('translate') ->will($this->returnValueMap([ - ['test', 'default', null, 'translate'] + ['test', 'default', null, 'translate'], ])); $sm->setService('translator', $translator); @@ -264,13 +264,11 @@ public function testTranslateWithMockedTranslator() $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); } - - } From 29fdcb35aa010c1901d22cfb76eb21b04431e409 Mon Sep 17 00:00:00 2001 From: KoKsPfLaNzE Date: Mon, 29 Feb 2016 21:35:21 +0100 Subject: [PATCH 2/4] cs change --- src/ZfcDatagrid/Filter.php | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/ZfcDatagrid/Filter.php b/src/ZfcDatagrid/Filter.php index 9fc46e73..a3173af0 100644 --- a/src/ZfcDatagrid/Filter.php +++ b/src/ZfcDatagrid/Filter.php @@ -122,12 +122,10 @@ 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); @@ -145,9 +143,7 @@ private function setColumnOperator($inputFilterValue, $defaultOperator = self::L // 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) == '~') { @@ -155,8 +151,7 @@ private function setColumnOperator($inputFilterValue, $defaultOperator = self::L } $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); From cf1d9b5fb9a7d17bd95f4d421e31fbc88f555319 Mon Sep 17 00:00:00 2001 From: KoKsPfLaNzE Date: Mon, 29 Feb 2016 21:37:20 +0100 Subject: [PATCH 3/4] phpcs-fix --- src/ZfcDatagrid/Filter.php | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/ZfcDatagrid/Filter.php b/src/ZfcDatagrid/Filter.php index a3173af0..6c353dbd 100644 --- a/src/ZfcDatagrid/Filter.php +++ b/src/ZfcDatagrid/Filter.php @@ -122,11 +122,9 @@ 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); @@ -143,16 +141,14 @@ private function setColumnOperator($inputFilterValue, $defaultOperator = self::L // 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); From ca7d467889aa614f826891a46cd8a102aa334f98 Mon Sep 17 00:00:00 2001 From: KoKsPfLaNzE Date: Mon, 29 Feb 2016 23:42:25 +0100 Subject: [PATCH 4/4] fixed problem with intl, we have to set a local for the test --- tests/ZfcDatagridTest/FilterTest.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/ZfcDatagridTest/FilterTest.php b/tests/ZfcDatagridTest/FilterTest.php index 355e33a0..79088e0a 100644 --- a/tests/ZfcDatagridTest/FilterTest.php +++ b/tests/ZfcDatagridTest/FilterTest.php @@ -398,13 +398,16 @@ public function testIsArrayComma() public function testIsArrayCommaWithNumber() { - $this->column->setType(new Number()); + $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->assertEquals([2.5], $filter->getValues()); + $this->assertSame(['2,5'], $filter->getValues()); } public function testIsApplyLike()