Skip to content

Commit

Permalink
Refactor SortableLink
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyslik committed Sep 8, 2016
1 parent 9f66e88 commit aa1e425
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
1 change: 0 additions & 1 deletion src/ColumnSortable/ColumnSortableServiceProvider.php
Expand Up @@ -30,7 +30,6 @@ public function boot()
], 'config');

Blade::directive('sortablelink', function ($expression) {
//TODO: only accepts single quotes 'Email', would be nice to accept double quotes as well.
$expression = ($expression[0] === '(') ? substr($expression, 1, -1) : $expression;
return "<?php echo \Kyslik\ColumnSortable\SortableLink::render(array ({$expression}));?>";
});
Expand Down
38 changes: 24 additions & 14 deletions src/ColumnSortable/SortableLink.php
Expand Up @@ -19,29 +19,18 @@ class SortableLink
*/
public static function render(array $parameters)
{
if (count($parameters) === 1) {
$title = self::getOneToOneSortOrNull($parameters[0]);
$title = (is_null($title)) ? $parameters[0] : $title[1];
} else {
$title = $parameters[1];
}
list($sort, $title) = self::parseParameters($parameters);
$sortOriginal = $sort;

$sort = $sortOriginal = $parameters[0];
unset($parameters);

$formatting_function = Config::get('columnsortable.formatting_function', null);

if (!is_null($formatting_function) && function_exists($formatting_function)) {
$title = call_user_func($formatting_function, $title);
}

$icon = Config::get('columnsortable.default_icon_set');

if ($oneToOneSort = self::getOneToOneSortOrNull($sort)) {
$sort = $oneToOneSort[1];
}

foreach (Config::get('columnsortable.columns') as $key => $value) {
foreach (Config::get('columnsortable.columns') as $value) {
if (in_array($sort, $value['rows'])) {
$icon = $value['class'];
}
Expand Down Expand Up @@ -100,4 +89,25 @@ public static function getOneToOneSortOrNull($sort)

return null;
}

/**
* @param array $parameters
* @return array
*/
private static function parseParameters(array $parameters)
{
if (count($parameters) === 1) {
$title = self::getOneToOneSortOrNull($parameters[0]);
$title = (is_null($title)) ? $parameters[0] : $title[1];
} else {
$title = $parameters[1];
}

$formatting_function = Config::get('columnsortable.formatting_function', null);
if (!is_null($formatting_function) && function_exists($formatting_function)) {
$title = call_user_func($formatting_function, $title);
}

return [$parameters[0], $title];
}
}
Empty file removed tests/.gitkeep
Empty file.

0 comments on commit aa1e425

Please sign in to comment.