Completed
Pull Request — master (#3)
by Denis
01:34
created

ActionsColumn   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 86
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A buildActions() 0 20 4
A configTests() 0 7 1
A _renderValue() 0 10 2
1
<?php
2
3
namespace Woo\GridView\Columns;
4
5
use Woo\GridView\GridViewHelper;
6
7
class ActionsColumn extends BaseColumn
8
{
9
    /**
10
     * By default is empty for this column
11
     * @var string
12
     */
13
    public $title = '';
14
15
    /**
16
     * Value contains short codes for actions
17
     * @var array
18
     */
19
    public $value = [];
20
21
    /**
22
     * @var bool
23
     */
24
    public $sortable = false;
25
26
    /**
27
     * @var string
28
     */
29
    public $formatters = [];
30
31
    /**
32
     * @var string
33
     */
34
    public $delimiter = ' ';
35
36
    /**
37
     * @var null
38
     */
39
    public $filter = null;
40
41
    public function __construct(array $config)
42
    {
43
        parent::__construct($config);
44
        $this->buildActions();
45
    }
46
47
    protected function buildActions()
48
    {
49
        foreach ($this->value as &$action) {
50
            if (is_object($action)) {
51
                continue;
52
            }
53
54
            if (is_string($action)) {
55
                $tmp = explode(':', $action);
56
57
                $action = [
58
                    'class' => array_shift($tmp),
59
                    'args' => $tmp,
60
                ];
61
            }
62
63
            $className = GridViewHelper::resolveAlias('action', $action['class']);
64
            $action = new $className(...$action['args']);
65
        }
66
    }
67
68
    /**
69
     * @return array
70
     */
71
    protected function configTests(): array
72
    {
73
        return array_merge(parent::configTests(), [
74
            'value' => 'array',
75
            'delimiter' => 'string',
76
        ]);
77
    }
78
79
    /**
80
     * @inheritdoc
81
     */
82
    public function _renderValue($row)
83
    {
84
        $result = [];
85
86
        foreach ($this->value as $action) {
87
            $result[] = $action->render($row);
88
        }
89
90
        return implode($this->delimiter, $result);
91
    }
92
}