|
1
|
|
|
<?php |
|
2
|
|
|
namespace Anavel\Crud\Abstractor\Eloquent; |
|
3
|
|
|
|
|
4
|
|
|
use Anavel\Crud\Contracts\Abstractor\Field as FieldAbstractorContract; |
|
5
|
|
|
use Doctrine\DBAL\Schema\Column; |
|
6
|
|
|
use FormManager\Fields\Field as FormManagerField; |
|
7
|
|
|
use Request; |
|
8
|
|
|
|
|
9
|
|
|
class Field implements FieldAbstractorContract |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var Column |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $dbal; |
|
15
|
|
|
/** |
|
16
|
|
|
* @var FormManagerField |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $formField; |
|
19
|
|
|
protected $name; |
|
20
|
|
|
protected $value; |
|
21
|
|
|
protected $presentation; |
|
22
|
|
|
protected $validationRules; |
|
23
|
|
|
protected $functions; |
|
24
|
|
|
protected $options; |
|
25
|
|
|
protected $hideValue; |
|
26
|
|
|
protected $saveIfEmpty; |
|
27
|
|
|
protected $noValidate; |
|
28
|
|
|
|
|
29
|
5 |
|
public function __construct(Column $column, FormManagerField $formField, $name, $presentation = null) |
|
30
|
|
|
{ |
|
31
|
5 |
|
$this->dbal = $column; |
|
32
|
5 |
|
$this->formField = $formField; |
|
33
|
5 |
|
$this->name = $name; |
|
34
|
5 |
|
$this->presentation = $presentation; |
|
35
|
5 |
|
$this->validationRules = array(); |
|
36
|
5 |
|
$this->functions = array(); |
|
37
|
5 |
|
$this->options = []; |
|
38
|
5 |
|
$this->hideValue = false; |
|
39
|
5 |
|
$this->saveIfEmpty = true; |
|
40
|
5 |
|
$this->noValidate = false; |
|
41
|
5 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function __clone() |
|
44
|
|
|
{ |
|
45
|
|
|
$formField = clone $this->formField; |
|
46
|
|
|
$this->formField = $formField; |
|
47
|
|
|
$field = new Field($this->dbal, $formField, $this->name, $this->presentation); |
|
48
|
|
|
|
|
49
|
|
|
return $field; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function getName() |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->name; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param string $name |
|
59
|
|
|
*/ |
|
60
|
|
|
public function setName($name) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->name = $name; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
1 |
|
public function presentation() |
|
66
|
|
|
{ |
|
67
|
1 |
|
return transcrud($this->presentation) ? : ucfirst(str_replace('_', ' ', transcrud($this->name))); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
1 |
|
public function type() |
|
71
|
|
|
{ |
|
72
|
1 |
|
return $this->dbal->getType(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function setValidationRules($rules) |
|
76
|
|
|
{ |
|
77
|
|
|
$this->validationRules = explode('|', $rules); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
1 |
|
public function getValidationRules() |
|
81
|
|
|
{ |
|
82
|
1 |
|
if (count($this->validationRules) === 0 && $this->noValidate() === false) { |
|
83
|
1 |
|
if ($this->dbal->getNotnull()) { |
|
84
|
1 |
|
$this->validationRules[] = 'required'; |
|
85
|
1 |
|
} |
|
86
|
1 |
|
} |
|
87
|
|
|
|
|
88
|
1 |
|
return implode('|', $this->validationRules); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return array |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getValidationRulesArray() |
|
95
|
|
|
{ |
|
96
|
|
|
return explode('|', $this->getValidationRules()); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function setFunctions($functions) |
|
100
|
|
|
{ |
|
101
|
|
|
if (! is_array($functions)) { |
|
102
|
|
|
$functions = array($functions); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$this->functions = $functions; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function applyFunctions($value) |
|
109
|
|
|
{ |
|
110
|
|
|
foreach ($this->functions as $function) { |
|
111
|
|
|
if (! function_exists($function)) { |
|
112
|
|
|
throw new \Exception("Function ".$function." does not exist"); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$value = call_user_func($function, $value); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return $value; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param string $value |
|
123
|
|
|
* @return void |
|
124
|
|
|
*/ |
|
125
|
|
|
public function setValue($value) |
|
126
|
|
|
{ |
|
127
|
|
|
$this->value = $value; |
|
128
|
|
|
|
|
129
|
|
|
if (! $this->hideValue()) { |
|
130
|
|
|
$this->formField->val($this->value); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @return mixed |
|
136
|
|
|
*/ |
|
137
|
|
|
public function getValue() |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->value; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param array $options |
|
144
|
|
|
* @return void |
|
145
|
|
|
*/ |
|
146
|
|
|
public function setOptions(array $options) |
|
147
|
|
|
{ |
|
148
|
|
|
$this->options = $options; |
|
149
|
|
|
|
|
150
|
|
|
$this->formField->options($this->options); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @return array |
|
155
|
|
|
*/ |
|
156
|
|
|
public function getOptions() |
|
157
|
|
|
{ |
|
158
|
|
|
return $this->options; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
public function getFormField() |
|
162
|
|
|
{ |
|
163
|
|
|
if (! $this->hideValue()) { |
|
164
|
|
|
if (Request::old($this->name)) { |
|
165
|
|
|
$this->formField->val(Request::old($this->name)); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
return $this->formField; |
|
|
|
|
|
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* |
|
174
|
|
|
*/ |
|
175
|
|
|
public function hideValue($value = null) |
|
176
|
|
|
{ |
|
177
|
|
|
if (! is_null($value)) { |
|
178
|
|
|
$this->hideValue = $value; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
return $this->hideValue; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* |
|
186
|
|
|
*/ |
|
187
|
|
|
public function saveIfEmpty($value = null) |
|
188
|
|
|
{ |
|
189
|
|
|
if (! is_null($value)) { |
|
190
|
|
|
$this->saveIfEmpty = $value; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
return $this->saveIfEmpty; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* |
|
198
|
|
|
*/ |
|
199
|
1 |
|
public function noValidate($value = null) |
|
200
|
|
|
{ |
|
201
|
1 |
|
if (! is_null($value)) { |
|
202
|
|
|
$this->noValidate = $value; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
1 |
|
return $this->noValidate; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* @param array $attributes |
|
210
|
|
|
* @return void |
|
211
|
|
|
*/ |
|
212
|
|
|
public function setFormElementAttributes(array $attributes) |
|
213
|
|
|
{ |
|
214
|
|
|
$this->formField->attr($attributes); |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
|
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.