This repository was archived by the owner on Apr 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAbstractControlBlock.php
More file actions
533 lines (460 loc) · 14.8 KB
/
AbstractControlBlock.php
File metadata and controls
533 lines (460 loc) · 14.8 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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
<?php
namespace FewAgency\FluentForm;
use FewAgency\FluentForm\Support\FollowedByBlockContainersTrait;
use FewAgency\FluentForm\Support\FollowedByBlocksTrait;
use FewAgency\FluentForm\Support\FormElementContract;
use FewAgency\FluentForm\Support\FormElementTrait;
use FewAgency\FluentHtml\FluentHtmlElement;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Support\Collection;
abstract class AbstractControlBlock extends FluentHtmlElement implements FormElementContract
{
use FormElementTrait, FollowedByBlocksTrait, FollowedByBlockContainersTrait;
/**
* @var string representing name or key for the main input of this block
*/
private $input_name;
/**
* @var AbstractLabel
*/
private $label_element;
/**
* @var DescriptionElement
*/
private $description_element;
/**
* @var bool true if the description element of this block has been pulled out for display elsewehere
*/
private $is_description_element_pulled = false;
/**
* @var array of elements for alignment within the block, e.g. label holder, input holder, description holder
*/
private $alignment_elements = [];
/**
* @var Collection of error messages for this input block
*/
private $errors;
/**
* @var Collection of warning messages for this input block
*/
private $warnings;
/**
* @var bool|callable indicating if the block has success state
*/
private $has_success;
/**
* @var bool|callable indicating if the block's inputs are disabled
*/
private $is_disabled;
/**
* @var bool|callable indicating if the block's inputs are readonly
*/
private $is_readonly;
/**
* @var bool|callable indicating if the block's inputs are required
*/
private $is_required;
/**
* @var string css class name to put on all form blocks
*/
private $form_block_class = 'form-block';
/**
* @var string css class name for labels in form blocks
*/
private $form_block_label_class = 'form-block__label';
/**
* @var string css class name for labels in form blocks
*/
private $form_block_description_class = 'form-block__description';
/**
* @var string css class name for message lists in form blocks
*/
private $form_block_messages_class = 'form-block__messages';
/**
* @var string css class name for error message lists in form blocks
*/
private $form_block_error_messages_class = 'form-block__messages--error';
/**
* @var string css class name for warning message lists in form blocks
*/
private $form_block_warning_messages_class = 'form-block__messages--warning';
/**
* @var string css class name to put on aligned form blocks
*/
private $form_block_aligned_class = 'form-block--aligned';
/**
* @var string css class name to put on disabled form blocks
*/
private $form_block_disabled_class = 'form-block--disabled';
/**
* @var string css class name to put on required form blocks
*/
private $form_block_required_class = 'form-block--required';
/**
* @var string css class name to put on form blocks with error
*/
private $form_block_error_class = 'form-block--error';
/**
* @var string css class name to put on form blocks with warning
*/
private $form_block_warning_class = 'form-block--warning';
/**
* @var string css class name to put on form blocks with success
*/
private $form_block_success_class = 'form-block--success';
public function __construct()
{
parent::__construct();
$this->withHtmlElementName(function () {
return $this->isInline() ? 'span' : 'div';
});
$this->errors = new Collection();
$this->warnings = new Collection();
$this->alignment_elements = [
//label holder
$this->createFluentHtmlElement(function () {
return $this->isAligned() || $this->isInline() ? 'span' : 'div';
})->withContent(function () {
return $this->getLabelElement();
})->withClass(function () {
return $this->getAlignmentClasses(1);
})->onlyDisplayedIfHasContent(),
//input holder
$this->createFluentHtmlElement(function () {
return $this->isAligned() || $this->isInline() ? 'span' : 'div';
})->withClass(function () {
return $this->getAlignmentClasses(2);
}),
//description
function () {
return $this->isDescriptionPulled() ? null : $this->getDescriptionElement();
},
];
$this->withContent($this->alignment_elements);
$this->withClass([
$this->form_block_class,
$this->form_block_aligned_class => function () {
return $this->isAligned();
},
$this->form_block_disabled_class => function () {
return $this->isDisabled();
},
$this->form_block_required_class => function () {
return $this->isRequired();
},
function () {
return $this->getStateClass();
}
]);
$this->withAttribute('disabled', function () {
return $this->isDisabled() and $this->getHtmlElementName() == 'fieldset';
});
}
/**
* Add label content.
* @param string|Htmlable|callable|array|Arrayable $html_contents,...
* @return $this
*/
public function withLabel($html_contents)
{
$this->getLabelElement()->withContent(func_get_args());
return $this;
}
/**
* Get the label element of the block.
* @return AbstractLabel
*/
public function getLabelElement()
{
return $this->label_element;
}
/**
* Set the label element of the block.
* @param AbstractLabel $label_element
* @return $this
*/
protected function withLabelElement(AbstractLabel $label_element)
{
$label_element->withClass($this->form_block_label_class);
$this->label_element = $label_element;
return $this;
}
/**
* Add description content.
* @param string|Htmlable|callable|array|Arrayable $html_contents,...
* @return $this
*/
public function withDescription($html_contents)
{
$this->getDescriptionElement()->withContent(func_get_args());
return $this;
}
/**
* Get the description element of the block.
* @return DescriptionElement
*/
public function getDescriptionElement()
{
if (!$this->description_element) {
$this->description_element = $this->createInstanceOf('DescriptionElement')
->withClass($this->form_block_description_class)
->withClass(function () {
return $this->getAlignmentClasses(3);
})
->withContent(function () {
return $this->generateErrorListElement();
})
->withContent(function () {
return $this->generateWarningListElement();
});
}
return $this->description_element;
}
/**
* Pull the description out of this form block to place it somewhere else in the tree.
* @return DescriptionElement
*/
public function pullDescriptionElement()
{
$this->is_description_element_pulled = true;
return $this->getDescriptionElement();
}
/**
* Check if the description element has been placed outside this form-block
* @return bool
*/
public function isDescriptionPulled()
{
return $this->is_description_element_pulled;
}
/**
* Make the input(s) in the block disabled.
* @param bool|callable $disabled
* @return $this
*/
public function disabled($disabled = true)
{
$this->is_disabled = $disabled;
return $this;
}
/**
* Check if the block is disabled
* @return bool true if the block is considered disabled
*/
public function isDisabled()
{
$disabled = $this->evaluate($this->is_disabled);
if (!isset($disabled)) {
$disabled = $this->isDisabledFromAncestor($this->getInputName());
}
return (bool)$disabled;
}
/**
* Make the input(s) in the block readonly
* @param bool|callable $readonly
* @return $this
*/
public function readonly($readonly = true)
{
$this->is_readonly = $readonly;
return $this;
}
/**
* Check if the block is readonly.
* @return bool true if the block's items is considered readonly
*/
public function isReadonly()
{
$readonly = $this->evaluate($this->is_readonly);
if (!isset($readonly)) {
$readonly = $this->isReadonlyFromAncestor($this->getInputName());
}
return (bool)$readonly;
}
/**
* Make the input in the block required.
* @param bool|callable $required
* @return $this
*/
public function required($required = true)
{
$this->is_required = $required;
return $this;
}
/**
* Check if the block is required.
* @return bool true if the block is considered required
*/
public function isRequired()
{
$required = $this->evaluate($this->is_required);
if (!isset($required)) {
$required = $this->isRequiredFromAncestor($this->getInputName());
}
return (bool)$required;
}
/**
* Set name of main input for this block.
* @param string $name
* @return $this
*/
public function withInputName($name)
{
$this->input_name = $name;
return $this;
}
/**
* Get the name of the main input of this block.
* @return string
*/
public function getInputName()
{
return $this->input_name;
}
/**
* Add error message(s) to this block.
* @param string|array|Arrayable $messages
* @return $this
*/
public function withError($messages)
{
$this->errors = $this->errors->merge($messages);
return $this;
}
/**
* Get all combined error messages for this block.
* @return array
*/
protected function getErrorMessages()
{
return $this->errors->merge($this->getErrorsFromAncestor($this->getInputName()))
->transform(function ($message) {
return trim($message);
})->filter(function ($message) {
//Filter out empty strings and booleans
return isset($message) and !is_bool($message) and '' !== $message;
})->unique()->toArray();
}
/**
* Find out if this form block has any errors.
* @return bool
*/
public function hasError()
{
return (bool)count($this->getErrorMessages());
}
/**
* Generate a html list of error messages for this block.
* @return FluentHtmlElement
*/
protected function generateErrorListElement()
{
return $this->createFluentHtmlElement('ul')->onlyDisplayedIfHasContent()
->withClass($this->form_block_messages_class)
->withClass($this->form_block_error_messages_class)
->withContentWrappedIn($this->getErrorMessages(), 'li');
}
/**
* Add warning message(s) to this block.
* @param string|array|Arrayable $messages
* @return $this
*/
public function withWarning($messages)
{
$this->warnings = $this->warnings->merge($messages);
return $this;
}
/**
* Get all combined warning messages for this block.
* @return array
*/
protected function getWarningMessages()
{
return $this->warnings->merge($this->getWarningsFromAncestor($this->getInputName()))
->transform(function ($message) {
return trim($message);
})->filter(function ($message) {
//Filter out empty strings and booleans
return isset($message) and !is_bool($message) and '' !== $message;
})->unique()->toArray();
}
/**
* Find out if this form block has any warnings.
* @return bool
*/
public function hasWarning()
{
return (bool)count($this->getWarningMessages());
}
/**
* Generate a html list of warning messages for this block.
* @return FluentHtmlElement
*/
protected function generateWarningListElement()
{
return $this->createFluentHtmlElement('ul')->onlyDisplayedIfHasContent()
->withClass($this->form_block_messages_class)
->withClass($this->form_block_warning_messages_class)
->withContentWrappedIn($this->getWarningMessages(), 'li');
}
/**
* Set success state on the input block.
* @param bool|callable $has_success
* @return $this
*/
public function withSuccess($has_success = true)
{
$this->has_success = $has_success;
return $this;
}
/**
* Find out if this form block is in the success state.
* @return bool
*/
public function hasSuccess()
{
$success = $this->evaluate($this->has_success);
if (!isset($success)) {
$success = $this->hasSuccessFromAncestor($this->getInputName());
}
return (bool)$success;
}
/**
* Get the holder/wrapper elements for parts of this form block.
* @param $number 1: label, 2: input, or 3: description
* @return FluentHtmlElement|null
*/
protected function getAlignmentElement($number)
{
$index = $number - 1;
return isset($this->alignment_elements[$index]) ? $this->alignment_elements[$index] : null;
}
/**
* Get the css class representing the state of this block.
* @return null|string
*/
protected function getStateClass()
{
if ($this->hasError()) {
return $this->form_block_error_class;
} elseif ($this->hasWarning()) {
return $this->form_block_warning_class;
} elseif ($this->hasSuccess()) {
return $this->form_block_success_class;
} else {
return null;
}
}
/**
* Get the alignment classes for an alignment element.
* @param $number 1: label, 2: input, or 3: description
* @return array of class names
*/
public function getAlignmentClasses($number)
{
$previous_alignment_element = $this->getAlignmentElement($number - 1);
$with_offset = $previous_alignment_element instanceof FluentHtmlElement ? !$previous_alignment_element->willRenderInHtml() : false;
return $this->isAligned() ? $this->getControlBlockContainer()->getAlignmentClasses($number, $with_offset) : [];
}
}