Skip to content

Commit

Permalink
Upstreaming instantiation by array
Browse files Browse the repository at this point in the history
  • Loading branch information
gbrock committed Oct 12, 2015
1 parent 212b602 commit 33319ce
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 26 deletions.
22 changes: 22 additions & 0 deletions src/BlankModel.php
@@ -0,0 +1,22 @@
<?php

namespace Gbrock\Table;

class BlankModel {
private $attributes;

public function __construct($attributes)
{
$this->attributes = (array) $attributes;
}

public function __get($name)
{
return array_get($this->attributes, $name, null);
}

public function __call($name, $arguments = [])
{
return;
}
}
52 changes: 26 additions & 26 deletions src/Table.php
Expand Up @@ -3,8 +3,8 @@
use Illuminate\Support\Facades\Input;
use Gbrock\Table\Column;

class Table {

class Table
{
protected $models;
protected $columns;
protected $view = 'gbrock::table';
Expand All @@ -16,12 +16,10 @@ class Table {
*/
public function __construct($models = [], $columns = [])
{
if($models)
{
if ($models) {
$this->setModels($models);

if(!$columns && $columns !== FALSE)
{
if (!$columns && $columns !== false) {
// Columns were not passed and were not prevented from auto-generation; generate them
$columns = $this->getFieldsFromModels($models);
}
Expand Down Expand Up @@ -58,8 +56,7 @@ public function getView()
public function setView($view, $vars = true)
{
$this->view = $view;
if(is_array($vars) || !$vars)
{
if (is_array($vars) || !$vars) {
$this->viewVars = $vars;
}
}
Expand Down Expand Up @@ -90,17 +87,12 @@ protected function addColumns($columns)
{
$model = $this->models->first();

if($columns)
{
foreach($columns as $key => $field)
{
if(is_numeric($key))
{
if ($columns) {
foreach ($columns as $key => $field) {
if (is_numeric($key)) {
// Simple non-keyed array passed.
$new_column = Column::create($field);
}
else
{
} else {
// Key also matters, apparently
$new_column = Column::create($key, $field);
}
Expand All @@ -119,8 +111,7 @@ protected function addColumns($columns)
*/
protected function getFieldsFromModels($models)
{
if(!$models->first())
{
if (!$models->first()) {
// No models, we can't add any columns.
return [];
}
Expand All @@ -133,8 +124,7 @@ protected function getFieldsFromModels($models)
$fields = array_keys($model->toArray());
// Remove the timestamp fields
$fields = array_diff($fields, $timestamp_fields);
if($model->isSortable)
{
if ($model->isSortable) {
// Add the fields from the model's sortable array
$fields = array_unique(array_merge($fields, $model->getSortable()));
}
Expand All @@ -149,6 +139,7 @@ protected function getFieldsFromModels($models)
public function render()
{
$this->appendPaginationLinks();

return view($this->view, $this->getData())->render();
}

Expand All @@ -159,7 +150,7 @@ public function render()
public function getData()
{
return array_merge($this->viewVars, [
'rows' => $this->getRows(),
'rows' => $this->getRows(),
'columns' => $this->getColumns(),
]);
}
Expand Down Expand Up @@ -198,6 +189,16 @@ public function setColumns($columns)
*/
public function setModels($models)
{
if (!is_object($models)) {
if (is_array($models)) {
foreach ($models as $k => $v) {
$models[$k] = new BlankModel($v);
}
}

$models = collect($models);
}

$this->models = $models;
}

Expand All @@ -214,11 +215,10 @@ private function clearColumns()
*/
private function appendPaginationLinks()
{
if(class_basename($this->models) == 'LengthAwarePaginator')
{
if (class_basename($this->models) == 'LengthAwarePaginator') {
// This set of models was paginated. Make it append our current view variables.
$this->models->appends(Input::only(config('gbrock-tables.key_field'), config('gbrock-tables.key_direction')));
$this->models->appends(Input::only(config('gbrock-tables.key_field'),
config('gbrock-tables.key_direction')));
}
}

}

0 comments on commit 33319ce

Please sign in to comment.