Skip to content

Commit

Permalink
Merge pull request #52 from leantony/2.0
Browse files Browse the repository at this point in the history
2.0
  • Loading branch information
leantony authored Jun 9, 2018
2 parents 7dbe643 + 8ffd578 commit 0a2d974
Show file tree
Hide file tree
Showing 12 changed files with 411 additions and 35 deletions.
281 changes: 268 additions & 13 deletions src/Columns/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,69 @@
namespace Leantony\Grid\Columns;

use InvalidArgumentException;
use Leantony\Grid\Filters\GenericFilter;

class Column
{
/**
* Column constructor.
* @param array $params
* @var string
*/
public function __construct(array $params)
{
foreach ($params as $k => $v) {
$this->__set($k, $v);
}
}
private $name;

/**
* @var callable
*/
private $data;

/**
* @var string
*/
private $key;

/**
* @var array
*/
private $searchableColumns;

/**
* @var string
*/
private $rowClass;

/**
* @var string
*/
private $columnClass;

/**
* @var boolean
*/
private $isSortable;

/**
* @var GenericFilter
*/
private $filter;

/**
* @var boolean
*/
private $useRawFormat;

/**
* @var boolean
*/
private $isExportable;

/**
* @var array|mixed
*/
private $extra;

/**
* @var array
*/
private $footer;

/**
* Dynamically get properties
Expand All @@ -36,13 +86,218 @@ public function __get($name)
}

/**
* Dynamically set properties
*
* @return mixed|array
*/
public function getFooter()
{
return $this->footer;
}

/**
* @param mixed|array $footer
* @return Column
*/
public function setFooter($footer): Column
{
$this->footer = $footer;
return $this;
}

/**
* @return array
*/
public function getExtra(): array
{
return $this->extra;
}

/**
* @param array $extra
* @return Column
*/
public function setExtra(array $extra): Column
{
$this->extra = $extra;
return $this;
}

/**
* @return string
*/
public function getName(): string
{
return $this->name;
}

/**
* @param string $name
* @param mixed $value
* @return Column
*/
public function setName(string $name): Column
{
$this->name = $name;
return $this;
}

/**
* @return callable
*/
public function getData(): callable
{
return $this->data;
}

/**
* @param callable $data
* @return Column
*/
public function setData(callable $data): Column
{
$this->data = $data;
return $this;
}

/**
* @return string
*/
public function getKey(): string
{
return $this->key;
}

/**
* @param string $key
* @return Column
*/
public function setKey(string $key): Column
{
$this->key = $key;
return $this;
}

/**
* @return array
*/
public function getSearchableColumns(): array
{
return $this->searchableColumns;
}

/**
* @param array $searchableColumns
* @return Column
*/
public function setSearchableColumns(array $searchableColumns): Column
{
$this->searchableColumns = $searchableColumns;
return $this;
}

/**
* @return string
*/
public function getRowClass(): string
{
return $this->rowClass;
}

/**
* @param string $rowClass
* @return Column
*/
public function setRowClass(string $rowClass): Column
{
$this->rowClass = $rowClass;
return $this;
}

/**
* @return string
*/
public function getColumnClass(): string
{
return $this->columnClass;
}

/**
* @param string $columnClass
* @return Column
*/
public function setColumnClass(string $columnClass): Column
{
$this->columnClass = $columnClass;
return $this;
}

/**
* @return bool
*/
public function isSortable(): bool
{
return $this->isSortable;
}

/**
* @param bool $isSortable
* @return Column
*/
public function setIsSortable(bool $isSortable): Column
{
$this->isSortable = $isSortable;
return $this;
}

/**
* @return GenericFilter
*/
public function getFilter(): GenericFilter
{
return $this->filter;
}

/**
* @param GenericFilter $filter
* @return Column
*/
public function setFilter($filter): Column
{
$this->filter = $filter;
return $this;
}

/**
* @return bool
*/
public function isUseRawFormat(): bool
{
return $this->useRawFormat;
}

/**
* @param bool $useRawFormat
* @return Column
*/
public function setUseRawFormat(bool $useRawFormat): Column
{
$this->useRawFormat = $useRawFormat;
return $this;
}

/**
* @return bool
*/
public function isExportable(): bool
{
return $this->isExportable;
}

/**
* @param bool $isExportable
* @return Column
*/
public function __set($name, $value)
public function setIsExportable(bool $isExportable): Column
{
$this->{$name} = $value;
$this->isExportable = $isExportable;
return $this;
}
}
61 changes: 48 additions & 13 deletions src/Columns/CreatesColumns.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,26 +96,61 @@ public function processColumns()
// data
$data = $this->fetchColumnData($columnName, $columnData)['data'];

// once we are done, push to columns array
array_push($columns, new Column([
'name' => $label,
'key' => $columnName,
'data' => $data,
'searchable' => $searchable,
'rowClass' => $rowClass,
'columnClass' => $columnClass,
'sortable' => $columnData['sort'] ?? true,
'filter' => $filter,
'raw' => $columnData['raw'] ?? false,
'export' => $columnData['export'] ?? true,
]));
// footer
$footer = $this->fetchFooterData($columnName, $columnData)['footer'];

$col = (new Column())->setName($label)
->setKey($columnName)
->setData($data)
->setSearchableColumns($searchable)
->setColumnClass($columnClass)
->setRowClass($rowClass)
->setIsSortable($columnData['sort'] ?? true)
->setUseRawFormat($columnData['raw'] ?? false)
->setFilter($filter)
->setIsExportable($columnData['export'] ?? true)
->setExtra($this->getExtras($columnData))
->setFooter($footer);

array_push($columns, $col);
}

$this->processedColumns = $columns;

return $this->processedColumns;
}

/**
* Any extra/custom column data to be sent to the view
*
* @param array $columnData
* @return array|mixed
*/
public function getExtras(array $columnData)
{
return $columnData['extra'] ?? [];
}

/**
* Get footer data
*
* @param string $columnName
* @param array $columnData
* @return array|mixed
*/
public function fetchFooterData(string $columnName, array $columnData): array
{
if (!isset($columnData['footer'])) {
return ['footer' => null];
} else {
$data = $columnData['footer']['data'] ?? null;
if (is_callable($data)) {
return ['footer' => $data];
}
throw new \InvalidArgumentException("the 'data' key needs to be a function.");
}
}

/**
* Determine if a column can be rendered on the grid
*
Expand Down
Loading

0 comments on commit 0a2d974

Please sign in to comment.