Skip to content

Commit

Permalink
Fix �#38, sortablelink directive update
Browse files Browse the repository at this point in the history
@sortablelink directive accepts 3rd parameter, 3rd parameter must be
an array and it will be appended to generated link.
  • Loading branch information
Kyslik committed Sep 19, 2016
1 parent 8cbebf1 commit 6d36f39
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
8 changes: 6 additions & 2 deletions src/ColumnSortable/SortableLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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,
Expand All @@ -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];
}

/**
Expand Down
27 changes: 19 additions & 8 deletions tests/SortableLinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand All @@ -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);
}

}
}

0 comments on commit 6d36f39

Please sign in to comment.