Passed
Pull Request — master (#160)
by Wilmer
02:47
created

FieldAttributes::readonly()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Widget\Attribute;
6
7
abstract class FieldAttributes extends WidgetAttributes
8
{
9
    private bool $ariaDescribedBy = false;
10
    /** @psalm-var array[][] */
11
    private array $buttonsIndividualAttributes = [];
12
    private bool $container = true;
13
    private array $containerAttributes = [];
14
    private string $containerClass = '';
15
    private array $containerIndividualClass = [];
16
    private ?string $error = '';
17
    private array $errorAttributes = [];
18
    private string $errorClass = '';
19
    /** @psalm-var string[] */
20
    private array $errorIndividualClass = [];
21
    private array $errorMessageCallback = [];
22
    private string $errorTag = 'div';
23
    private ?string $hint = '';
24
    private array $hintAttributes = [];
25
    private string $hintClass = '';
26
    /** @psalm-var string[] */
27
    protected array $hintIndividualClass = [];
28
    private string $hintTag = 'div';
29
    protected string $id = '';
30
    protected string $inputClass = '';
31
    protected array $inputsClass = [];
32
    private ?string $label = '';
33
    private array $labelAttributes = [];
34
    private string $labelClass = '';
35
    /** @psalm-var string[] */
36
    private array $labelIndividualClass = [];
37
    protected string $invalidClass = '';
38
    /** @psalm-var array<string, string> */
39
    protected array $invalidsClass = [];
40
    protected string $validClass = '';
41
    /** @psalm-var array<string, string> */
42
    protected array $validsClass = [];
43
    protected string $template = "{label}\n{input}\n{hint}\n{error}";
44
    /** @psalm-var array<string, string> */
45
    protected array $templates = [];
46
    protected array $parts = [];
47
    protected string $type = '';
48
    protected ?string $placeholder = null;
49
50
    /**
51
     * Set invalid class each for field type.
52
     *
53
     * @param array $invalidClass the input class to be used to layout the field.
54
     *
55
     * ```php
56
     * [Field::TYPE_TEXT => 'test-class-1', Field::TYPE_SUBMIT_BUTTON => 'test-class-2']
57
     *
58
     * @return static
59
     *
60
     * @psalm-param array<string, string> $invalidClass
61
     */
62
    public function addInvalidClass(array $invalidClass): self
63
    {
64
        $new = clone $this;
65
        $new->invalidsClass = $invalidClass;
66
        return $new;
67
    }
68
69
    /**
70
     * Set layout template for render a field with label, input, hint and error.
71
     *
72
     * @param array $template the template to be used to layout the field.
73
     *
74
     * ```php
75
     * [Field::TYPE_TEXT => '{input}', Field::TYPE_SUBMIT_BUTTON => '<div>{input}</div>']
76
     *
77
     * @return static
78
     *
79
     * @psalm-param array<string, string> $template
80
     */
81
    public function addTemplate(array $template): self
82
    {
83
        $new = clone $this;
84
        $new->templates = $template;
85
        return $new;
86
    }
87
88
    /**
89
     * Set invalid class each for field type.
90
     *
91
     * @param array $validsClass the input class to be used to layout the field.
92
     *
93
     * ```php
94
     * [Field::TYPE_TEXT => 'test-class-1', Field::TYPE_SUBMIT_BUTTON => 'test-class-2']
95
     *
96
     * @return static
97
     *
98
     * @psalm-param array<string, string> $validsClass
99
     */
100
    public function addValidClass(array $validsClass): self
101
    {
102
        $new = clone $this;
103
        $new->validsClass = $validsClass;
104
        return $new;
105
    }
106
107
    /**
108
     * Set aria-describedby attribute.
109
     *
110
     * @return static
111
     *
112
     * @link https://www.w3.org/TR/WCAG20-TECHS/ARIA1.html
113
     */
114
    public function ariaDescribedBy(): self
115
    {
116
        $new = clone $this;
117
        $new->ariaDescribedBy = true;
118
        return $new;
119
    }
120
121
    /**
122
     * Set individual attributes for the buttons widgets.
123
     *
124
     * @param array $values Attribute values indexed by attribute names.
125
     * ```php
126
     * [0 => ['value' => 'Submit'], 1 => ['value' => 'Reseteable']]
127
     * ```
128
     *
129
     * @return static
130
     *
131
     * @psalm-param array[][] $values
132
     */
133 1
    public function buttonsIndividualAttributes(array $values): self
134
    {
135 1
        $new = clone $this;
136 1
        $new->buttonsIndividualAttributes = $values;
137 1
        return $new;
138
    }
139
140
    /**
141
     * Set container attributes.
142
     *
143
     * @param array $values Attribute values indexed by attribute names.
144
     *
145
     * ```php
146
     * ['class' => 'test-class']
147
     * ```
148
     *
149
     * @return static
150
     *
151
     * @psalm-param array<string, string> $values
152
     */
153 1
    public function containerAttributes(array $values): self
154
    {
155 1
        $new = clone $this;
156 1
        $new->containerAttributes = array_merge($new->containerAttributes, $values);
157 1
        return $new;
158
    }
159
160
    /**
161
     * Set container css class.
162
     *
163
     * @return static
164
     */
165
    public function containerClass(string $value): self
166
    {
167
        $new = clone $this;
168
        $new->containerClass = $value;
169
        return $new;
170
    }
171
172
    /**
173
     * Set the ID of the container field.
174
     *
175
     * @param string|null $id
176
     *
177
     * @return static
178
     */
179 1
    public function containerId(?string $id): self
180
    {
181 1
        $new = clone $this;
182 1
        $new->containerAttributes['id'] = $id;
183 1
        return $new;
184
    }
185
186
    /**
187
     * Set the name of the container field.
188
     *
189
     * @param string|null $id
190
     *
191
     * @return static
192
     */
193 1
    public function containerName(?string $id): self
194
    {
195 1
        $new = clone $this;
196 1
        $new->containerAttributes['name'] = $id;
197 1
        return $new;
198
    }
199
200
    /**
201
     * Set error message for the field.
202
     *
203
     * @param string|null $value the error message.
204
     *
205
     * @return static The field widget instance.
206
     */
207 1
    public function error(?string $value): self
208
    {
209 1
        $new = clone $this;
210 1
        $new->error = $value;
211 1
        return $new;
212
    }
213
214
    /**
215
     * Set error attributes.
216
     *
217
     * @param array $values Attribute values indexed by attribute names.
218
     *
219
     * ```php
220
     * ['class' => 'test-class']
221
     * ```
222
     *
223
     * @return static The field widget instance.
224
     */
225
    public function errorAttributes(array $values): self
226
    {
227
        $new = clone $this;
228
        $new->errorAttributes = $values;
229
        return $new;
230
    }
231
232
    /**
233
     * Set error css class.
234
     *
235
     * @return static
236
     */
237 4
    public function errorClass(string $value): self
238
    {
239 4
        $new = clone $this;
240 4
        $new->errorClass = $value;
241 4
        return $new;
242
    }
243
244
    /**
245
     * Set error class used for an invalid field.
246
     *
247
     * @param array $errorClass The error class to apply to an invalid field.
248
     *
249
     * ```php
250
     * [Field::TYPE_TEXT => 'test-class-1', Field::TYPE_SUBMIT_BUTTON => 'test-class-2']
251
     * ```
252
     *
253
     * @return static
254
     *
255
     * @psalm-param array<string, string> $errorClass
256
     */
257
    public function errorIndividualClass(array $errorClass): self
258
    {
259
        $new = clone $this;
260
        $new->errorIndividualClass = $errorClass;
261
        return $new;
262
    }
263
264
    /**
265
     * Callback that will be called to obtain an error message.
266
     *
267
     * The signature of the callback must be:
268
     *
269
     * ```php
270
     * [$FormModel, function()]
271
     * ```
272
     *
273
     * @param array $value
274
     *
275
     * @return static
276
     */
277
    public function errorMessageCallback(array $value): self
278
    {
279
        $new = clone $this;
280
        $new->errorMessageCallback = $value;
281
        return $new;
282
    }
283
284
    /**
285
     * The tag name of the container element.
286
     *
287
     * Empty to render error messages without container {@see Html::tag()}.
288
     *
289
     * @param string $value
290
     *
291
     * @return static
292
     */
293
    public function errorTag(string $value): self
294
    {
295
        $new = clone $this;
296
        $new->errorTag = $value;
297
        return $new;
298
    }
299
300 353
    public function getAriaDescribedBy(): bool
301
    {
302 353
        return $this->ariaDescribedBy;
303
    }
304
305 21
    public function getButtonsIndividualAttributes(string $index): ?array
306
    {
307 21
        return $this->buttonsIndividualAttributes[$index] ?? null;
308
    }
309
310 368
    public function getContainer(): bool
311
    {
312 368
        return $this->container;
313
    }
314
315 386
    public function getContainerAttributes(): array
316
    {
317 386
        return $this->containerAttributes;
318
    }
319
320 386
    public function getContainerClass(): string
321
    {
322 386
        return $this->containerClass;
323
    }
324
325 334
    public function getError(): ?string
326
    {
327 334
        return $this->error;
328
    }
329
330 333
    public function getErrorAttributes(): array
331
    {
332 333
        return $this->errorAttributes;
333
    }
334
335 333
    public function getErrorClass(): string
336
    {
337 333
        return $this->errorClass;
338
    }
339
340 333
    public function getErrorIndividualClass(string $type): ?string
341
    {
342 333
        return $this->errorIndividualClass[$type] ?? null;
343
    }
344
345 333
    public function getErrorMessageCallback(): array
346
    {
347 333
        return $this->errorMessageCallback;
348
    }
349
350 333
    public function getErrorTag(): string
351
    {
352 333
        return $this->errorTag;
353
    }
354
355 334
    public function getHint(): ?string
356
    {
357 334
        return $this->hint;
358
    }
359
360 334
    public function getHintAttributes(): array
361
    {
362 334
        return $this->hintAttributes;
363
    }
364
365 334
    public function getHintClass(): string
366
    {
367 334
        return $this->hintClass;
368
    }
369
370 334
    public function getHintIndividualClass(string $type): ?string
371
    {
372 334
        return $this->hintIndividualClass[$type] ?? null;
373
    }
374
375 334
    public function getHintTag(): string
376
    {
377 334
        return $this->hintTag;
378
    }
379
380 303
    public function getLabel(): ?string
381
    {
382 303
        return $this->label;
383
    }
384
385 303
    public function getLabelAttributes(): array
386
    {
387 303
        return $this->labelAttributes;
388
    }
389
390 303
    public function getLabelClass(): string
391
    {
392 303
        return $this->labelClass;
393
    }
394
395 303
    public function getLabelIndividualClass(string $type): ?string
396
    {
397 303
        return $this->labelIndividualClass[$type] ?? null;
398
    }
399
400
    /**
401
     * Set hint message for the field.
402
     *
403
     * @return static
404
     */
405 5
    public function hint(?string $value): self
406
    {
407 5
        $new = clone $this;
408 5
        $new->hint = $value;
409 5
        return $new;
410
    }
411
412
    /**
413
     * Set hint attributes.
414
     *
415
     * @param array $values Attribute values indexed by attribute names.
416
     *
417
     * ```php
418
     * ['class' => 'test-class']
419
     * ```
420
     *
421
     * @return static The field widget instance.
422
     */
423 1
    public function hintAttributes(array $values): self
424
    {
425 1
        $new = clone $this;
426 1
        $new->hintAttributes = $values;
427 1
        return $new;
428
    }
429
430
    /**
431
     * Set hint css class.
432
     *
433
     * @return static
434
     */
435 5
    public function hintClass(string $value): self
436
    {
437 5
        $new = clone $this;
438 5
        $new->hintClass = $value;
439 5
        return $new;
440
    }
441
442
    /**
443
     * Set hint tag name.
444
     *
445
     * @return static
446
     */
447 1
    public function hintTag(string $value): self
448
    {
449 1
        $new = clone $this;
450 1
        $new->hintTag = $value;
451 1
        return $new;
452
    }
453
454
    /**
455
     * Set hint class for a field.
456
     *
457
     * @param array $hintClass The hint class to be applied to a field.
458
     *
459
     * ```php
460
     * [Field::TYPE_TEXT => 'test-class-1', Field::TYPE_SUBMIT_BUTTON => 'test-class-2']
461
     *
462
     * @return static
463
     *
464
     * @psalm-param array<string, string> $hintClass
465
     */
466
    public function hintIndividualClass(array $hintClass): self
467
    {
468
        $new = clone $this;
469
        $new->hintIndividualClass = $hintClass;
470
        return $new;
471
    }
472
473
    /**
474
     * Set input css class.
475
     *
476
     * @return static
477
     */
478
    public function inputClass(string $value): self
479
    {
480
        $new = clone $this;
481
        $new->inputClass = $value;
482
        return $new;
483
    }
484
485
    /**
486
     * Set invalid css class.
487
     *
488
     * @return static
489
     */
490 4
    public function invalidClass(string $value): self
491
    {
492 4
        $new = clone $this;
493 4
        $new->invalidClass = $value;
494 4
        return $new;
495
    }
496
497
    /**
498
     * Set label message for the field.
499
     *
500
     * @return static
501
     */
502 7
    public function label(?string $value): self
503
    {
504 7
        $new = clone $this;
505 7
        $new->label = $value;
506 7
        return $new;
507
    }
508
509
    /**
510
     * Set label attributes.
511
     *
512
     * @param array $values Attribute values indexed by attribute names.
513
     *
514
     * ```php
515
     * ['class' => 'test-class']
516
     * ```
517
     *
518
     * @return static The field widget instance.
519
     */
520
    public function labelAttributes(array $values): self
521
    {
522
        $new = clone $this;
523
        $new->labelAttributes = $values;
524
        return $new;
525
    }
526
527
    /**
528
     * Set the label css class.
529
     *
530
     * @return static
531
     */
532 3
    public function labelClass(string $value): self
533
    {
534 3
        $new = clone $this;
535 3
        $new->labelClass = $value;
536 3
        return $new;
537
    }
538
539
    /**
540
     * The id of a labelable form-related element in the same document as the tag label element.
541
     *
542
     * The first element in the document with an id matching the value of the for attribute is the labeled control for
543
     * this label element, if it is a labelable element.
544
     *
545
     * @param string|null $value The id of a labelable form-related element in the same document as the tag label
546
     * element. If null, the attribute will be removed.
547
     *
548
     * @return static
549
     */
550 2
    public function labelFor(?string $value): self
551
    {
552 2
        $new = clone $this;
553 2
        $new->labelAttributes['for'] = $value;
554 2
        return $new;
555
    }
556
557
    /**
558
     * It allows defining placeholder.
559
     *
560
     * @param string $value
561
     *
562
     * @return static
563
     *
564
     * @link https://html.spec.whatwg.org/multipage/input.html#the-placeholder-attribute
565
     */
566 7
    public function placeholder(string $value): self
567
    {
568 7
        $new = clone $this;
569 7
        $new->placeholder = $value;
570 7
        return $new;
571
    }
572
573
    /**
574
     * A Boolean attribute which, if present, means this field cannot be edited by the user.
575
     * Its value can, however, still be changed by JavaScript code directly setting the HTMLInputElement.value
576
     * property.
577
     *
578
     * @param bool $value
579
     *
580
     * @return static
581
     *
582
     * @link https://html.spec.whatwg.org/multipage/input.html#the-readonly-attribute
583
     */
584 10
    public function readonly(bool $value = true): self
585
    {
586 10
        $new = clone $this;
587 10
        $new->attributes['readonly'] = $value;
588 10
        return $new;
589
    }
590
591
    /**
592
     * If it is required to fill in a value in order to submit the form.
593
     *
594
     * @return static
595
     *
596
     * @link https://www.w3.org/TR/html52/sec-forms.html#the-required-attribute
597
     */
598 15
    public function required(): self
599
    {
600 15
        $new = clone $this;
601 15
        $new->attributes['required'] = true;
602 15
        return $new;
603
    }
604
605
    /**
606
     * Set layout template for render a field.
607
     *
608
     * @param string $value
609
     *
610
     * @return static
611
     */
612
    public function template(string $value): self
613
    {
614
        $new = clone $this;
615
        $new->template = $value;
616
        return $new;
617
    }
618
619
    /**
620
     * Set the value valid css class.
621
     *
622
     * @param string $value is the valid css class.
623
     *
624
     * @return static
625
     */
626 4
    public function validClass(string $value): self
627
    {
628 4
        $new = clone $this;
629 4
        $new->validClass = $value;
630 4
        return $new;
631
    }
632
633
    /**
634
     * Set container class each for field type.
635
     *
636
     * @param array $containerClass The container class to be applied to field's container tag.
637
     *
638
     * ```php
639
     * [Field::TYPE_TEXT => 'test-class-1', Field::TYPE_SUBMIT_BUTTON => 'test-class-2']
640
     *
641
     * @return static
642
     *
643
     * @psalm-param array<string, string> $containerClass
644
     */
645
    public function containerIndividualClass(array $containerClass): self
646
    {
647
        $new = clone $this;
648
        $new->containerIndividualClass = $containerClass;
649
        return $new;
650
    }
651
652
    /**
653
     * Disabled container for field.
654
     *
655
     * @return static
656
     */
657 1
    public function withoutContainer(): self
658
    {
659 1
        $new = clone $this;
660 1
        $new->container = false;
661 1
        return $new;
662
    }
663
664
    /**
665
     * Set input class for a field.
666
     *
667
     * @param array $inputClass The input class to be applied field container.
668
     *
669
     * ```php
670
     * [Field::TYPE_TEXT => 'test-class-1', Field::TYPE_SUBMIT_BUTTON => 'test-class-2']
671
     *
672
     * @return static
673
     *
674
     * @psalm-param array<string, string> $inputClass
675
     */
676
    public function withInputClass(array $inputClass): self
677
    {
678
        $new = clone $this;
679
        $new->inputsClass = $inputClass;
680
        return $new;
681
    }
682
}
683