Completed
Push — master ( 36465f...c2bf67 )
by Adam
02:30
created

Column   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 263
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 263
rs 10
wmc 27
lcom 1
cbo 4

20 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setGrid() 0 6 1
A getGrid() 0 4 1
A getTitle() 0 4 1
A setTitle() 0 6 1
A getName() 0 4 1
A setName() 0 6 1
A isSortable() 0 4 1
A setSortable() 0 6 1
A setClickable() 0 6 1
A setRender() 0 6 1
A setPlaceholder() 0 6 1
A addDecorator() 0 6 1
A setDecorators() 0 6 1
A getDecorators() 0 4 1
A setFilter() 0 7 1
A getFilter() 0 4 1
A isFilterable() 0 4 1
C setDefaultOptions() 0 30 7
A __get() 0 12 2
1
<?php
2
3
namespace Boduch\Grid;
4
5
use Boduch\Grid\Decorators\DecoratorInterface;
6
use Boduch\Grid\Decorators\Link;
7
use Boduch\Grid\Decorators\Html;
8
use Boduch\Grid\Decorators\Placeholder;
9
use Boduch\Grid\Filters\FilterInterface;
10
11
class Column
12
{
13
    /**
14
     * @var Grid
15
     */
16
    protected $grid;
17
18
    /**
19
     * @var string
20
     */
21
    protected $title;
22
23
    /**
24
     * @var string
25
     */
26
    protected $name;
27
28
    /**
29
     * @var bool
30
     */
31
    protected $sortable = false;
32
33
    /**
34
     * @var DecoratorInterface[]
35
     */
36
    protected $decorators = [];
37
38
    /**
39
     * @var FilterInterface
40
     */
41
    protected $filter;
42
43
    /**
44
     * @var string
45
     */
46
    protected $placeholder;
47
48
    /**
49
     * @param array $options
50
     */
51
    public function __construct(array $options = [])
52
    {
53
        $this->setDefaultOptions($options);
54
    }
55
56
    /**
57
     * @param Grid $grid
58
     * @return $this
59
     */
60
    public function setGrid($grid)
61
    {
62
        $this->grid = $grid;
63
64
        return $this;
65
    }
66
67
    /**
68
     * @return Grid
69
     */
70
    public function getGrid()
71
    {
72
        return $this->grid;
73
    }
74
75
    /**
76
     * @return string
77
     */
78
    public function getTitle()
79
    {
80
        return $this->title;
81
    }
82
83
    /**
84
     * @param string $title
85
     * @return $this
86
     */
87
    public function setTitle($title)
88
    {
89
        $this->title = $title;
90
91
        return $this;
92
    }
93
94
    /**
95
     * @return string
96
     */
97
    public function getName()
98
    {
99
        return $this->name;
100
    }
101
102
    /**
103
     * @param string $name
104
     * @return $this
105
     */
106
    public function setName($name)
107
    {
108
        $this->name = $name;
109
110
        return $this;
111
    }
112
113
    /**
114
     * @return boolean
115
     */
116
    public function isSortable()
117
    {
118
        return $this->sortable;
119
    }
120
121
    /**
122
     * @param boolean $flag
123
     * @return $this
124
     */
125
    public function setSortable($flag)
126
    {
127
        $this->sortable = (bool) $flag;
128
129
        return $this;
130
    }
131
132
    /**
133
     * @param \Closure $closure
134
     * @return $this
135
     */
136
    public function setClickable(\Closure $closure)
137
    {
138
        $this->addDecorator((new Link())->render($closure));
139
140
        return $this;
141
    }
142
143
    /**
144
     * @param \Closure $closure
145
     * @return $this
146
     */
147
    public function setRender(\Closure $closure)
148
    {
149
        $this->addDecorator((new Html())->render($closure));
150
151
        return $this;
152
    }
153
154
    /**
155
     * @param string $placeholder
156
     * @return $this
157
     */
158
    public function setPlaceholder($placeholder)
159
    {
160
        $this->addDecorator(new Placeholder($placeholder));
161
162
        return $this;
163
    }
164
165
    /**
166
     * @param DecoratorInterface $decorator
167
     * @return $this
168
     */
169
    public function addDecorator(DecoratorInterface $decorator)
170
    {
171
        $this->decorators[] = $decorator;
172
173
        return $this;
174
    }
175
176
    /**
177
     * @param DecoratorInterface[] $decorators
178
     * @return $this
179
     */
180
    public function setDecorators(array $decorators)
181
    {
182
        $this->decorators = $decorators;
183
184
        return $this;
185
    }
186
187
    /**
188
     * @return DecoratorInterface[]
189
     */
190
    public function getDecorators()
191
    {
192
        return $this->decorators;
193
    }
194
195
    /**
196
     * @param FilterInterface $filter
197
     * @return $this
198
     */
199
    public function setFilter(FilterInterface $filter)
200
    {
201
        $filter->setColumn($this);
202
        $this->filter = $filter;
203
204
        return $this;
205
    }
206
207
    /**
208
     * @return FilterInterface
209
     */
210
    public function getFilter()
211
    {
212
        return $this->filter;
213
    }
214
215
    /**
216
     * @return bool
217
     */
218
    public function isFilterable()
219
    {
220
        return $this->filter !== null;
221
    }
222
223
    /**
224
     * @param array $options
225
     */
226
    protected function setDefaultOptions(array $options)
227
    {
228
        if (empty($options['name'])) {
229
            throw new \InvalidArgumentException(sprintf('Column MUST have name in %s class.', get_class($this)));
230
        }
231
232
        if (empty($options['label'])) {
233
            $options['label'] = camel_case($options['name']);
234
        }
235
236
        // placeholder MUST be the first element in options array. that's because "placeholder" decorator
237
        // can break further decorators.
238
        $placeholder = array_pull($options, 'placeholder');
239
        if (!empty($placeholder)) {
240
            $options = array_merge(['placeholder' => $placeholder], $options);
241
        }
242
243
        foreach ($options as $key => $values) {
244
            $methodName = 'set' . ucfirst(camel_case($key));
245
246
            if (method_exists($this, $methodName)) {
247
                // setDecorators() method can overwrite previously set decorators. we don't want that to happen
248
                // because user could set other decorators via setRender() or setClickable() method.
249
                if ($methodName === 'setDecorators') {
250
                    $values = array_merge($this->decorators, $values);
251
                }
252
                $this->$methodName($values);
253
            }
254
        }
255
    }
256
257
    /**
258
     * @param string $name
259
     * @return mixed
260
     */
261
    public function __get($name)
262
    {
263
        $name = camel_case($name);
264
265
        if (!isset($this->$name)) {
266
            throw new \InvalidArgumentException(
267
                sprintf("Field %s does not exist in %s class", $name, get_class($this))
268
            );
269
        }
270
271
        return $this->$name;
272
    }
273
}
274