Completed
Push — master ( f15fe2...9a8393 )
by Pavel
07:23 queued 11s
created

AbstractColumn::getAttribute()   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
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace Pfilsx\DataGrid\Grid\Columns;
5
6
7
use Pfilsx\DataGrid\DataGridServiceContainer;
8
use Pfilsx\DataGrid\Grid\Filters\AbstractFilter;
9
use Twig\Template;
10
11
abstract class AbstractColumn
12
{
13
    protected $attributes = [];
14
15
    protected $value = null;
16
17
    protected $label = '';
18
    /**
19
     * @var AbstractFilter
20
     */
21
    protected $filter;
22
23
    protected $filterValue;
24
25
    protected $sort = false;
26
27
    protected $isVisible = true;
28
29
    /**
30
     * @var Template
31
     */
32
    protected $template;
33
    /**
34
     * @var DataGridServiceContainer
35
     *
36
     */
37
    protected $container;
38
39
    protected $translationDomain = null;
40
41
    public function __construct(DataGridServiceContainer $container, array $config = [])
42
    {
43
44
        $this->container = $container;
45
        $this->setConfiguration($config);
46
        $this->checkConfiguration();
47
    }
48
49
    protected function setConfiguration($config)
50
    {
51
        foreach ($config as $key => $value) {
52
            $setter = 'set' . ucfirst($key);
53
            if (method_exists($this, $setter)) {
54
                $this->$setter($value);
55
            }
56
        }
57
    }
58
59
    protected function checkConfiguration()
60
    {
61
62
    }
63
64
    public function getAttributes()
65
    {
66
        return $this->attributes;
67
    }
68
69
    public function hasAttributes()
70
    {
71
        return !empty($this->attributes);
72
    }
73
74
    /**
75
     * @param mixed $attributes
76
     */
77
    protected function setAttributes($attributes): void
78
    {
79
        $this->attributes = $attributes;
80
    }
81
82
    /**
83
     * @return mixed
84
     */
85
    public function getValue()
86
    {
87
        return $this->value;
88
    }
89
90
    /**
91
     * @param mixed $value
92
     */
93
    protected function setValue($value): void
94
    {
95
        $this->value = $value;
96
    }
97
98
    /**
99
     * @return string
100
     */
101
    public function getLabel(): string
102
    {
103
        return $this->label;
104
    }
105
106
    /**
107
     * @param string $label
108
     */
109
    protected function setLabel(string $label): void
110
    {
111
        $this->label = $label;
112
    }
113
114
    public function hasFilter()
115
    {
116
        return is_subclass_of($this->filter, AbstractFilter::class);
117
    }
118
119
    public function getFilter()
120
    {
121
        return $this->filter;
122
    }
123
124
    public function setFilter(array $filter)
125
    {
126
        $filterClass = $filter['class'];
127
        unset($filter['class']);
128
        $filter['template'] = $this->template;
129
        /** @var AbstractFilter $filterObj */
130
        $this->filter = new $filterClass($this->container, $filter);
131
    }
132
133
134
    public function hasSort()
135
    {
136
        return $this->sort !== false && !empty($this->attribute);
137
    }
138
139
    public function setSort($direction)
140
    {
141
        $this->sort = $direction;
142
    }
143
144
    public function getSort()
145
    {
146
        return $this->sort;
147
    }
148
149
150
    public function getFilterValue()
151
    {
152
        return $this->filterValue;
153
    }
154
155
    public function setFilterValue($value)
156
    {
157
        $this->filterValue = $value;
158
    }
159
160
    abstract public function getHeadContent();
161
162
    abstract public function getFilterContent();
163
164
    abstract public function getCellContent($entity);
165
166
    /**
167
     * @return mixed
168
     */
169
    public function getTemplate()
170
    {
171
        return $this->template;
172
    }
173
174
    /**
175
     * @param mixed $template
176
     */
177
    public function setTemplate($template): void
178
    {
179
        $this->template = $template;
180
        if ($this->hasFilter()) {
181
            $this->filter->setTemplate($template);
182
        }
183
    }
184
185
    /**
186
     * @return bool
187
     */
188
    public function isVisible(): bool
189
    {
190
        return $this->isVisible;
191
    }
192
193
    /**
194
     * @param bool $visibility
195
     */
196
    protected function setVisible(bool $visibility): void
197
    {
198
        $this->isVisible = $visibility;
199
    }
200
201
    public function setTranslationDomain(?string $domain): void {
202
        $this->translationDomain = $domain;
203
    }
204
205
}
206