Completed
Pull Request — master (#3)
by Denis
01:50 queued 23s
created

BaseColumn::contentHtmlOptions()   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 1
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 array
33
     */
34
    public $headerHtmlOptions = [];
35
36
    /**
37
     * @var array
38
     */
39
    public $contentHtmlOptions = [];
40
41
    /**
42
     * @var array - allowed: raw, url, email, text, image
43
     */
44
    public $formatters = [];
45
46
    /**
47
     * Value when column is empty
48
     * @var string
49
     */
50
    public $emptyValue = '-';
51
52
    /**
53
     * BaseColumn constructor.
54
     * @param array $config
55
     * @throws \Woo\GridView\Exceptions\GridViewConfigException
56
     */
57
    public function __construct(array $config)
58
    {
59
        $this->loadConfig($config);
60
61
        $this->buildFilter();
62
    }
63
64
    protected function buildFilter()
65
    {
66
        if (is_null($this->filter) || is_object($this->filter)) {
67
            return;
68
        }
69
70
        if (is_string($this->filter)) {
71
            $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...
72
                'class' => $this->filter,
73
                'name' => $this->value,
74
            ];
75
        }
76
77
        if (empty($this->filter['class'])) {
78
            $this->filter['class'] = TextFilter::class;
79
        }
80
81
        if (empty($this->filter['name'])) {
82
            $this->filter['name'] = $this->value;
83
        }
84
85
        $className = GridViewHelper::resolveAlias('filter', $this->filter['class']);
86
        $this->filter = new $className($this->filter);
87
    }
88
89
    /**
90
     * @return array
91
     */
92
    protected function configTests(): array
93
    {
94
        return [
95
            'title' => 'string',
96
            'value' => 'any',
97
            'headerHtmlOptions' => 'array',
98
            'contentHtmlOptions' => 'array',
99
            'formatters' => 'array',
100
            'emptyValue' => 'string',
101
            'filter' => BaseFilter::class . '|null',
102
        ];
103
    }
104
105
    /**
106
     * Formatted header html options
107
     * @return string
108
     */
109
    public function compileHeaderHtmlOptions() : string
110
    {
111
        return GridViewHelper::htmlOptionsToString($this->headerHtmlOptions);
112
    }
113
114
    /**
115
     * Formatted content html options
116
     * @param array $context
117
     * @return string
118
     */
119
    public function compileContentHtmlOptions(array $context) : string
120
    {
121
        return GridViewHelper::htmlOptionsToString($this->contentHtmlOptions, $context);
122
    }
123
124
    /**
125
     * Render column value for row
126
     * @param array|object $row
127
     * @return string|mixed
128
     */
129
    protected abstract function _renderValue($row);
130
131
    /**
132
     * Renders column content
133
     * @param $row
134
     * @return string
135
     */
136
    public function renderValue($row)
137
    {
138
        $value = $this->_renderValue($row);
139
140
        foreach ($this->formatters as $formatter) {
141
            $className = GridViewHelper::resolveAlias('formatter', $formatter);
142
            $value = (new $className)->format($value);
143
        }
144
145
        return $value;
146
    }
147
148
}