Passed
Push — master ( d57bd3...3dbf58 )
by Churakov
03:47
created

BaseColumn::renderHtmlOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace ChurakovMike\EasyGrid\Columns;
4
5
use ChurakovMike\EasyGrid\Filters\BaseFilter;
6
use ChurakovMike\EasyGrid\Filters\TextFilter;
7
use ChurakovMike\EasyGrid\Formatters\BaseFormatter;
8
use ChurakovMike\EasyGrid\Formatters\HtmlFormatter;
9
use ChurakovMike\EasyGrid\Formatters\TextFormatter;
10
use ChurakovMike\EasyGrid\Interfaces\Formattable;
11
use ChurakovMike\EasyGrid\Traits\Configurable;
12
13
/**
14
 * Class BaseColumn.
15
 * @package ChurakovMike\EasyGrid\Columns
16
 *
17
 * @property string $label
18
 * @property string $attribute
19
 * @property string|\Closure|mixed $value
20
 * @property BaseFilter $filter
21
 * @property string|BaseFormatter $format
22
 */
23
abstract class BaseColumn
24
{
25
    use Configurable;
26
27
    /**
28
     * @var string $label
29
     */
30
    public $label;
31
32
    /**
33
     * @var string $attribute
34
     */
35
    public $attribute;
36
37
    /**
38
     * @var string $value
39
     */
40
    public $value;
41
42
    /**
43
     * @var string $filter
44
     */
45
    public $filter;
46
47
    /**
48
     * @var string|Formattable $format
49
     */
50
    public $format;
51
52
    /**
53
     * @var string $width
54
     */
55
    public $width;
56
57
    /**
58
     * BaseColumn constructor.
59
     * @param array $config
60
     */
61
    public function __construct(array $config)
62
    {
63
        $this->loadConfig($config);
64
        $this->buildFilter();
65
        $this->buildFormatter();
66
    }
67
68
    /**
69
     * @param $row
70
     * @return string
71
     */
72
    public function getValue($row): string
73
    {
74
    }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return string. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
75
76
    /**
77
     * Render row attribute value.
78
     *
79
     * @param $row
80
     * @return mixed
81
     */
82
    public function render($row)
83
    {
84
        return $this->formatTo($this->getValue($row));
85
    }
86
87
    /**
88
     * Format value with formatter.
89
     *
90
     * @param $value
91
     * @return mixed
92
     */
93
    public function formatTo($value)
94
    {
95
        return $this->format->format($value);
96
    }
97
98
    /**
99
     * Get title for grid head.
100
     *
101
     * @return string
102
     */
103
    public function getLabel(): string
104
    {
105
        return $this->label ?? ucfirst($this->attribute);
106
    }
107
108
    /**
109
     * Get attribute.
110
     *
111
     * @return string|null
112
     */
113
    public function getAttribute()
114
    {
115
        return $this->attribute;
116
    }
117
118
    /**
119
     * Build filter for grid.
120
     *
121
     * @return void
122
     */
123
    protected function buildFilter()
124
    {
125
        if (is_null($this->filter)) {
126
            $this->filter = new TextFilter([
127
                'name' => $this->getAttribute(),
128
            ]);
129
        }
130
    }
131
132
    /**
133
     * Build row data formatter.
134
     *
135
     * @return void
136
     */
137
    protected function buildFormatter()
138
    {
139
        if (is_null($this->format)) {
140
            $this->format = new TextFormatter();
141
            return;
142
        }
143
144
        if (is_object($this->format) && ($this->format instanceof Formattable)) {
145
            return;
146
        } else {
147
            //todo:add another filters.
148
        }
149
150
        if (is_string($this->format)) {
151
            switch ($this->format) {
152
                case 'text':
153
                    $this->format = new TextFormatter();
154
                    break;
155
                case 'html':
156
                    $this->format = new HtmlFormatter();
157
                    break;
158
                default:
159
                    $this->format = new TextFormatter();
160
                    break;
161
            }
162
163
            return;
164
        }
165
166
        return;
167
    }
168
169
    /**
170
     * @return string
171
     */
172
    public function renderHtmlOptions(): string 
173
    {
174
        return "width='{$this->width}'%";
175
    }
176
}
177