@@ -6,5 +6,5 @@ |
||
| 6 | 6 | * @return \Woo\GridView\GridView |
| 7 | 7 | */ |
| 8 | 8 | function grid(array $config) { |
| 9 | - return \Woo\GridView\GridView::make($config); |
|
| 9 | + return \Woo\GridView\GridView::make($config); |
|
| 10 | 10 | } |
| 11 | 11 | \ No newline at end of file |
@@ -6,94 +6,94 @@ |
||
| 6 | 6 | |
| 7 | 7 | class GridViewHelper |
| 8 | 8 | { |
| 9 | - private function __construct() {} |
|
| 10 | - |
|
| 11 | - /** |
|
| 12 | - * Allows to load config into object properties |
|
| 13 | - * @param object $object |
|
| 14 | - * @param array $config |
|
| 15 | - * @throws GridViewConfigException |
|
| 16 | - */ |
|
| 17 | - public static function loadConfig($object, array $config) |
|
| 18 | - { |
|
| 19 | - foreach ($config as $key => $value) { |
|
| 20 | - |
|
| 21 | - if (property_exists($object, $key)) { |
|
| 22 | - $object->$key = $value; |
|
| 23 | - } |
|
| 24 | - } |
|
| 25 | - } |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * Allows to test attributes and types in config |
|
| 29 | - * @param $object |
|
| 30 | - * @param array $tests |
|
| 31 | - * @throws GridViewConfigException |
|
| 32 | - */ |
|
| 33 | - public static function testConfig($object, array $tests) |
|
| 34 | - { |
|
| 35 | - foreach ($tests as $property => $test) { |
|
| 36 | - |
|
| 37 | - if (!property_exists($object, $property)) { |
|
| 38 | - throw new GridViewConfigException( |
|
| 39 | - 'Unable to test ' . get_class($object) . ': property ' . $property . ' does not exist' |
|
| 40 | - ); |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - if (is_scalar($test)) { |
|
| 44 | - |
|
| 45 | - $testPassed = true; |
|
| 46 | - |
|
| 47 | - switch ($test) { |
|
| 48 | - case 'int': |
|
| 49 | - $testPassed = is_numeric($object->$property); |
|
| 50 | - break; |
|
| 51 | - |
|
| 52 | - case 'string': |
|
| 53 | - $testPassed = is_string($object->$property); |
|
| 54 | - break; |
|
| 55 | - |
|
| 56 | - case 'array': |
|
| 57 | - $testPassed = is_array($object->$property); |
|
| 58 | - break; |
|
| 59 | - |
|
| 60 | - case 'closure': |
|
| 61 | - $testPassed = $object->$property instanceof \Closure; |
|
| 62 | - break; |
|
| 63 | - |
|
| 64 | - case 'any': |
|
| 65 | - break; |
|
| 66 | - |
|
| 67 | - default: |
|
| 68 | - $testPassed = is_subclass_of($object->$property, $test); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - if (!$testPassed) { |
|
| 72 | - throw new GridViewConfigException(' |
|
| 9 | + private function __construct() {} |
|
| 10 | + |
|
| 11 | + /** |
|
| 12 | + * Allows to load config into object properties |
|
| 13 | + * @param object $object |
|
| 14 | + * @param array $config |
|
| 15 | + * @throws GridViewConfigException |
|
| 16 | + */ |
|
| 17 | + public static function loadConfig($object, array $config) |
|
| 18 | + { |
|
| 19 | + foreach ($config as $key => $value) { |
|
| 20 | + |
|
| 21 | + if (property_exists($object, $key)) { |
|
| 22 | + $object->$key = $value; |
|
| 23 | + } |
|
| 24 | + } |
|
| 25 | + } |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * Allows to test attributes and types in config |
|
| 29 | + * @param $object |
|
| 30 | + * @param array $tests |
|
| 31 | + * @throws GridViewConfigException |
|
| 32 | + */ |
|
| 33 | + public static function testConfig($object, array $tests) |
|
| 34 | + { |
|
| 35 | + foreach ($tests as $property => $test) { |
|
| 36 | + |
|
| 37 | + if (!property_exists($object, $property)) { |
|
| 38 | + throw new GridViewConfigException( |
|
| 39 | + 'Unable to test ' . get_class($object) . ': property ' . $property . ' does not exist' |
|
| 40 | + ); |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + if (is_scalar($test)) { |
|
| 44 | + |
|
| 45 | + $testPassed = true; |
|
| 46 | + |
|
| 47 | + switch ($test) { |
|
| 48 | + case 'int': |
|
| 49 | + $testPassed = is_numeric($object->$property); |
|
| 50 | + break; |
|
| 51 | + |
|
| 52 | + case 'string': |
|
| 53 | + $testPassed = is_string($object->$property); |
|
| 54 | + break; |
|
| 55 | + |
|
| 56 | + case 'array': |
|
| 57 | + $testPassed = is_array($object->$property); |
|
| 58 | + break; |
|
| 59 | + |
|
| 60 | + case 'closure': |
|
| 61 | + $testPassed = $object->$property instanceof \Closure; |
|
| 62 | + break; |
|
| 63 | + |
|
| 64 | + case 'any': |
|
| 65 | + break; |
|
| 66 | + |
|
| 67 | + default: |
|
| 68 | + $testPassed = is_subclass_of($object->$property, $test); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + if (!$testPassed) { |
|
| 72 | + throw new GridViewConfigException(' |
|
| 73 | 73 | Test ' . $test . ' has failed on ' . get_class($object) . '::' . $property |
| 74 | - ); |
|
| 75 | - } |
|
| 76 | - } |
|
| 77 | - } |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * Allows to convert options array to html string |
|
| 82 | - * @param array $htmlOptions |
|
| 83 | - * @return string |
|
| 84 | - */ |
|
| 85 | - public static function htmlOptionsToString(array $htmlOptions) : string |
|
| 86 | - { |
|
| 87 | - if (empty($htmlOptions)) { |
|
| 88 | - return ''; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - $out = []; |
|
| 92 | - |
|
| 93 | - foreach ($htmlOptions as $k => $v) { |
|
| 94 | - $out[] = htmlentities($k) . '="' . htmlentities($v, ENT_COMPAT) . '"'; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - return implode(' ', $out); |
|
| 98 | - } |
|
| 74 | + ); |
|
| 75 | + } |
|
| 76 | + } |
|
| 77 | + } |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * Allows to convert options array to html string |
|
| 82 | + * @param array $htmlOptions |
|
| 83 | + * @return string |
|
| 84 | + */ |
|
| 85 | + public static function htmlOptionsToString(array $htmlOptions) : string |
|
| 86 | + { |
|
| 87 | + if (empty($htmlOptions)) { |
|
| 88 | + return ''; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + $out = []; |
|
| 92 | + |
|
| 93 | + foreach ($htmlOptions as $k => $v) { |
|
| 94 | + $out[] = htmlentities($k) . '="' . htmlentities($v, ENT_COMPAT) . '"'; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + return implode(' ', $out); |
|
| 98 | + } |
|
| 99 | 99 | } |
| 100 | 100 | \ No newline at end of file |
@@ -7,20 +7,20 @@ |
||
| 7 | 7 | |
| 8 | 8 | class DefaultRenderer implements RendererInterface |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * @inheritdoc |
|
| 12 | - */ |
|
| 13 | - public function render(GridView $view): string |
|
| 14 | - { |
|
| 15 | - $page = intval($_GET['page'] ?? 1); |
|
| 10 | + /** |
|
| 11 | + * @inheritdoc |
|
| 12 | + */ |
|
| 13 | + public function render(GridView $view): string |
|
| 14 | + { |
|
| 15 | + $page = intval($_GET['page'] ?? 1); |
|
| 16 | 16 | |
| 17 | - return view('woo_gridview::render-default', [ |
|
| 18 | - 'columns' => $view->columns, |
|
| 19 | - 'data' => $view->dataProvider->getData($page, $view->rowsPerPage), |
|
| 20 | - 'tableHtmlOptions' => GridViewHelper::htmlOptionsToString($view->tableHtmlOptions), |
|
| 21 | - 'dataProvider' => $view->dataProvider, |
|
| 22 | - 'perPage' => $view->rowsPerPage, |
|
| 23 | - 'currentPage' => $page, |
|
| 24 | - ])->render(); |
|
| 25 | - } |
|
| 17 | + return view('woo_gridview::render-default', [ |
|
| 18 | + 'columns' => $view->columns, |
|
| 19 | + 'data' => $view->dataProvider->getData($page, $view->rowsPerPage), |
|
| 20 | + 'tableHtmlOptions' => GridViewHelper::htmlOptionsToString($view->tableHtmlOptions), |
|
| 21 | + 'dataProvider' => $view->dataProvider, |
|
| 22 | + 'perPage' => $view->rowsPerPage, |
|
| 23 | + 'currentPage' => $page, |
|
| 24 | + ])->render(); |
|
| 25 | + } |
|
| 26 | 26 | } |
| 27 | 27 | \ No newline at end of file |
@@ -6,5 +6,5 @@ |
||
| 6 | 6 | |
| 7 | 7 | interface RendererInterface |
| 8 | 8 | { |
| 9 | - public function render(GridView $view) : string; |
|
| 9 | + public function render(GridView $view) : string; |
|
| 10 | 10 | } |
| 11 | 11 | \ No newline at end of file |
@@ -6,48 +6,48 @@ |
||
| 6 | 6 | |
| 7 | 7 | abstract class BaseColumn |
| 8 | 8 | { |
| 9 | - public $title = ''; |
|
| 10 | - |
|
| 11 | - public $value = ''; |
|
| 12 | - |
|
| 13 | - public $headerHtmlOptions = []; |
|
| 14 | - |
|
| 15 | - public $contentHtmlOptions = []; |
|
| 16 | - |
|
| 17 | - public function __construct(array $config) |
|
| 18 | - { |
|
| 19 | - GridViewHelper::loadConfig($this, $config); |
|
| 20 | - |
|
| 21 | - GridViewHelper::testConfig($this, [ |
|
| 22 | - 'title' => 'string', |
|
| 23 | - 'value' => 'any', |
|
| 24 | - 'headerHtmlOptions' => 'array', |
|
| 25 | - 'contentHtmlOptions' => 'array', |
|
| 26 | - ]); |
|
| 27 | - } |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * Formatted header html options |
|
| 31 | - * @return string |
|
| 32 | - */ |
|
| 33 | - public function headerHtmlOptions() : string |
|
| 34 | - { |
|
| 35 | - return GridViewHelper::htmlOptionsToString($this->headerHtmlOptions); |
|
| 36 | - } |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Formatted content html options |
|
| 40 | - * @return string |
|
| 41 | - */ |
|
| 42 | - public function contentHtmlOptions() : string |
|
| 43 | - { |
|
| 44 | - return GridViewHelper::htmlOptionsToString($this->contentHtmlOptions); |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Render column value for row |
|
| 49 | - * @param array|object $row |
|
| 50 | - * @return string|mixed |
|
| 51 | - */ |
|
| 52 | - public abstract function renderValue($row); |
|
| 9 | + public $title = ''; |
|
| 10 | + |
|
| 11 | + public $value = ''; |
|
| 12 | + |
|
| 13 | + public $headerHtmlOptions = []; |
|
| 14 | + |
|
| 15 | + public $contentHtmlOptions = []; |
|
| 16 | + |
|
| 17 | + public function __construct(array $config) |
|
| 18 | + { |
|
| 19 | + GridViewHelper::loadConfig($this, $config); |
|
| 20 | + |
|
| 21 | + GridViewHelper::testConfig($this, [ |
|
| 22 | + 'title' => 'string', |
|
| 23 | + 'value' => 'any', |
|
| 24 | + 'headerHtmlOptions' => 'array', |
|
| 25 | + 'contentHtmlOptions' => 'array', |
|
| 26 | + ]); |
|
| 27 | + } |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * Formatted header html options |
|
| 31 | + * @return string |
|
| 32 | + */ |
|
| 33 | + public function headerHtmlOptions() : string |
|
| 34 | + { |
|
| 35 | + return GridViewHelper::htmlOptionsToString($this->headerHtmlOptions); |
|
| 36 | + } |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Formatted content html options |
|
| 40 | + * @return string |
|
| 41 | + */ |
|
| 42 | + public function contentHtmlOptions() : string |
|
| 43 | + { |
|
| 44 | + return GridViewHelper::htmlOptionsToString($this->contentHtmlOptions); |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Render column value for row |
|
| 49 | + * @param array|object $row |
|
| 50 | + * @return string|mixed |
|
| 51 | + */ |
|
| 52 | + public abstract function renderValue($row); |
|
| 53 | 53 | } |
| 54 | 54 | \ No newline at end of file |
@@ -6,24 +6,24 @@ |
||
| 6 | 6 | |
| 7 | 7 | class RawColumn extends BaseColumn |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * RawColumn constructor. |
|
| 11 | - * @param array $config |
|
| 12 | - */ |
|
| 13 | - public function __construct(array $config) |
|
| 14 | - { |
|
| 15 | - parent::__construct($config); |
|
| 9 | + /** |
|
| 10 | + * RawColumn constructor. |
|
| 11 | + * @param array $config |
|
| 12 | + */ |
|
| 13 | + public function __construct(array $config) |
|
| 14 | + { |
|
| 15 | + parent::__construct($config); |
|
| 16 | 16 | |
| 17 | - GridViewHelper::testConfig($this, [ |
|
| 18 | - 'value' => 'closure', |
|
| 19 | - ]); |
|
| 20 | - } |
|
| 17 | + GridViewHelper::testConfig($this, [ |
|
| 18 | + 'value' => 'closure', |
|
| 19 | + ]); |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * @inheritdoc |
|
| 24 | - */ |
|
| 25 | - public function renderValue($row) |
|
| 26 | - { |
|
| 27 | - return call_user_func($this->value, $row); |
|
| 28 | - } |
|
| 22 | + /** |
|
| 23 | + * @inheritdoc |
|
| 24 | + */ |
|
| 25 | + public function renderValue($row) |
|
| 26 | + { |
|
| 27 | + return call_user_func($this->value, $row); |
|
| 28 | + } |
|
| 29 | 29 | } |
| 30 | 30 | \ No newline at end of file |
@@ -4,24 +4,24 @@ |
||
| 4 | 4 | |
| 5 | 5 | interface DataProviderInterface |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * Should return total amount of rows |
|
| 9 | - * @return int |
|
| 10 | - */ |
|
| 11 | - public function getCount() : int; |
|
| 7 | + /** |
|
| 8 | + * Should return total amount of rows |
|
| 9 | + * @return int |
|
| 10 | + */ |
|
| 11 | + public function getCount() : int; |
|
| 12 | 12 | |
| 13 | - /** |
|
| 14 | - * Should return amount of pages |
|
| 15 | - * @param int $perPage - amount of records per page |
|
| 16 | - * @return int |
|
| 17 | - */ |
|
| 18 | - public function getTotalPages(int $perPage) : int; |
|
| 13 | + /** |
|
| 14 | + * Should return amount of pages |
|
| 15 | + * @param int $perPage - amount of records per page |
|
| 16 | + * @return int |
|
| 17 | + */ |
|
| 18 | + public function getTotalPages(int $perPage) : int; |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * Should return a list of data for current page |
|
| 22 | - * @param int $page |
|
| 23 | - * @param int $perPage - amount of records per page |
|
| 24 | - * @return mixed |
|
| 25 | - */ |
|
| 26 | - public function getData(int $page, int $perPage); |
|
| 20 | + /** |
|
| 21 | + * Should return a list of data for current page |
|
| 22 | + * @param int $page |
|
| 23 | + * @param int $perPage - amount of records per page |
|
| 24 | + * @return mixed |
|
| 25 | + */ |
|
| 26 | + public function getData(int $page, int $perPage); |
|
| 27 | 27 | } |
| 28 | 28 | \ No newline at end of file |
@@ -6,38 +6,38 @@ |
||
| 6 | 6 | |
| 7 | 7 | class EloquentDataProvider implements DataProviderInterface |
| 8 | 8 | { |
| 9 | - protected $query; |
|
| 9 | + protected $query; |
|
| 10 | 10 | |
| 11 | - /** |
|
| 12 | - * EloquentDataProvider constructor. |
|
| 13 | - * @param Builder $query |
|
| 14 | - */ |
|
| 15 | - public function __construct(Builder $query) |
|
| 16 | - { |
|
| 17 | - $this->query = $query; |
|
| 18 | - } |
|
| 11 | + /** |
|
| 12 | + * EloquentDataProvider constructor. |
|
| 13 | + * @param Builder $query |
|
| 14 | + */ |
|
| 15 | + public function __construct(Builder $query) |
|
| 16 | + { |
|
| 17 | + $this->query = $query; |
|
| 18 | + } |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * @inheritdoc |
|
| 22 | - */ |
|
| 23 | - public function getCount(): int |
|
| 24 | - { |
|
| 25 | - return $this->query->count(); |
|
| 26 | - } |
|
| 20 | + /** |
|
| 21 | + * @inheritdoc |
|
| 22 | + */ |
|
| 23 | + public function getCount(): int |
|
| 24 | + { |
|
| 25 | + return $this->query->count(); |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * @inheritdoc |
|
| 30 | - */ |
|
| 31 | - public function getTotalPages(int $perPage): int |
|
| 32 | - { |
|
| 33 | - return ceil($this->getCount() / $perPage); |
|
| 34 | - } |
|
| 28 | + /** |
|
| 29 | + * @inheritdoc |
|
| 30 | + */ |
|
| 31 | + public function getTotalPages(int $perPage): int |
|
| 32 | + { |
|
| 33 | + return ceil($this->getCount() / $perPage); |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * @inheritdoc |
|
| 38 | - */ |
|
| 39 | - public function getData(int $page, int $perPage) |
|
| 40 | - { |
|
| 41 | - return (clone $this->query)->paginate($perPage, ['*'], 'page', $page); |
|
| 42 | - } |
|
| 36 | + /** |
|
| 37 | + * @inheritdoc |
|
| 38 | + */ |
|
| 39 | + public function getData(int $page, int $perPage) |
|
| 40 | + { |
|
| 41 | + return (clone $this->query)->paginate($perPage, ['*'], 'page', $page); |
|
| 42 | + } |
|
| 43 | 43 | } |
| 44 | 44 | \ No newline at end of file |
@@ -10,103 +10,103 @@ |
||
| 10 | 10 | |
| 11 | 11 | class GridView |
| 12 | 12 | { |
| 13 | - /** |
|
| 14 | - * @var DataProviderInterface |
|
| 15 | - */ |
|
| 16 | - public $dataProvider; |
|
| 17 | - |
|
| 18 | - /** |
|
| 19 | - * @var array |
|
| 20 | - */ |
|
| 21 | - public $columns = []; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * @var string |
|
| 25 | - */ |
|
| 26 | - public $columnClass = AttributeColumn::class; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * @var string|RendererInterface |
|
| 30 | - */ |
|
| 31 | - public $renderer = DefaultRenderer::class; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @var int |
|
| 35 | - */ |
|
| 36 | - public $rowsPerPage = 25; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * @var array |
|
| 40 | - */ |
|
| 41 | - public $tableHtmlOptions = [ |
|
| 42 | - 'class' => 'table table-bordered gridview-table', |
|
| 43 | - ]; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * GridView constructor. |
|
| 47 | - * @param array $config |
|
| 48 | - */ |
|
| 49 | - public function __construct(array $config) |
|
| 50 | - { |
|
| 51 | - GridViewHelper::loadConfig($this, $config); |
|
| 52 | - |
|
| 53 | - GridViewHelper::testConfig($this, [ |
|
| 54 | - 'dataProvider' => DataProviderInterface::class, |
|
| 55 | - 'columns' => 'array', |
|
| 56 | - 'columnClass' => BaseColumn::class, |
|
| 57 | - 'renderer' => RendererInterface::class, |
|
| 58 | - 'rowsPerPage' => 'int', |
|
| 59 | - 'tableHtmlOptions' => 'array', |
|
| 60 | - ]); |
|
| 61 | - |
|
| 62 | - $this->buildColumns(); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Build columns into objects |
|
| 67 | - */ |
|
| 68 | - protected function buildColumns() |
|
| 69 | - { |
|
| 70 | - foreach ($this->columns as &$column) { |
|
| 71 | - |
|
| 72 | - if (is_object($column)) { |
|
| 73 | - continue; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - $className = $column['class'] ?? $this->columnClass; |
|
| 77 | - $column = new $className($column); |
|
| 78 | - } |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Makes an instance |
|
| 83 | - * @param $params |
|
| 84 | - * @return GridView |
|
| 85 | - */ |
|
| 86 | - public static function make($params) |
|
| 87 | - { |
|
| 88 | - return new self($params); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * Draws widget and return html code |
|
| 93 | - * @return string |
|
| 94 | - */ |
|
| 95 | - public function render() |
|
| 96 | - { |
|
| 97 | - if (!is_object($this->renderer)) { |
|
| 98 | - $this->renderer = new $this->renderer; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - return $this->renderer->render($this); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * Wrapper for draw method |
|
| 106 | - * @see View::draw() |
|
| 107 | - */ |
|
| 108 | - public function __toString() |
|
| 109 | - { |
|
| 110 | - return $this->render(); |
|
| 111 | - } |
|
| 13 | + /** |
|
| 14 | + * @var DataProviderInterface |
|
| 15 | + */ |
|
| 16 | + public $dataProvider; |
|
| 17 | + |
|
| 18 | + /** |
|
| 19 | + * @var array |
|
| 20 | + */ |
|
| 21 | + public $columns = []; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * @var string |
|
| 25 | + */ |
|
| 26 | + public $columnClass = AttributeColumn::class; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * @var string|RendererInterface |
|
| 30 | + */ |
|
| 31 | + public $renderer = DefaultRenderer::class; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @var int |
|
| 35 | + */ |
|
| 36 | + public $rowsPerPage = 25; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * @var array |
|
| 40 | + */ |
|
| 41 | + public $tableHtmlOptions = [ |
|
| 42 | + 'class' => 'table table-bordered gridview-table', |
|
| 43 | + ]; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * GridView constructor. |
|
| 47 | + * @param array $config |
|
| 48 | + */ |
|
| 49 | + public function __construct(array $config) |
|
| 50 | + { |
|
| 51 | + GridViewHelper::loadConfig($this, $config); |
|
| 52 | + |
|
| 53 | + GridViewHelper::testConfig($this, [ |
|
| 54 | + 'dataProvider' => DataProviderInterface::class, |
|
| 55 | + 'columns' => 'array', |
|
| 56 | + 'columnClass' => BaseColumn::class, |
|
| 57 | + 'renderer' => RendererInterface::class, |
|
| 58 | + 'rowsPerPage' => 'int', |
|
| 59 | + 'tableHtmlOptions' => 'array', |
|
| 60 | + ]); |
|
| 61 | + |
|
| 62 | + $this->buildColumns(); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Build columns into objects |
|
| 67 | + */ |
|
| 68 | + protected function buildColumns() |
|
| 69 | + { |
|
| 70 | + foreach ($this->columns as &$column) { |
|
| 71 | + |
|
| 72 | + if (is_object($column)) { |
|
| 73 | + continue; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + $className = $column['class'] ?? $this->columnClass; |
|
| 77 | + $column = new $className($column); |
|
| 78 | + } |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Makes an instance |
|
| 83 | + * @param $params |
|
| 84 | + * @return GridView |
|
| 85 | + */ |
|
| 86 | + public static function make($params) |
|
| 87 | + { |
|
| 88 | + return new self($params); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * Draws widget and return html code |
|
| 93 | + * @return string |
|
| 94 | + */ |
|
| 95 | + public function render() |
|
| 96 | + { |
|
| 97 | + if (!is_object($this->renderer)) { |
|
| 98 | + $this->renderer = new $this->renderer; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + return $this->renderer->render($this); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * Wrapper for draw method |
|
| 106 | + * @see View::draw() |
|
| 107 | + */ |
|
| 108 | + public function __toString() |
|
| 109 | + { |
|
| 110 | + return $this->render(); |
|
| 111 | + } |
|
| 112 | 112 | } |
| 113 | 113 | \ No newline at end of file |