Completed
Push — master ( 481507...e81850 )
by Denis
01:52
created

ActionsColumn::_renderValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
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
    /**
33
     * @var string
34
     */
35
    public $contentFormat = 'raw';
36
37
    /**
38
     * @return array
39
     */
40
    protected function configTests(): array
41
    {
42
        return array_merge(parent::configTests(), [
43
            'value' => 'string',
44
            'additionalActions' => 'array',
45
            'actionsUrls' => 'any',
46
        ]);
47
    }
48
49
    /**
50
     * @return array
51
     */
52
    public function basicActions()
53
    {
54
        return [
55
            'show' => function($model) {
56
                return '<a href="' . call_user_func($this->actionsUrls, $model)['show'] . '">View</a>';
57
            },
58
            'edit' => function($model) {
59
                return '<a href="' . call_user_func($this->actionsUrls, $model)['edit'] . '">Edit</a>';
60
            },
61
            'delete' => function($model) {
62
                return '<a href="' . call_user_func($this->actionsUrls, $model)['delete'] . '">Delete</a>';
63
            },
64
        ];
65
    }
66
67
    /**
68
     * @inheritdoc
69
     */
70
    public function _renderValue($row)
71
    {
72
        $result = $this->value;
73
74
        $actions = array_merge($this->basicActions(), $this->additionalActions);
75
76
        foreach ($actions as $key => $action) {
77
            if (strpos($result, '{' . $key . '}') === false) {
78
                continue;
79
            }
80
81
            $result = str_replace('{' . $key . '}', $action($row), $result);
82
        }
83
84
        return $result;
85
    }
86
}