1
1
<?php
2
-
3
2
namespace CrestApps \CodeGenerator \HtmlGenerators ;
4
3
5
4
use CrestApps \CodeGenerator \HtmlGenerators \HtmlGeneratorBase ;
@@ -21,7 +20,6 @@ protected function getFieldMinValueWithName($minValue)
21
20
{
22
21
return is_null ($ minValue ) ? '' : sprintf (" 'min' => '%s', " , $ minValue );
23
22
}
24
-
25
23
/**
26
24
* Gets the maxValue attribute.
27
25
*
@@ -33,7 +31,6 @@ protected function getFieldMaxValueWithName($maxValue)
33
31
{
34
32
return is_null ($ maxValue ) ? '' : sprintf (" 'max' => '%s', " , $ maxValue );
35
33
}
36
-
37
34
/**
38
35
* Get the minLength attribute.
39
36
*
@@ -45,7 +42,6 @@ protected function getFieldMinLengthName($minLength)
45
42
{
46
43
return empty ($ minLength ) ? '' : sprintf (" 'minlength' => '%s', " , $ minLength );
47
44
}
48
-
49
45
/**
50
46
* Gets the maxLength attribute.
51
47
*
@@ -57,7 +53,6 @@ protected function getFieldMaxLengthName($maxLength)
57
53
{
58
54
return empty ($ maxLength ) ? '' : sprintf (" 'maxlength' => '%s', " , $ maxLength );
59
55
}
60
-
61
56
/**
62
57
* Gets the required attribute.
63
58
*
@@ -69,7 +64,6 @@ protected function getFieldRequired($required)
69
64
{
70
65
return $ required ? sprintf (" 'required' => %s, " , ($ required ? 'true ' : 'false ' )) : '' ;
71
66
}
72
-
73
67
/**
74
68
* Get the placeholder attribute.
75
69
*
@@ -81,7 +75,6 @@ protected function getFieldPlaceHolder(Label $placeholder = null)
81
75
{
82
76
return is_null ($ placeholder ) ? '' : sprintf (" 'placeholder' => %s, " , $ this ->getTitle ($ placeholder ));
83
77
}
84
-
85
78
/**
86
79
* Get the placeholder attribute for a menu.
87
80
*
@@ -94,7 +87,6 @@ protected function getFieldPlaceHolderForMenu(Label $placeholder = null, $name =
94
87
{
95
88
return $ this ->getFieldPlaceHolder ($ placeholder );
96
89
}
97
-
98
90
/**
99
91
* Get the multiple attribute.
100
92
*
@@ -106,7 +98,6 @@ protected function getFieldMultiple($isMulti)
106
98
{
107
99
return $ isMulti ? "'multiple' => 'multiple', " : '' ;
108
100
}
109
-
110
101
/**
111
102
* It gets converts an array to a stringbase array for the views.
112
103
*
@@ -119,12 +110,9 @@ protected function getFieldItems(Field $field)
119
110
if ($ field ->hasForeignRelation () && $ field ->isOnFormView ) {
120
111
return sprintf ('$%s ' , $ field ->getForeignRelation ()->getCollectionName ());
121
112
}
122
-
123
113
$ labels = $ field ->getOptionsByLang ();
124
-
125
114
return sprintf ('[%s] ' , implode (', ' . PHP_EOL , $ this ->getKeyValueStringsFromLabels ($ labels )));
126
115
}
127
-
128
116
/**
129
117
* Gets a plain title from a giving label.
130
118
*
@@ -137,7 +125,6 @@ protected function getPlainTitle(Label $label, $raw = false)
137
125
{
138
126
return sprintf (!$ raw ? "'%s' " : "%s " , $ label ->text );
139
127
}
140
-
141
128
/**
142
129
* Gets the fields value
143
130
*
@@ -151,13 +138,10 @@ protected function getFieldValue($value, $name)
151
138
{
152
139
if (!is_null ($ value )) {
153
140
$ modelVariable = $ this ->getSingularVariable ($ this ->modelName );
154
-
155
141
return sprintf ("(!isset( \$%s->%s) ? '%s' : null) " , $ modelVariable , $ name , $ value );
156
142
}
157
-
158
143
return 'null ' ;
159
144
}
160
-
161
145
/**
162
146
* Gets checked item attribute.
163
147
*
@@ -175,7 +159,6 @@ protected function getCheckedItem($value, $name, $defaultValue)
175
159
$ value
176
160
);
177
161
}
178
-
179
162
/**
180
163
* Gets selected value attribute.
181
164
*
@@ -187,7 +170,7 @@ protected function getCheckedItem($value, $name, $defaultValue)
187
170
*/
188
171
protected function getSelectedValue ($ name , $ valueAccessor , $ defaultValue )
189
172
{
190
- return sprintf (" (%s == %s ? true : null) " , $ this ->getRawOptionValue ($ name , $ defaultValue ), $ valueAccessor );
173
+ return sprintf (" (%s == %s ? true : null) " , $ this ->getRawOptionValue ($ name , ' null ' ), ' null ' );
191
174
}
192
175
193
176
/**
@@ -203,7 +186,6 @@ protected function getMultipleCheckedItem($value, $name, $defaultValue)
203
186
{
204
187
return sprintf (" (%s ? true : null) " , $ this ->getMultipleRawOptionValue ($ name , $ value , $ defaultValue ));
205
188
}
206
-
207
189
/**
208
190
* Gets a raw value for a giving field's name.
209
191
*
@@ -215,12 +197,10 @@ protected function getMultipleCheckedItem($value, $name, $defaultValue)
215
197
protected function getRawOptionValue ($ name , $ value )
216
198
{
217
199
$ modelVariable = $ this ->getSingularVariable ($ this ->modelName );
218
-
219
- $ valueString = is_null ($ value ) ? 'null ' : sprintf ("'%s' " , $ value );
220
-
221
- return sprintf ("old('%s', isset( \$%s->%s) ? \$%s->%s : %s) " , $ name , $ modelVariable , $ name , $ modelVariable , $ name , $ valueString );
200
+ $ valueString = is_null ($ value ) || $ value == 'null ' ? 'null ' : sprintf ("'%s' " , $ value );
201
+ $ accessor = $ this ->getDefaultValueAccessor ($ modelVariable , $ name , $ valueString );
202
+ return sprintf ("old('%s', %s) " , $ name , $ accessor );
222
203
}
223
-
224
204
/**
225
205
* Gets a raw value for a giving field's name.
226
206
*
@@ -234,21 +214,37 @@ protected function getMultipleRawOptionValue($name, $value, $defaultValue)
234
214
{
235
215
$ modelVariable = $ this ->getSingularVariable ($ this ->modelName );
236
216
$ valueString = 'null ' ;
237
-
238
217
if (!is_null ($ value )) {
239
218
$ valueString = starts_with ('$ ' , $ value ) ? sprintf ("%s " , $ value ) : sprintf ("'%s' " , $ value );
240
219
}
241
-
242
220
$ defaultValueString = '[] ' ;
243
-
244
221
if (!empty ($ defaultValue )) {
245
222
$ joinedValues = implode (', ' , Helpers::wrapItems ((array ) $ defaultValue ));
246
223
$ defaultValueString = sprintf ('[%s] ' , $ joinedValues );
247
224
}
248
-
249
- return sprintf ("in_array(%s, old('%s', isset( \$%s->%s) ? \$%s->%s : %s)) " , $ valueString , $ name , $ modelVariable , $ name , $ modelVariable , $ name , $ defaultValueString );
225
+ $ accessor = $ this ->getDefaultValueAccessor ($ modelVariable , $ name , $ defaultValueString );
226
+ return sprintf ("in_array(%s, old('%s', %s)) " , $ valueString , $ name , $ accessor );
227
+ }
228
+ /**
229
+ * Gets the best value accessor for the view
230
+ *
231
+ * @param string $modelVariable
232
+ * @param string $property
233
+ * @param string $value
234
+ *
235
+ * @return string
236
+ */
237
+ protected function getDefaultValueAccessor ($ modelVariable , $ property , $ value )
238
+ {
239
+ if (Helpers::isNewerThanOrEqualTo ('5.5 ' )) {
240
+ $ template = sprintf ('optional($%s)->%s ' , $ modelVariable , $ property );
241
+ if (is_null ($ value ) || in_array ($ value , ['null ' , '[] ' ])) {
242
+ return $ template ;
243
+ }
244
+ return $ template . ' ?: ' . $ value ;
245
+ }
246
+ return sprintf ("isset( \$%s->%s) ? \$%s->%s : %s " , $ modelVariable , $ property , $ modelVariable , $ property , $ value );
250
247
}
251
-
252
248
/**
253
249
* Gets selected value attribute.
254
250
*
@@ -262,7 +258,6 @@ protected function getMultipleSelectedValue($name, $valueAccessor, $defaultValue
262
258
{
263
259
return sprintf (" (%s ? true : null) " , $ name );
264
260
}
265
-
266
261
/**
267
262
* Gets the html steps attribute.
268
263
*
@@ -274,7 +269,6 @@ protected function getStepsValue($value)
274
269
{
275
270
return ($ value ) > 0 ? "'step' => \"any \", " : '' ;
276
271
}
277
-
278
272
/**
279
273
* Gets an instance of ViewLabelsGenerator
280
274
*
0 commit comments