Passed
Push — master ( f64044...30b8bb )
by Churakov
04:07 queued 11s
created

BaseColumn::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 5
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
     * BaseColumn constructor.
54
     * @param array $config
55
     */
56
    public function __construct(array $config)
57
    {
58
        $this->loadConfig($config);
59
        $this->buildFilter();
60
        $this->buildFormatter();
61
    }
62
63
    /**
64
     * @param $row
65
     * @return string
66
     */
67
    public function getValue($row): string
68
    {
69
    }
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...
70
71
    /**
72
     * Render row attribute value.
73
     *
74
     * @param $row
75
     * @return mixed
76
     */
77
    public function render($row)
78
    {
79
        return $this->formatTo($this->getValue($row));
80
    }
81
82
    /**
83
     * Format value with formatter.
84
     *
85
     * @param $value
86
     * @return mixed
87
     */
88
    public function formatTo($value)
89
    {
90
        return $this->format->format($value);
91
    }
92
93
    /**
94
     * Get title for grid head.
95
     *
96
     * @return string
97
     */
98
    public function getLabel(): string
99
    {
100
        return $this->label ?? ucfirst($this->attribute);
101
    }
102
103
    /**
104
     * Get attribute.
105
     *
106
     * @return string
107
     */
108
    public function getAttribute(): string
109
    {
110
        return $this->attribute;
111
    }
112
113
    /**
114
     * Build filter for grid.
115
     *
116
     * @return void
117
     */
118
    protected function buildFilter()
119
    {
120
        if (is_null($this->filter)) {
121
            $this->filter = new TextFilter([
122
                'name' => $this->getAttribute(),
123
            ]);
124
        }
125
    }
126
127
    /**
128
     * Build row data formatter.
129
     *
130
     * @return void
131
     */
132
    protected function buildFormatter()
133
    {
134
        if (is_null($this->format)) {
135
            $this->format = new TextFormatter();
136
            return;
137
        }
138
139
        if (is_object($this->format) && ($this->format instanceof Formattable)) {
140
            return;
141
        } else {
142
143
        }
144
145
        if (is_string($this->format)) {
146
            switch ($this->format) {
147
                case 'text':
148
                    $this->format = new TextFormatter();
149
                    break;
150
                case 'html':
151
                    $this->format = new HtmlFormatter();
152
                    break;
153
                default:
154
                    $this->format = new TextFormatter();
155
                    break;
156
            }
157
158
            return;
159
        }
160
161
        return;
162
    }
163
}
164