This repository has been 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
/
AbstractControlBlockContainer.php
499 lines (436 loc) · 14.2 KB
/
AbstractControlBlockContainer.php
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
<?php
namespace FewAgency\FluentForm;
use FewAgency\FluentForm\Support\ContainingBlockContainersTrait;
use FewAgency\FluentForm\Support\ContainingBlocksTrait;
use FewAgency\FluentForm\Support\MapCollection;
use FewAgency\FluentHtml\FluentHtmlElement;
use FewAgency\FluentForm\Support\FormElementContract;
use FewAgency\FluentForm\Support\FormElementTrait;
use Illuminate\Contracts\Support\MessageProvider;
use Illuminate\Support\Collection;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\MessageBag;
use Illuminate\Support\ViewErrorBag;
abstract class AbstractControlBlockContainer extends FluentHtmlElement implements FormElementContract
{
use FormElementTrait {
FormElementTrait::isInline as isAncestorInline;
FormElementTrait::isAligned as isAncestorAligned;
}
use ContainingBlocksTrait, ContainingBlockContainersTrait;
/**
* @var string css classname to put on block containers
*/
protected $form_block_container_class = 'form-block-container';
/**
* @var string css classname to put on aligned block containers
*/
protected $form_block_container_aligned_class = "form-block-container--aligned";
/**
* @var string css classname to put on inline block containers
*/
protected $form_block_container_inline_class = "form-block-container--inline";
protected $is_aligned;
protected $alignment_classes;
protected $alignment_classes_default = [
1 => 'form-block__align1',
2 => 'form-block__align2',
3 => 'form-block__align3',
];
protected $alignment_offset_classes;
protected $alignment_offset_classes_default = [2 => 'form-block-container__align2--offset', 3 => ''];
/**
* The values and selected options for inputs in this level of the form.
* @var MapCollection
*/
private $value_maps;
/**
* The labels for controls in this level of the form.
* @var MapCollection
*/
private $label_maps;
/**
* Error messages for this level of the form.
* @var MessageBag
*/
private $error_messages;
/**
* Warning messages for this level of the form.
* @var MessageBag
*/
private $warning_messages;
/**
* Success controls in this level of the form.
* @var MapCollection
*/
private $success_maps;
/**
* Disabled controls in this level of the form.
* @var MapCollection
*/
private $disabled_maps;
/**
* Readonly controls in this level of the form.
* @var MapCollection
*/
private $readonly_maps;
/**
* Required controls in this level of the form.
* @var MapCollection
*/
private $required_maps;
/**
* AbstractControlBlock elements in this container.
* @var Collection
*/
private $form_block_elements;
/**
* AbstractControlBlockContainer element in this container.
* @var Collection
*/
private $form_block_container_elements;
/**
* @var bool|callable|null indicating if the block container's content is inline
*/
private $is_inline;
public function __construct()
{
parent::__construct();
$this->value_maps = new MapCollection();
$this->label_maps = new MapCollection();
$this->form_block_elements = new Collection();
$this->form_block_container_elements = new Collection();
$this->error_messages = new MessageBag();
$this->warning_messages = new MessageBag();
$this->success_maps = new MapCollection();
$this->disabled_maps = new MapCollection();
$this->readonly_maps = new MapCollection();
$this->required_maps = new MapCollection();
$this->withClass([
$this->form_block_container_class,
$this->form_block_container_aligned_class => function () {
return $this->isAligned();
},
$this->form_block_container_inline_class => function () {
return $this->isInline();
}
]);
$this->withContent(function () {
//This prints blocks' description elements at top of the container under certain conditions
if ($this->isInline()) {
$container = $this->getControlBlockContainer();
if (empty($container) or !$container->isInline()) {
return $this->pullSubBlocksDescriptionElements();
}
}
return null;
});
}
/**
* Add a hidden input to the form
* @param string $name
* @param mixed $value
* @return $this
*/
public function withHiddenInput($name, $value = null)
{
$this->withContent($this->createInstanceOf('HiddenInputElement', [$name, $value]));
return $this;
}
/**
* Set a value container for populating inputs.
* @param array|object|Arrayable $map,... key-value implementations
* @return $this
*/
public function withValues($map)
{
$this->value_maps->prependMaps(func_get_args());
return $this;
}
/**
* Get an input's value or selected option.
* @param string $key in dot-notation
* @return mixed|null
*/
public function getValue($key)
{
return $this->evaluate($this->value_maps)->firstValue($key, function () use ($key) {
return $this->getValueFromAncestor($key);
});
}
/**
* Add maps of labels, keyed by control name.
*
* @param array|Arrayable $map,...
* @return $this
*/
public function withLabels($map)
{
$this->label_maps->prependMaps(func_get_args());
return $this;
}
/**
* Get a label from a key.
* @param string $key in dot-notation
* @return mixed|null
*/
public function getLabel($key)
{
return $this->evaluate($this->label_maps)->firstValue($key, function () use ($key) {
return $this->getLabelFromAncestor($key);
});
}
/**
* Merge a new array of messages into the error messages.
*
* @param MessageProvider|array $messages keyed by control name
* @return $this
*/
public function withErrors($messages)
{
if ($messages instanceof ViewErrorBag) {
$messages = $messages->getMessageBag();
}
$this->error_messages->merge($messages);
return $this;
}
/**
* Get the error messages for a form control.
*
* @param string $key
* @return array of message strings
*/
public function getErrors($key)
{
return array_merge($this->error_messages->get($key), $this->getErrorsFromAncestor($key));
}
/**
* Merge a new array of messages into the warning messages.
*
* @param MessageProvider|array $messages keyed by control name
* @return $this
*/
public function withWarnings($messages)
{
if ($messages instanceof ViewErrorBag) {
$messages = $messages->getMessageBag();
}
$this->warning_messages->merge($messages);
return $this;
}
/**
* Get the warning messages for a form control.
*
* @param string $key
* @return array of message strings
*/
public function getWarnings($key)
{
return array_merge($this->warning_messages->get($key), $this->getWarningsFromAncestor($key));
}
/**
* Add control names that have success status.
* @param string|callable|array|Arrayable $map,... key-boolean map(s) or string(s) of success form control names
* @return $this
*/
public function withSuccess($map)
{
$this->success_maps->prependMaps(func_get_args());
return $this;
}
/**
* Find out if a control has success state.
* @param string $key
* @return bool
*/
public function hasSuccess($key)
{
return $this->evaluate($this->success_maps)->firstBoolean($key, function () use ($key) {
return $this->hasSuccessFromAncestor($key);
});
}
/**
* Add control names that are disabled.
* @param string|callable|array|Arrayable $map,... key-boolean map(s) or string(s) of disabled form control names
* @return $this
*/
public function withDisabled($map)
{
$this->disabled_maps->prependMaps(func_get_args());
return $this;
}
/**
* Find out if a control is disabled.
* @param string $key
* @return bool
*/
public function isDisabled($key)
{
return $this->evaluate($this->disabled_maps)->firstBoolean($key, function () use ($key) {
return $this->isDisabledFromAncestor($key);
});
}
/**
* Add control names that are readonly.
* @param string|callable|array|Arrayable $map,... key-boolean map(s) or string(s) of readonly form control names
* @return $this
*/
public function withReadonly($map)
{
$this->readonly_maps->prependMaps(func_get_args());
return $this;
}
/**
* Find out if a control is readonly.
* @param string $key
* @return bool
*/
public function isReadonly($key)
{
return $this->evaluate($this->readonly_maps)->firstBoolean($key, function () use ($key) {
return $this->isReadonlyFromAncestor($key);
});
}
/**
* Add control names that are required.
* @param string|callable|array|Arrayable $map,... key-boolean map(s) or string(s) of required form control names
* @return $this
*/
public function withRequired($map)
{
$this->required_maps->prependMaps(func_get_args());
return $this;
}
/**
* Find out if a control is required.
* @param string $key
* @return bool
*/
public function isRequired($key)
{
return $this->evaluate($this->required_maps)->firstBoolean($key, function () use ($key) {
return $this->isRequiredFromAncestor($key);
});
}
/**
* Set custom classes for horizontally aligned inputs in this container.
* @param string|array $classes1
* @param string|array $classes2
* @param string|array $classes3
* @param string|array $offset_classes2
* @param string|array|null $offset_classes3
*/
function withAlignmentClasses($classes1, $classes2, $classes3, $offset_classes2, $offset_classes3 = null)
{
$this->alignment_classes = [1 => $classes1, 2 => $classes2, 3 => $classes3];
$this->alignment_offset_classes = [2 => $offset_classes2, 3 => $offset_classes3];
}
/**
* Get the classes for an alignment element.
* @param $number 1: label, 2: input, or 3: description
* @param bool $with_offset Switch to include offset classes (if preceding element was left blank)
* @return array of class names
*/
public function getAlignmentClasses($number, $with_offset = false)
{
if (!empty($this->alignment_classes)) {
$alignment_classes = $this->alignment_classes;
$alignment_offset_classes = $this->alignment_offset_classes;
} elseif ($ancestor = $this->getControlBlockContainer()) {
return $ancestor->getAlignmentClasses($number, $with_offset);
} else {
$alignment_classes = $this->alignment_classes_default;
$alignment_offset_classes = $this->alignment_offset_classes_default;
}
$classes = (array)$alignment_classes[$number];
if ($with_offset) {
$classes = array_merge($classes, (array)$alignment_offset_classes[$number]);
}
return $classes;
}
/**
* Make control blocks in this container display horizontally aligned.
* @param bool|callable $align
* @return $this
*/
public function aligned($align = true)
{
$this->is_aligned = $align;
return $this;
}
/**
* Check if the control blocks in this container are set to display horizontally aligned.
* @return bool
*/
public function isAligned()
{
$aligned = $this->evaluate($this->is_aligned);
if (!isset($aligned)) {
$aligned = $this->isAncestorAligned();
}
return (bool)$aligned;
}
/**
* Make contents in this form block container display inline.
* @param bool|callable $inline
* @return $this
*/
public function inline($inline = true)
{
$this->is_inline = $inline;
return $this;
}
/**
* Check if the contents of this container are set to display inline.
* @return bool
*/
public function isInline()
{
$inline = $this->evaluate($this->is_inline);
if (!isset($inline)) {
$inline = $this->isAncestorInline();
}
return (bool)$inline;
}
/**
* Pull all contained form-blocks' descriptions for display outside their blocks.
* @return Collection
*/
protected function pullSubBlocksDescriptionElements()
{
return $this->form_block_elements->map(function (AbstractControlBlock $block) {
return $block->pullDescriptionElement();
})->merge($this->form_block_container_elements->map(function (AbstractControlBlockContainer $container) {
return $container->pullSubBlocksDescriptionElements();
}));
}
/**
* Generate a new control-block.
* @param string $type
* @param array $parameters
* @return AbstractControlBlock
*/
public function createControlBlock($type, $parameters = [])
{
try {
$block = $this->createInstanceOf($type . 'Block', $parameters);
} catch (\Exception $e) {
$block = $this->createInstanceOf('InputBlock', array_merge($parameters, [$type]));
}
return $block;
}
/**
* Overridden to make sure any inserted control blocks and sub-containers are registered with this container
* @inheritdoc
*/
protected function prepareContentsForInsertion($html_contents)
{
return parent::prepareContentsForInsertion(func_get_args())->each(function ($item) {
if ($item instanceof AbstractControlBlock) {
$this->form_block_elements->push($item);
} elseif ($item instanceof AbstractControlBlockContainer) {
$this->form_block_container_elements->push($item);
}
});
}
}