Completed
Push — master ( 9d2442...b1f272 )
by Denis
01:49
created

ActionsColumn   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A basicActions() 0 14 1
A renderValue() 0 16 3
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 string
18
     */
19
    public $value = '{show} {edit} {delete}';
20
21
    /**
22
     * Additional actions could be added, key is short-code and value is callback
23
     * @var array
24
     */
25
    public $additionalActions = [];
26
27
    /**
28
     * @var \Closure|null
29
     */
30
    public $actionsUrls;
31
32
    public function __construct(array $config)
33
    {
34
        parent::__construct($config);
35
36
        GridViewHelper::testConfig($this, [
37
            'value' => 'string',
38
            'additionalActions' => 'array',
39
            'actionsUrls' => 'any',
40
        ]);
41
    }
42
43
    public function basicActions()
44
    {
45
        return [
46
            'show' => function($model) {
47
                return '<a href="' . call_user_func($this->actionsUrls, $model)['show'] . '">Show</a>';
48
            },
49
            'edit' => function($model) {
50
                return '<a href="' . call_user_func($this->actionsUrls, $model)['edit'] . '">Edit</a>';
51
            },
52
            'delete' => function($model) {
53
                return '<a href="' . call_user_func($this->actionsUrls, $model)['delete'] . '">Remove</a>';
54
            },
55
        ];
56
    }
57
58
    public function renderValue($row)
59
    {
60
        $result = $this->value;
61
62
        $actions = array_merge($this->basicActions(), $this->additionalActions);
63
64
        foreach ($actions as $key => $action) {
65
            if (strpos($result, '{' . $key . '}') === false) {
66
                continue;
67
            }
68
69
            $result = str_replace('{' . $key . '}', $action($row), $result);
70
        }
71
72
        return $result;
73
    }
74
}