1
|
|
|
<?php namespace PascalKleindienst\FormListGenerator\Fields; |
2
|
|
|
|
3
|
|
|
use AdamWathan\Form\FormBuilder; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Abstract Field for Form generator |
7
|
|
|
* @package \PascalKleindienst\FormListGenerator\Fields |
8
|
|
|
*/ |
9
|
|
|
abstract class AbstractField |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var string List field name. |
13
|
|
|
*/ |
14
|
|
|
public $fieldName; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string Display mode. Text, number |
18
|
|
|
*/ |
19
|
|
|
public $type = 'text'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string The label of the field |
23
|
|
|
*/ |
24
|
|
|
public $label; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string Specifies a default value when value is empty. |
28
|
|
|
*/ |
29
|
|
|
public $default; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string Specify a CSS class to attach to the input element. |
33
|
|
|
*/ |
34
|
|
|
public $cssClass; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array Raw field configuration. |
38
|
|
|
*/ |
39
|
|
|
public $config; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var array Array of attributes for the input element |
43
|
|
|
*/ |
44
|
|
|
public $attributes = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var boolean Whether the input is disabled or not |
48
|
|
|
*/ |
49
|
|
|
public $disabled = false; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var boolean Whether the input is required or not |
53
|
|
|
*/ |
54
|
|
|
public $required = false; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var boolean Whether the input is readonly or not |
58
|
|
|
*/ |
59
|
|
|
public $readOnly = false; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var string Description which is displayed under the input element |
63
|
|
|
*/ |
64
|
|
|
public $description; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var \AdamWathan\Form\FormBuilder |
68
|
|
|
*/ |
69
|
|
|
protected $builder; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var \AdamWathan\Form\Elements\Element |
73
|
|
|
*/ |
74
|
|
|
protected $input; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Register Config keys |
78
|
|
|
* |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
|
|
abstract protected function registerConfigKeys(); |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Constructor. |
85
|
|
|
* @param string $name |
86
|
|
|
* @param array $config |
87
|
|
|
*/ |
88
|
63 |
|
public function __construct($name, array $config) |
89
|
|
|
{ |
90
|
63 |
|
$this->fieldName = $name; |
91
|
63 |
|
$this->config = $config; |
92
|
63 |
|
$this->builder = new FormBuilder(); |
93
|
63 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Setup the field properties |
97
|
|
|
* |
98
|
|
|
* @return void |
99
|
|
|
*/ |
100
|
63 |
|
public function setup() |
101
|
|
|
{ |
102
|
|
|
// type |
103
|
63 |
|
$this->type = isset($this->config['type']) ? strtolower($this->config['type']) : $this->type; |
104
|
|
|
|
105
|
|
|
// save value of properties if they exist |
106
|
63 |
|
$configKeys = $this->registerConfigKeys() + ['cssClass', 'default', 'description', 'label', 'readOnly', 'disabled', 'required', 'attributes']; |
107
|
|
|
|
108
|
63 |
View Code Duplication |
foreach ($configKeys as $key) { |
|
|
|
|
109
|
63 |
|
if (isset($this->config[$key])) { |
110
|
57 |
|
$this->{$key} = $this->config[$key]; |
111
|
57 |
|
} |
112
|
63 |
|
} |
113
|
|
|
|
114
|
|
|
// css class |
115
|
63 |
|
$this->cssClass .= ' form-control'; |
116
|
63 |
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Set attributes for the input |
120
|
|
|
* |
121
|
|
|
* @return void |
122
|
|
|
*/ |
123
|
18 |
|
protected function setAttributes() |
124
|
|
|
{ |
125
|
18 |
|
foreach ($this->attributes as $attr => $val) { |
126
|
6 |
|
$this->input->attribute($attr, $val); |
127
|
18 |
|
} |
128
|
18 |
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Mark input as required if necessary |
132
|
|
|
* |
133
|
|
|
* @param string $labelSuffix add a suffix like '*' to the label |
134
|
|
|
* @return void |
135
|
|
|
*/ |
136
|
9 |
|
protected function setRequired($labelSuffix = '') |
137
|
|
|
{ |
138
|
9 |
|
if ($this->required) { |
139
|
|
|
$this->label .= $labelSuffix; |
140
|
|
|
$this->input->required(); |
141
|
|
|
} |
142
|
9 |
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Add a label after the input |
146
|
|
|
* |
147
|
|
|
* @param string $label |
148
|
|
|
* @param string $class |
149
|
|
|
* @return \AdamWathan\Form\Elements\Element |
150
|
|
|
*/ |
151
|
18 |
|
protected function labelAfterInput($label, $class = 'd-block') |
152
|
|
|
{ |
153
|
18 |
|
return $this->builder->label($label)->addClass($class)->after($this->input); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Get the record for this field |
158
|
|
|
* |
159
|
|
|
* @param array $records |
160
|
|
|
* @return mixed |
161
|
|
|
*/ |
162
|
27 |
|
protected function getRecord(array $records) |
163
|
|
|
{ |
164
|
27 |
|
return array_key_exists($this->fieldName, $records) ? $records[$this->fieldName] : null; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Get option values for dropdown, radio and checkboxes |
169
|
|
|
* |
170
|
|
|
* @return array |
171
|
|
|
*/ |
172
|
21 |
|
protected function getOptions() |
173
|
|
|
{ |
174
|
|
|
// Check for options callable |
175
|
21 |
|
$options = $this->options; |
|
|
|
|
176
|
21 |
|
if (is_callable($options)) { |
177
|
|
|
$options = call_user_func($options); |
178
|
|
|
} |
179
|
|
|
|
180
|
21 |
|
return $options; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
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.