|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Action; |
|
4
|
|
|
|
|
5
|
|
|
use LAG\AdminBundle\Action\Configuration\ActionConfiguration; |
|
6
|
|
|
use LAG\AdminBundle\Admin\AdminInterface; |
|
7
|
|
|
use LAG\AdminBundle\Admin\Filter; |
|
8
|
|
|
use LAG\AdminBundle\Field\Field; |
|
9
|
|
|
|
|
10
|
|
|
class Action implements ActionInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Action name. |
|
14
|
|
|
* |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $name; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Action title. |
|
21
|
|
|
* |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $title; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Fields displayed for this action. |
|
28
|
|
|
* |
|
29
|
|
|
* @var Field[] |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $fields = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Action permissions. |
|
35
|
|
|
* |
|
36
|
|
|
* @var string[] |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $permissions = []; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var ActionConfiguration |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $configuration; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var array |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $filters = []; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Action constructor. |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $actionName |
|
54
|
|
|
* @param ActionConfiguration $configuration |
|
55
|
|
|
*/ |
|
56
|
15 |
|
public function __construct($actionName, ActionConfiguration $configuration) |
|
57
|
|
|
{ |
|
58
|
15 |
|
$this->configuration = $configuration; |
|
59
|
15 |
|
$this->name = $actionName; |
|
60
|
15 |
|
$this->title = $configuration->getParameter('title'); |
|
61
|
15 |
|
$this->permissions = $configuration->getParameter('permissions'); |
|
62
|
15 |
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return string |
|
66
|
|
|
*/ |
|
67
|
13 |
|
public function getName() |
|
68
|
|
|
{ |
|
69
|
13 |
|
return $this->name; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
1 |
|
public function getTitle() |
|
76
|
|
|
{ |
|
77
|
1 |
|
return $this->title; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return Field[] |
|
82
|
|
|
*/ |
|
83
|
1 |
|
public function getFields() |
|
84
|
|
|
{ |
|
85
|
1 |
|
return $this->fields; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Return true if action has a field named $fieldName. |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $fieldName |
|
92
|
|
|
* |
|
93
|
|
|
* @return bool |
|
94
|
|
|
*/ |
|
95
|
|
|
public function hasField($fieldName) |
|
96
|
|
|
{ |
|
97
|
|
|
return array_key_exists($fieldName, $this->fields); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param Field[] $fields |
|
102
|
|
|
*/ |
|
103
|
|
|
public function setFields($fields) |
|
104
|
|
|
{ |
|
105
|
|
|
$this->fields = $fields; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param Field $field |
|
110
|
|
|
*/ |
|
111
|
2 |
|
public function addField(Field $field) |
|
112
|
|
|
{ |
|
113
|
2 |
|
$this->fields[$field->getName()] = $field; |
|
114
|
2 |
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return string[] |
|
118
|
|
|
*/ |
|
119
|
3 |
|
public function getPermissions() |
|
120
|
|
|
{ |
|
121
|
3 |
|
return $this->permissions; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @return array |
|
126
|
|
|
*/ |
|
127
|
|
|
public function getFilters() |
|
128
|
|
|
{ |
|
129
|
|
|
return $this->filters; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param array $filters |
|
134
|
|
|
*/ |
|
135
|
|
|
public function setFilters($filters) |
|
136
|
|
|
{ |
|
137
|
|
|
$this->filters = $filters; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param Filter $filter |
|
142
|
|
|
*/ |
|
143
|
|
|
public function addFilter(Filter $filter) |
|
144
|
|
|
{ |
|
145
|
|
|
$this->filters[] = $filter; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @return ActionConfiguration |
|
150
|
|
|
*/ |
|
151
|
12 |
|
public function getConfiguration() |
|
152
|
|
|
{ |
|
153
|
12 |
|
return $this->configuration; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Return true if the pagination is required for this action. Only action with a "multiple" load strategy require |
|
158
|
|
|
* pagination. |
|
159
|
|
|
* |
|
160
|
|
|
* @return bool |
|
161
|
|
|
*/ |
|
162
|
9 |
|
public function isPaginationRequired() |
|
163
|
|
|
{ |
|
164
|
9 |
|
return $this |
|
165
|
|
|
->configuration |
|
166
|
|
|
->getParameter('load_strategy') === AdminInterface::LOAD_STRATEGY_MULTIPLE; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Return true if one or more entities should be loaded ini this action. |
|
171
|
|
|
* |
|
172
|
|
|
* @return bool |
|
173
|
|
|
*/ |
|
174
|
|
|
public function isLoadingRequired() |
|
175
|
|
|
{ |
|
176
|
|
|
return $this |
|
|
|
|
|
|
177
|
|
|
->configuration |
|
178
|
|
|
->getParameter('load_strategy') !== AdminInterface::LOAD_STRATEGY_NONE; |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.