Completed
Pull Request — master (#3)
by Denis
01:34
created

BaseColumn::headerHtmlOptions()   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
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
    protected function buildFilter()
70
    {
71
        if (is_null($this->filter) || is_object($this->filter)) {
72
            return;
73
        }
74
75
        if (is_string($this->filter)) {
76
            $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...
77
                'class' => $this->filter,
78
                'name' => $this->value,
79
            ];
80
        }
81
82
        if (empty($this->filter['class'])) {
83
            $this->filter['class'] = TextFilter::class;
84
        }
85
86
        if (empty($this->filter['name'])) {
87
            $this->filter['name'] = $this->value;
88
        }
89
90
        $className = GridViewHelper::resolveAlias('filter', $this->filter['class']);
91
        $this->filter = new $className($this->filter);
92
    }
93
94
    /**
95
     * @return array
96
     */
97
    protected function configTests(): array
98
    {
99
        return [
100
            'title' => 'string',
101
            'value' => 'any',
102
            'headerHtmlOptions' => 'array',
103
            'contentHtmlOptions' => 'array',
104
            'formatters' => 'array',
105
            'emptyValue' => 'string',
106
            'sortable' => 'bool',
107
            'filter' => BaseFilter::class . '|null',
108
        ];
109
    }
110
111
    /**
112
     * Formatted header html options
113
     * @return string
114
     */
115
    public function compileHeaderHtmlOptions() : string
116
    {
117
        return GridViewHelper::htmlOptionsToString($this->headerHtmlOptions);
118
    }
119
120
    /**
121
     * Formatted content html options
122
     * @param array $context
123
     * @return string
124
     */
125
    public function compileContentHtmlOptions(array $context) : string
126
    {
127
        return GridViewHelper::htmlOptionsToString($this->contentHtmlOptions, $context);
128
    }
129
130
    /**
131
     * Render column value for row
132
     * @param array|object $row
133
     * @return string|mixed
134
     */
135
    protected abstract function _renderValue($row);
136
137
    /**
138
     * Renders column content
139
     * @param $row
140
     * @return string
141
     */
142
    public function renderValue($row)
143
    {
144
        $value = $this->_renderValue($row);
145
146
        foreach ($this->formatters as $formatter) {
147
            $className = GridViewHelper::resolveAlias('formatter', $formatter);
148
            $value = (new $className)->format($value);
149
        }
150
151
        return $value;
152
    }
153
154
}