Completed
Push — master ( 9d2442...b1f272 )
by Denis
01:49
created

GridView   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 102
rs 10
c 0
b 0
f 0
wmc 8
lcom 2
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A buildColumns() 0 12 3
A make() 0 4 1
A render() 0 8 2
A __toString() 0 4 1
1
<?php
2
3
namespace Woo\GridView;
4
5
use Woo\GridView\Columns\AttributeColumn;
6
use Woo\GridView\Columns\BaseColumn;
7
use Woo\GridView\DataProviders\DataProviderInterface;
8
use Woo\GridView\Renderers\DefaultRenderer;
9
use Woo\GridView\Renderers\RendererInterface;
10
11
class GridView
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
    }
112
}