Skip to content

Instantly share code, notes, and snippets.

@andrewwheal
Created August 12, 2013 13:38
Show Gist options
  • Save andrewwheal/6210875 to your computer and use it in GitHub Desktop.
Save andrewwheal/6210875 to your computer and use it in GitHub Desktop.
<?php
namespace DataGrid;
class DataGrid
{
/**
* Forge a new DataGrid
*
* @param string|\Fuel\Core\Database_Query_Builder_Select $table_or_query
* @param string $edit_link
*
* @return static a new DataGrid object
*/
public static function forge($table_or_query, $edit_link = null)
{
/* @var DataGrid $object */
$object = new static($table_or_query);
$edit_link and $object->set_edit_link($edit_link);
return $object;
}
/**
* Add a Filter
*
* @throws \InvalidArgumentException when you try to add a filter with the same name as an existing filter
*
* @param Filter $filter
*
* @return static $this for chaining
*/
public function add_filter(Filter $filter)
{
$name = $filter->get_name();
if (array_key_exists($name, $this->filters)) {
throw new \InvalidArgumentException('Cannot add duplicate Filter to DataGrid: '.$name);
}
$this->filters[$name] = $filter;
return $this;
}
}
<?php
namespace DataGrid;
class Filter
{
/**
* Forge a new DataGrid Filter
*
* @param string $title
* @param array|\Fuel\Core\Database_Query_Builder_Select $query_or_array
* @param array $actions
* @param string $icon
*
* @return static a new DataGrid Filter object
*/
public static function forge($title, $query_or_array, $actions = [], $icon = null)
{
/* @var Filter $object */
$object = new static($title, $query_or_array);
$actions and $object->set_actions($actions);
$icon and $object->set_icon($icon);
return $object;
}
}
$datagrid = \DataGrid\DataGrid::forge($query)
->add_filter(\DataGrid\Filter::forge(
'Categories',
[]
));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment