Completed
Push — master ( 8408ee...8e6ba3 )
by Denis
01:35
created

BaseColumn::compileHeaderHtmlOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Woo\GridView\Columns;
4
5
use Woo\GridView\Filters\BaseFilter;
6
use Woo\GridView\Filters\TextFilter;
7
use Woo\GridView\GridViewHelper;
8
use Woo\GridView\Traits\Configurable;
9
10
abstract class BaseColumn
11
{
12
    use Configurable;
13
14
    /**
15
     * Column title
16
     * @var string
17
     */
18
    public $title = '';
19
20
    /**
21
     * Column value. Could be an attribute,
22
     * @var string|mixed
23
     */
24
    public $value = '';
25
26
    /**
27
     * @var BaseFilter
28
     */
29
    public $filter;
30
31
    /**
32
     * @var boolean|string
33
     */
34
    public $sortable = true;
35
36
    /**
37
     * @var array
38
     */
39
    public $headerHtmlOptions = [];
40
41
    /**
42
     * @var array
43
     */
44
    public $contentHtmlOptions = [];
45
46
    /**
47
     * @var array - allowed: raw, url, email, text, image
48
     */
49
    public $formatters = ['text'];
50
51
    /**
52
     * Value when column is empty
53
     * @var string
54
     */
55
    public $emptyValue = '-';
56
57
    /**
58
     * BaseColumn constructor.
59
     * @param array $config
60
     * @throws \Woo\GridView\Exceptions\GridViewConfigException
61
     */
62
    public function __construct(array $config)
63
    {
64
        $this->loadConfig($config);
65
66
        $this->buildFilter();
67
    }
68
69
    /**
70
     * Allows to get sortable column's name
71
     */
72
    public function getSortableName()
73
    {
74
        if ($this->sortable === false) {
75
            return false;
76
        }
77
78
        if (is_scalar($this->sortable)) {
79
            return $this->sortable;
80
        }
81
82
        if (is_scalar($this->value)) {
83
            return $this->value;
84
        }
85
86
        return false;
87
    }
88
89
    protected function buildFilter()
90
    {
91
        if (is_null($this->filter) || is_object($this->filter)) {
92
            return;
93
        }
94
95
        if (is_string($this->filter)) {
96
            $this->filter = [
0 ignored issues
show
Documentation Bug introduced by
It seems like array('class' => $this->...'name' => $this->value) of type array<string,*,{"class":"string","name":"*"}> is incompatible with the declared type object<Woo\GridView\Filters\BaseFilter> of property $filter.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
97
                'class' => $this->filter,
98
                'name' => $this->value,
99
            ];
100
        }
101
102
        if (empty($this->filter['class'])) {
103
            $this->filter['class'] = TextFilter::class;
104
        }
105
106
        if (empty($this->filter['name'])) {
107
            $this->filter['name'] = $this->value;
108
        }
109
110
        $className = GridViewHelper::resolveAlias('filter', $this->filter['class']);
111
        $this->filter = new $className($this->filter);
112
    }
113
114
    /**
115
     * @return array
116
     */
117
    protected function configTests(): array
118
    {
119
        return [
120
            'title' => 'string',
121
            'value' => 'any',
122
            'headerHtmlOptions' => 'array',
123
            'contentHtmlOptions' => 'array',
124
            'formatters' => 'array',
125
            'emptyValue' => 'string',
126
            'sortable' => 'any',
127
            'filter' => BaseFilter::class . '|null',
128
        ];
129
    }
130
131
    /**
132
     * Formatted header html options
133
     * @return string
134
     */
135
    public function compileHeaderHtmlOptions() : string
136
    {
137
        return GridViewHelper::htmlOptionsToString($this->headerHtmlOptions);
138
    }
139
140
    /**
141
     * Formatted content html options
142
     * @param array $context
143
     * @return string
144
     */
145
    public function compileContentHtmlOptions(array $context) : string
146
    {
147
        return GridViewHelper::htmlOptionsToString($this->contentHtmlOptions, $context);
148
    }
149
150
    /**
151
     * Render column value for row
152
     * @param array|object $row
153
     * @return string|mixed
154
     */
155
    protected abstract function _renderValue($row);
156
157
    /**
158
     * Renders column content
159
     * @param $row
160
     * @return string
161
     */
162
    public function renderValue($row)
163
    {
164
        $value = $this->_renderValue($row);
165
166
        foreach ($this->formatters as $formatter) {
167
            $className = GridViewHelper::resolveAlias('formatter', $formatter);
168
            $value = (new $className)->format($value);
169
        }
170
171
        return $value;
172
    }
173
174
}
175