Skip to content

Commit

Permalink
Merge pull request #147 from Kyslik/master
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyslik authored Mar 1, 2020
2 parents c567d5d + 29f7c90 commit 2fecd32
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 7 deletions.
26 changes: 21 additions & 5 deletions src/ColumnSortable/SortableLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Kyslik\ColumnSortable;

use Kyslik\ColumnSortable\Exceptions\ColumnSortableException;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Support\Str;

/**
Expand All @@ -22,7 +23,7 @@ public static function render(array $parameters)
{
list($sortColumn, $sortParameter, $title, $queryParameters, $anchorAttributes) = self::parseParameters($parameters);

$title = self::applyFormatting($title);
$title = self::applyFormatting($title, $sortColumn);

if ($mergeTitleAs = config('columnsortable.inject_title_as', null)) {
request()->merge([$mergeTitleAs => $title]);
Expand All @@ -38,7 +39,7 @@ public static function render(array $parameters)

$queryString = self::buildQueryString($queryParameters, $sortParameter, $direction);

return '<a'.$anchorClass.' href="'.url(request()->path().'?'.$queryString).'"'.$anchorAttributesString.'>'.htmlentities($title).$trailingTag;
return '<a'.$anchorClass.' href="'.url(request()->path().'?'.$queryString).'"'.$anchorAttributesString.'>'.e($title).$trailingTag;
}


Expand All @@ -54,7 +55,7 @@ public static function parseParameters(array $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];
$title = (count($parameters) === 1) ? null : $parameters[1];
$queryParameters = (isset($parameters[2]) && is_array($parameters[2])) ? $parameters[2] : [];
$anchorAttributes = (isset($parameters[3]) && is_array($parameters[3])) ? $parameters[3] : [];

Expand Down Expand Up @@ -90,12 +91,23 @@ public static function explodeSortParameter($parameter)


/**
* @param string $title
* @param string|\Illuminate\Contracts\Support\Htmlable|null $title
* @param string $sortColumn
*
* @return string
*/
private static function applyFormatting($title)
private static function applyFormatting($title, $sortColumn)
{
if ($title instanceof Htmlable) {
return $title;
}

if ($title === null) {
$title = $sortColumn;
} elseif ( ! config('columnsortable.format_custom_titles', true)){
return $title;
}

$formatting_function = config('columnsortable.formatting_function', null);
if ( ! is_null($formatting_function) && function_exists($formatting_function)) {
$title = call_user_func($formatting_function, $title);
Expand Down Expand Up @@ -249,6 +261,10 @@ private static function buildQueryString($queryParameters, $sortParameter, $dire

private static function buildAnchorAttributesString($anchorAttributes)
{
if (empty($anchorAttributes)) {
return '';
}

$attributes = [];
foreach ($anchorAttributes as $k => $v) {
$attributes[] = $k.('' != $v ? '="'.$v.'"' : '');
Expand Down
5 changes: 5 additions & 0 deletions src/config/columnsortable.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@
*/
'formatting_function' => 'ucfirst',

/*
apply formatting function to custom titles as well as column names
*/
'format_custom_titles' => true,

/*
inject title parameter in query strings, use null to turn injection off
example: 'inject_title' => 't' will result in ..user/?t="formatted title of sorted column"
Expand Down
59 changes: 57 additions & 2 deletions tests/SortableLinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\HtmlString;
use Kyslik\ColumnSortable\SortableLink;

/**
Expand Down Expand Up @@ -52,12 +53,66 @@ public function testInjectTitleInQueryStringsIsOff()
$this->assertEquals([], Request::all());
}

public function testGeneratingAnchorAttributes()
{
$link = SortableLink::render(['column', 'ColumnTitle', ['a' => 'b'], ['c' => 'd']]);

$this->assertSame('<a href="http://localhost?a=b&sort=column&direction=asc" c="d">ColumnTitle</a><i class=""></i>', $link);
}

public function testGeneratingTitleWithoutFormattingFunction()
{
Config::set('columnsortable.formatting_function', null);
$link = SortableLink::render(['column']);

$this->assertSame('<a href="http://localhost?sort=column&direction=asc">column</a><i class=""></i>', $link);
}

public function testGeneratingTitle()
{
Config::set('columnsortable.formatting_function', 'ucfirst');
Config::set('columnsortable.format_custom_titles', true);
$link = SortableLink::render(['column']);

$this->assertSame('<a href="http://localhost?sort=column&direction=asc">Column</a><i class=""></i>', $link);
}


public function testCustomTitle()
{
Config::set('columnsortable.formatting_function', 'ucfirst');
Config::set('columnsortable.format_custom_titles', true);
$link = SortableLink::render(['column', 'columnTitle']);

$this->assertSame('<a href="http://localhost?sort=column&direction=asc">ColumnTitle</a><i class=""></i>', $link);
}


public function testCustomTitleWithoutFormatting()
{
Config::set('columnsortable.formatting_function', 'ucfirst');
Config::set('columnsortable.format_custom_titles', false);
$link = SortableLink::render(['column', 'columnTitle']);

$this->assertSame('<a href="http://localhost?sort=column&direction=asc">columnTitle</a><i class=""></i>', $link);
}


public function testCustomTitleWithHTML()
{
Config::set('columnsortable.formatting_function', 'ucfirst');
Config::set('columnsortable.format_custom_titles', true);
$link = SortableLink::render(['column', new HtmlString('<em>columnTitle</em>')]);

$this->assertSame('<a href="http://localhost?sort=column&direction=asc"><em>columnTitle</em></a><i class=""></i>', $link);
}


public function testParseParameters()
{
$parameters = ['column'];
$resultArray = SortableLink::parseParameters($parameters);
$expected = ['column', 'column', 'column', [], []];
$expected = ['column', 'column', null, [], []];
$this->assertEquals($expected, $resultArray);

$parameters = ['column', 'ColumnTitle'];
Expand All @@ -77,7 +132,7 @@ public function testParseParameters()

$parameters = ['relation.column'];
$resultArray = SortableLink::parseParameters($parameters);
$expected = ['column', 'relation.column', 'column', [], []];
$expected = ['column', 'relation.column', null, [], []];
$this->assertEquals($expected, $resultArray);

$parameters = ['relation.column', 'ColumnTitle'];
Expand Down

0 comments on commit 2fecd32

Please sign in to comment.