Skip to content

Commit

Permalink
Fixing typo
Browse files Browse the repository at this point in the history
  • Loading branch information
gbrock committed Oct 12, 2015
1 parent e635015 commit b0e0f76
Showing 1 changed file with 33 additions and 49 deletions.
82 changes: 33 additions & 49 deletions src/Column.php
Expand Up @@ -2,29 +2,22 @@

use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Routing\UrlGenerator;
use Illuminate\Routing\Route;
use Illuminate\Support\Facades\URL;

class Column {
class Column
{
/** @var Model The base model from which we can gather certain options */
protected $model;

/** @var string Applicable database field used in sorting */
protected $field;

/** @var string The default sorting direction */
protected $direction;

/** @var string The visible portion of the column header */
protected $label;

/** @var bool Whether this column can be sorted by the user */
protected $sortable = false;

/** @var array The CSS classes applied to the column */
protected $classes = [];

/**
* @var closure
* A rendering closure used when generating cell data, accepts the model:
Expand All @@ -39,42 +32,33 @@ public static function create()
$class = new static;

// Detect instantiation scheme
switch(count($args))
{
switch (count($args)) {
case 1: // one argument passed
if(is_string($args[0]))
{
if (is_string($args[0])) {
// Only the field was passed
$class->setField($args[0]);
$class->setLabel(ucwords(str_replace('_', ' ', $args[0])));
}
elseif(is_array($args[0]))
{
} elseif (is_array($args[0])) {
// Just an array was sent; set the parameters.
$class->setParameters($args);
}
break;
case 2: // two arguments
if(is_string($args[0]) && is_string($args[1]))
{
if (is_string($args[0]) && is_string($args[1])) {
// Both are strings, this is a Field => Label affair.
$class->setField($args[0]);
$class->setLabel($args[1]);
}
elseif(is_string($args[0]) && is_array($args[1]))
{
} elseif (is_string($args[0]) && is_array($args[1])) {
// Normal complex initialization: field and quick parameters
$class->setField($args[0]);
$class->setParameters($args[1]);
if(!isset($args[1]['label']))
{
if (!isset($args[1]['label'])) {
$class->setLabel(ucwords(str_replace('_', ' ', $args[0])));
}
}
break;
case 3: // three arguments
if(is_string($args[0]) && is_string($args[1]) && is_callable($args[2]))
{
if (is_string($args[0]) && is_string($args[1]) && is_callable($args[2])) {
// Field, Label, and [rendering] Closure. Standard View addition.
$class->setField($args[0]);
$class->setLabel($args[1]);
Expand All @@ -94,18 +78,19 @@ public static function create()
*/
public function setOptionsFromModel($model)
{
if(!$model)
{
if (!$model) {
return $this;
}

if (property_exists($model, 'is_sortable') && $model->is_sortable && in_array($this->getField(), $model->getSortable())) {
{
if (property_exists($model, 'is_sortable') && $model->is_sortable && in_array($this->getField(),
$model->getSortable())
) {
// The model dictates that this column should be sortable
$this->setSortable(true);
}

$this->model = $model;

return $this;
}

Expand All @@ -114,13 +99,11 @@ public function setOptionsFromModel($model)
*/
public function isSorted()
{
if(Request::input(config('gbrock-tables.key_field')) == $this->getField())
{
if (Request::input(config('gbrock-tables.key_field')) == $this->getField()) {
return true;
}

if(!Request::input(config('gbrock-tables.key_field')) && $this->model && $this->model->getSortingField() == $this->getField())
{
if (!Request::input(config('gbrock-tables.key_field')) && $this->model && $this->model->getSortingField() == $this->getField()) {
// No sorting was requested, but this is the default field.
return true;
}
Expand All @@ -133,21 +116,19 @@ public function isSorted()
*/
public function getSortURL($direction = false)
{
if(!$direction)
{
if (!$direction) {
// No direction indicated, determine automatically from defaults.
$direction = $this->getDirection();

if($this->isSorted())
{
if ($this->isSorted()) {
// If we are already sorting by this column, swap the direction
$direction = $direction == 'asc' ? 'desc' : 'asc';
}
}

// Generate and return a URL which may be used to sort this column
return $this->generateUrl(array_filter([
config('gbrock-tables.key_field') => $this->getField(),
config('gbrock-tables.key_field') => $this->getField(),
config('gbrock-tables.key_direction') => $direction,
]));
}
Expand All @@ -158,14 +139,12 @@ public function getSortURL($direction = false)
*/
public function getDirection()
{
if($this->isSorted())
{
if ($this->isSorted()) {
// If the column is currently being sorted, grab the direction from the query string
$this->direction = Request::input(config('gbrock-tables.key_direction'));
}

if(!$this->direction)
{
if (!$this->direction) {
$this->direction = config('gbrock-tables.default_direction');
}

Expand All @@ -187,6 +166,7 @@ public function getField()
public function setField($field)
{
$this->field = $field;

return $this;
}

Expand All @@ -205,6 +185,7 @@ public function isSortable()
public function setSortable($sortable)
{
$this->sortable = (bool) $sortable;

return $this;
}

Expand All @@ -222,7 +203,7 @@ public function generateUrl($parameters = [])
protected function getCurrentInput()
{
return Input::only([
config('gbrock-tables.key_field') => Request::input(config('gbrock-tables.key_field')),
config('gbrock-tables.key_field') => Request::input(config('gbrock-tables.key_field')),
config('gbrock-tables.key_direction') => Request::input(config('gbrock-tables.key_direction')),
]);
}
Expand All @@ -242,15 +223,16 @@ public function getLabel()
public function setLabel($label)
{
$this->label = $label;

return $this;
}

public function setParameters($arguments)
{
foreach($arguments as $k => $v)
{
foreach ($arguments as $k => $v) {
$this->{'set' . ucfirst($k)}($v);
}

return $this;
}

Expand All @@ -261,14 +243,15 @@ public function setParameters($arguments)
public function setDirection($direction)
{
$this->direction = $direction;

return $this;
}

public function render($data)
{
if($this->hasRenderer())
{
if ($this->hasRenderer()) {
$renderer = $this->renderer;

return $renderer($data);
}
}
Expand All @@ -280,18 +263,19 @@ public function hasRenderer()

public function setRenderer($function)
{
if(!is_callable($function))
{
if (!is_callable($function)) {
throw new CallableFunctionNotProvidedException;
}

$this->renderer = $function;

return $this;
}

public function addClass($class)
{
$this->classes[] = $class;

return $this;
}

Expand Down

0 comments on commit b0e0f76

Please sign in to comment.