1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Deller [email protected] |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Nnx\DataGrid\Column; |
7
|
|
|
|
8
|
|
|
use Nnx\DataGrid\Column\Action\ActionInterface; |
9
|
|
|
use Zend\View\Helper\Url; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Action |
13
|
|
|
* @package Nnx\DataGrid\Column |
14
|
|
|
*/ |
15
|
|
|
class Action extends AbstractColumn implements ActionAwareInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $actions = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Url |
24
|
|
|
*/ |
25
|
|
|
protected $urlHelper; |
26
|
|
|
|
27
|
|
|
|
28
|
|
View Code Duplication |
public function __construct(array $options = []) |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
parent::__construct($options); |
31
|
|
|
if (array_key_exists('actions', $options)) { |
32
|
|
|
if (!is_array($options['actions'])) { |
33
|
|
|
throw new Exception\InvalidArgumentException( |
34
|
|
|
'Действия для колонки действий должны приходить в виде массива' |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
$this->setActions($options['actions']); |
38
|
|
|
unset($options['actions']); |
39
|
|
|
} |
40
|
|
|
if (array_key_exists('urlHelper', $options)) { |
41
|
|
|
$this->setUrlHelper($options['urlHelper']); |
42
|
|
|
} |
43
|
|
|
$this->setOptions($options); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Возвращает набор действий в колонке |
48
|
|
|
* @return array|ActionInterface[] |
49
|
|
|
*/ |
50
|
|
|
public function getActions() |
51
|
|
|
{ |
52
|
|
|
return $this->actions; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Устанавливает набор действий в колонку |
57
|
|
|
* @param array $actions |
58
|
|
|
* @return $this |
59
|
|
|
*/ |
60
|
|
|
public function setActions($actions) |
61
|
|
|
{ |
62
|
|
|
$this->actions = $actions; |
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Добавляет действие в колонку |
68
|
|
|
* @param ActionInterface|array $action |
69
|
|
|
* @return $this |
70
|
|
|
*/ |
71
|
|
|
public function addAction($action) |
72
|
|
|
{ |
73
|
|
|
if (!$action instanceof ActionInterface && is_array($action)) { |
74
|
|
|
$actionFactory = new Action\Factory(); |
|
|
|
|
75
|
|
|
$action['urlHelper'] = $this->getUrlHelper(); |
76
|
|
|
$action = $actionFactory->create($action); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
$this->actions[$action->getName()] = $action; |
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return Url |
84
|
|
|
*/ |
85
|
|
|
public function getUrlHelper() |
86
|
|
|
{ |
87
|
|
|
return $this->urlHelper; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param Url $urlHelper |
92
|
|
|
* @return $this |
93
|
|
|
*/ |
94
|
|
|
public function setUrlHelper($urlHelper) |
95
|
|
|
{ |
96
|
|
|
$this->urlHelper = $urlHelper; |
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
} |
|
|
|
|
100
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.