Completed
Push — master ( 481507...e81850 )
by Denis
01:52
created

GridView::configTests()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
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\BaseRenderer;
10
use Woo\GridView\Traits\Configurable;
11
12
class GridView
13
{
14
    use Configurable;
15
16
    /**
17
     * @var DataProviderInterface
18
     */
19
    public $dataProvider;
20
21
    /**
22
     * @var array
23
     */
24
    public $columns = [];
25
26
    /**
27
     * @var array
28
     */
29
    public $columnOptions = [
30
        'class' => AttributeColumn::class,
31
    ];
32
33
    /**
34
     * @var string|BaseRenderer
35
     */
36
    public $renderer = DefaultRenderer::class;
37
38
    /**
39
     * @var array
40
     */
41
    public $rendererOptions = [];
42
43
    /**
44
     * @var int
45
     */
46
    public $rowsPerPage = 25;
47
48
    /**
49
     * @var array
50
     */
51
    public $tableHtmlOptions = [
52
        'class' => 'table table-bordered gridview-table',
53
    ];
54
55
    /**
56
     * GridView constructor.
57
     * @param array $config
58
     * @throws Exceptions\GridViewConfigException
59
     */
60
    public function __construct(array $config)
61
    {
62
        $this->loadConfig($config);
63
        $this->buildColumns();
64
    }
65
66
    /**
67
     * @return array
68
     */
69
    protected function configTests(): array
70
    {
71
        return [
72
            'dataProvider' => DataProviderInterface::class,
73
            'columns' => 'array',
74
            'renderer' => BaseRenderer::class,
75
            'rowsPerPage' => 'int',
76
            'tableHtmlOptions' => 'array',
77
        ];
78
    }
79
80
    /**
81
     * Build columns into objects
82
     */
83
    protected function buildColumns()
84
    {
85
        foreach ($this->columns as &$columnOptions) {
86
87
            /**
88
             * In case of when column is already build
89
             */
90
            if (is_object($columnOptions)) {
91
                continue;
92
            }
93
94
            /**
95
             * When only attribute name/value passed
96
             */
97
            if (is_string($columnOptions)) {
98
                $columnOptions = [
99
                    'value' => $columnOptions,
100
                ];
101
            }
102
103
            $columnOptions = array_merge($this->columnOptions, $columnOptions);
104
105
            $className = GridViewHelper::resolveAlias('column', $columnOptions['class']);
106
            $columnOptions = new $className($columnOptions);
107
        }
108
    }
109
110
    /**
111
     * Makes an instance
112
     * @param $params
113
     * @return GridView
114
     * @throws Exceptions\GridViewConfigException
115
     */
116
    public static function make($params)
117
    {
118
        return new self($params);
119
    }
120
121
    /**
122
     * Draws widget and return html code
123
     * @return string
124
     */
125
    public function render()
126
    {
127
        if (!is_object($this->renderer)) {
128
129
            $className = GridViewHelper::resolveAlias('renderer', $this->renderer);
130
131
            $this->renderer = new $className($this->rendererOptions);
132
        }
133
134
        return $this->renderer->render($this);
135
    }
136
137
    /**
138
     * Wrapper for draw method
139
     * @see View::draw()
140
     */
141
    public function __toString()
142
    {
143
        return $this->render();
144
    }
145
}