This repository was archived by the owner on Mar 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormGenerator.php
More file actions
458 lines (407 loc) · 12.9 KB
/
FormGenerator.php
File metadata and controls
458 lines (407 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
<?php
/**
* Copyright (C) GrizzIT, Inc. All rights reserved.
* See LICENSE for license details.
*/
namespace Ulrack\Cli\Generator;
use GrizzIt\Validator\Common\ValidatorInterface;
use GrizzIt\Validator\Component\Chain\OrValidator;
use GrizzIt\Validator\Component\Chain\AndValidator;
use GrizzIt\Validator\Component\Logical\AlwaysValidator;
use GrizzIt\Validator\Component\Logical\EnumValidator;
use GrizzIt\Validator\Component\Iterable\MinItemsValidator;
use GrizzIt\Validator\Component\Textual\MinLengthValidator;
use Ulrack\Cli\Common\Element\ElementInterface;
use Ulrack\Cli\Common\Element\Form\FieldValidatorInterface;
use Ulrack\Cli\Component\Io\OptionProvider;
use Ulrack\Cli\Common\Element\FormInterface;
use Ulrack\Cli\Exception\NotInitializedException;
use Ulrack\Cli\Common\Factory\FormFactoryInterface;
use Ulrack\Cli\Component\Element\Form\FieldValidator;
use Ulrack\Cli\Common\Factory\ElementFactoryInterface;
use Ulrack\Cli\Common\Generator\FormGeneratorInterface;
class FormGenerator implements FormGeneratorInterface
{
/**
* Contains the theme factory.
*
* @var FormFactoryInterface
*/
private $formFactory;
/**
* Contains the element factory.
*
* @var ElementFactoryInterface
*/
private $elementFactory;
/**
* Contains the current theme.
*
* @var FormInterface
*/
private $form;
/**
* Constructor.
*
* @param FormFactoryInterface $formFactory
* @param ElementFactoryInterface $elementFactory
*/
public function __construct(
FormFactoryInterface $formFactory,
ElementFactoryInterface $elementFactory
) {
$this->formFactory = $formFactory;
$this->elementFactory = $elementFactory;
}
/**
* Creates a new theme.
*
* @param string $title
* @param string $description
*
* @return FormGeneratorInterface
*/
public function init(
string $title,
string $description = ''
): FormGeneratorInterface {
$title = [$this->elementFactory->createText($title, true, 'title')];
if (!empty($description)) {
$title[] = $this->elementFactory->createText(
$description,
true,
'text'
);
}
$this->form = $this->formFactory->createForm(...$title);
return $this;
}
/**
* Reinitializes a form.
*
* @param FormInterface $form
*
* @return FormGeneratorInterface
*/
public function reinit(FormInterface $form): FormGeneratorInterface
{
$this->form = $form;
return $this;
}
/**
* Adds an open field to the form.
*
* @param string $name
* @param bool $required
* @param string $errorMessage
* @param ValidatorInterface ...$additionalValidators
*
* @return FormGeneratorInterface
*
* @throws NotInitializedException When the form is not initialized.
*/
public function addOpenField(
string $name,
bool $required = true,
string $errorMessage = 'This is a required field.',
ValidatorInterface ...$additionalValidators
): FormGeneratorInterface {
$this->preAddCheck();
$title = sprintf(
'%s%s: ',
$this->convertKeyToLabel($name),
$required ? '*' : ''
);
$label = $this->elementFactory->createText($title, false, 'label');
$validator = $this->createValidator(
$required,
$errorMessage,
...$additionalValidators
);
$field = $this->formFactory->createField($label);
$this->form->addField($name, $field, $validator);
return $this;
}
/**
* Adds a hidden field to the form.
*
* @param string $name
* @param bool $required
* @param string $errorMessage
* @param ValidatorInterface ...$additionalValidators
*
* @return FormGeneratorInterface
*
* @throws NotInitializedException When the form is not initialized.
*/
public function addHiddenField(
string $name,
bool $required = true,
string $errorMessage = 'This is a required field.',
ValidatorInterface ...$additionalValidators
): FormGeneratorInterface {
$this->preAddCheck();
$title = sprintf(
'%s(hidden)%s: ',
$this->convertKeyToLabel($name),
$required ? '*' : ''
);
$label = $this->elementFactory->createText($title, false, 'label');
$validator = $this->createValidator(
$required,
$errorMessage,
...$additionalValidators
);
$field = $this->formFactory->createObscuredField($label);
$this->form->addField($name, $field, $validator);
return $this;
}
/**
* Adds an autocompleting (options) field to the form.
*
* @param string $name
* @param array $options
* @param boolean $required
* @param string $errorMessageRequired
* @param string $errorMessageEnum
* @param ValidatorInterface ...$additionalValidators
*
* @return FormGeneratorInterface
*
* @throws NotInitializedException When the form is not initialized.
*/
public function addAutocompletingField(
string $name,
array $options,
bool $required = true,
string $errorMessageRequired = 'This is a required field.',
string $errorMessageEnum = 'The value must be in the options list.',
ValidatorInterface ...$additionalValidators
): FormGeneratorInterface {
$this->preAddCheck();
$title = sprintf(
'%s%s: ',
$this->convertKeyToLabel($name),
$required ? '*' : ''
);
$label = $this->elementFactory->createChain(
$this->elementFactory->createList($options),
$this->elementFactory->createText($title, false, 'label')
);
$additionalValidators[] = new EnumValidator($options);
$validator = $this->createValidator(
$required,
($required ? $errorMessageRequired . "\n" : '') . $errorMessageEnum,
...$additionalValidators
);
$field = $this->formFactory->createAutocompletingField(
$label,
new OptionProvider($options)
);
$this->form->addField($name, $field, $validator);
return $this;
}
/**
* Adds an open array field to the form.
*
* @param string $name
* @param bool $required
* @param string $additionalMessage
* @param string $errorMessage
* @param ValidatorInterface ...$additionalValidators
*
* @return FormGeneratorInterface
*
* @throws NotInitializedException When the form is not initalized.
*/
public function addOpenArrayField(
string $name,
bool $required = true,
string $additionalMessage = 'Add another value?',
string $errorMessage = 'This is a required field.',
ValidatorInterface ...$additionalValidators
): FormGeneratorInterface {
$this->preAddCheck();
$title = sprintf(
'%s%s: ',
$this->convertKeyToLabel($name),
$required ? '*' : ''
);
$label = $this->elementFactory->createText($title, false, 'label');
$additionalLabel = $this->createAdditionalMessage($additionalMessage);
$validator = $this->createValidator(
$required,
$errorMessage,
...$additionalValidators
);
$field = $this->formFactory->createArrayField($label, $additionalLabel);
$this->form->addField($name, $field, $validator);
return $this;
}
/**
* Adds a hidden field to the form.
*
* @param string $name
* @param bool $required
* @param string $additionalMessage
* @param string $errorMessage
* @param ValidatorInterface ...$additionalValidators
*
* @return FormGeneratorInterface
*
* @throws NotInitializedException When the form is not initialized.
*/
public function addHiddenArrayField(
string $name,
bool $required = true,
string $additionalMessage = 'Add another value?',
string $errorMessage = 'This is a required field.',
ValidatorInterface ...$additionalValidators
): FormGeneratorInterface {
$this->preAddCheck();
$title = $this->convertKeyToLabel($name) . sprintf(
'(hidden)%s: ',
$required ? '*' : ''
);
$label = $this->elementFactory->createText($title, false, 'label');
$additionalLabel = $this->createAdditionalMessage($additionalMessage);
$validator = $this->createValidator(
$required,
$errorMessage,
...$additionalValidators
);
$field = $this->formFactory->createObscuredArrayField(
$label,
$additionalLabel
);
$this->form->addField($name, $field, $validator);
return $this;
}
/**
* Adds an autocompleting (options) field to the form.
*
* @param string $name
* @param array $options
* @param boolean $required
* @param string $additionalMessage
* @param string $errorMessageRequired
* @param string $errorMessageEnum
* @param ValidatorInterface ...$additionalValidators
*
* @return FormGeneratorInterface
*
* @throws NotInitializedException When the form is not initialized.
*/
public function addAutocompletingArrayField(
string $name,
array $options,
bool $required = true,
string $additionalMessage = 'Add another value?',
string $errorMessageRequired = 'This is a required field.',
string $errorMessageEnum = 'The value must be in the options list.',
ValidatorInterface ...$additionalValidators
): FormGeneratorInterface {
$this->preAddCheck();
$title = $this->convertKeyToLabel($name) . sprintf(
'%s: ',
$required ? '*' : ''
);
$label = $this->elementFactory->createChain(
$this->elementFactory->createList($options),
$this->elementFactory->createText($title, false, 'label')
);
$additionalLabel = $this->createAdditionalMessage($additionalMessage);
$additionalValidators[] = new EnumValidator($options);
$validator = $this->createValidator(
$required,
($required ? $errorMessageRequired . "\n" : '') . $errorMessageEnum,
...$additionalValidators
);
$field = $this->formFactory->createAutocompletingArrayField(
$label,
$additionalLabel,
new OptionProvider($options)
);
$this->form->addField($name, $field, $validator);
return $this;
}
/**
* Executes a check before elements can be added.
*
* @return void
*
* @throws NotInitializedException When the form is not initialized.
*/
private function preAddCheck(): void
{
if ($this->form === null) {
throw new NotInitializedException(self::class, 'init', 'form');
}
}
/**
* Creates the additional message label.
*
* @param string $additionalMessage
*
* @return ElementInterface
*/
private function createAdditionalMessage(string $additionalMessage): ElementInterface
{
return $this->elementFactory->createText(
sprintf(
'%s (y/n): ',
$additionalMessage
),
false,
'label'
);
}
/**
* Creates a validator for the field.
*
* @param boolean $required
* @param string $errorMessage
* @param ValidatorInterface ...$additionalValidators
*
* @return FieldValidatorInterface
*/
private function createValidator(
bool $required,
string $errorMessage,
ValidatorInterface ...$additionalValidators
): FieldValidatorInterface {
$validator = $required ? new OrValidator(
new MinLengthValidator(1),
new MinItemsValidator(1)
) : new AlwaysValidator(true);
return new FieldValidator(
new AndValidator($validator, ...$additionalValidators),
$this->elementFactory->createBlock($errorMessage, 'error-block')
);
}
/**
* Converts the key to a label.
*
* @param string $key
*
* @return string
*/
private function convertKeyToLabel(string $key): string
{
return ucwords(str_replace('_', ' ', $key));
}
/**
* Retrieves the current generating form.
*
* @return FormInterface
*
* @throws NotInitializedException When the form is not initialized.
*/
public function getForm(): FormInterface
{
$this->preAddCheck();
$form = $this->form;
$this->form = null;
return $form;
}
}