From 6d36f395e3cc05675e73b4f97effeef3c3736b5d Mon Sep 17 00:00:00 2001 From: Martin Kiesel Date: Mon, 19 Sep 2016 20:43:42 +0200 Subject: [PATCH] Fix #38, sortablelink directive update @sortablelink directive accepts 3rd parameter, 3rd parameter must be an array and it will be appended to generated link. --- README.md | 11 +++++++---- src/ColumnSortable/SortableLink.php | 8 ++++++-- tests/SortableLinkTest.php | 27 +++++++++++++++++++-------- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index beda292..308034b 100755 --- a/README.md +++ b/README.md @@ -102,18 +102,21 @@ You're set to go. There is one blade extension for you to use **@sortablelink()** ``` -@sortablelink('column', 'Title') +@sortablelink('column', 'Title', ['parameter' => 'smile']) ``` -**Column** (1st) parameter is `order by` and **Title** (2nd) parameter is displayed inside anchor tags. -You can omit **Title** parameter. +**Column** (1st) parameter is `order by`, **Title** (2nd) parameter is displayed inside anchor tags and `array()` parameter (3rd) is default (GET) query strings parameter. + +You can omit 2nd and 3rd parameter. Possible examples and usages of blade extension: + ``` @sortablelink('name') @sortablelink('name', 'Username') -@sortablelink('address', trans('fields.address')) +@sortablelink('address', trans('fields.address'), ['filter' => 'actve,visible']) ``` + If you do not fill **Title** (2nd parameter) column name is used instead. > **Note**: you can set default formatting function that is applied on **Title** (2nd parameter), by default this is set to [`ucfirst`](http://php.net/manual/en/function.ucfirst.php). diff --git a/src/ColumnSortable/SortableLink.php b/src/ColumnSortable/SortableLink.php index fcce536..0d562b2 100644 --- a/src/ColumnSortable/SortableLink.php +++ b/src/ColumnSortable/SortableLink.php @@ -19,7 +19,7 @@ class SortableLink */ public static function render(array $parameters) { - list($sortColumn, $sortParameter, $title) = self::parseParameters($parameters); + list($sortColumn, $sortParameter, $title, $queryParameters) = self::parseParameters($parameters); $title = self::applyFormatting($title); @@ -52,6 +52,7 @@ public static function render(array $parameters) $queryString = http_build_query( array_merge( + $queryParameters, array_filter(Request::except('sort', 'order', 'page')), [ 'sort' => $sortParameter, @@ -69,11 +70,14 @@ public static function render(array $parameters) */ public static function parseParameters(array $parameters) { + //TODO: let 2nd parameter be both title, or default query parameters + //TODO: needs some checks before determining $title $explodeResult = self::explodeSortParameter($parameters[0]); $sortColumn = (empty($explodeResult)) ? $parameters[0] : $explodeResult[1]; $title = (count($parameters) === 1) ? $sortColumn : $parameters[1]; + $queryParameters = (isset($parameters[2]) && is_array($parameters[2])) ? $parameters[2] : []; - return [$sortColumn, $parameters[0], $title]; + return [$sortColumn, $parameters[0], $title, $queryParameters]; } /** diff --git a/tests/SortableLinkTest.php b/tests/SortableLinkTest.php index 0bd3cf4..42f3d36 100644 --- a/tests/SortableLinkTest.php +++ b/tests/SortableLinkTest.php @@ -11,26 +11,37 @@ public function testParseParameters() { $parameters = ['column']; $resultArray = SortableLink::parseParameters($parameters); - $expected = ['column', 'column', 'column']; + $expected = ['column', 'column', 'column', []]; $this->assertEquals($expected, $resultArray); $parameters = ['column', 'ColumnTitle']; $resultArray = SortableLink::parseParameters($parameters); - $expected = ['column', 'column', 'ColumnTitle']; + $expected = ['column', 'column', 'ColumnTitle', []]; + $this->assertEquals($expected, $resultArray); + + $parameters = ['column', 'ColumnTitle', ['world' => 'matrix']]; + $resultArray = SortableLink::parseParameters($parameters); + $expected = ['column', 'column', 'ColumnTitle', ['world' => 'matrix']]; $this->assertEquals($expected, $resultArray); $parameters = ['relation.column']; $resultArray = SortableLink::parseParameters($parameters); - $expected = ['column', 'relation.column', 'column']; + $expected = ['column', 'relation.column', 'column', []]; $this->assertEquals($expected, $resultArray); $parameters = ['relation.column', 'ColumnTitle']; $resultArray = SortableLink::parseParameters($parameters); - $expected = ['column', 'relation.column', 'ColumnTitle']; + $expected = ['column', 'relation.column', 'ColumnTitle', []]; + $this->assertEquals($expected, $resultArray); + + $parameters = ['relation.column', 'ColumnTitle', ['world' => 'matrix']]; + $resultArray = SortableLink::parseParameters($parameters); + $expected = ['column', 'relation.column', 'ColumnTitle', ['world' => 'matrix']]; $this->assertEquals($expected, $resultArray); } - public function testGetOneToOneSort() { + public function testGetOneToOneSort() + { $sortParameter = 'relation-name.column'; $resultArray = SortableLink::explodeSortParameter($sortParameter); $expected = ['relation-name', 'column']; @@ -46,9 +57,9 @@ public function testGetOneToOneSort() { * @expectedException Kyslik\ColumnSortable\Exceptions\ColumnSortableException * @expectedExceptionCode 0 */ - public function testGetOneToOneSortThrowsException() { + public function testGetOneToOneSortThrowsException() + { $sortParameter = 'relation-name..column'; SortableLink::explodeSortParameter($sortParameter); } - -} \ No newline at end of file +}