FillHandler::__construct()   B
last analyzed

Complexity

Conditions 9
Paths 24

Size

Total Lines 25
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 25
ccs 12
cts 12
cp 1
rs 8.0555
cc 9
nc 24
nop 3
crap 9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Enjoys\Forms;
6
7
class FillHandler
8
{
9
    private array $attributes = [];
10
11
    private mixed $value = null;
12
    private ?string $label = null;
13
14
15 75
    public function __construct(mixed $value, mixed $label, bool $useTitleAsValue = false)
16
    {
17 75
        if (is_array($label)) {
18 32
            $this->label = (string)($label[0] ?? null);
19
20 32
            if (isset($label[1]) && is_array($label[1])) {
21 31
                $this->attributes = $label[1];
22
            }
23
        }
24
25 75
        $this->label ??= (string)$label;
26
27
        /** @since 2.4.0 */
28 75
        if (is_string($value)) {
29 31
            $this->value = \trim($value);
30
        }
31
32
        /** @since 2.4.0 */
33 75
        if (is_int($value) && $useTitleAsValue) {
34 28
            $this->value = $this->label;
35
        }
36
37
        /** @since 3.4.0 */
38 75
        if (is_int($value) && !$useTitleAsValue) {
39 17
            $this->value = $value;
40
        }
41
    }
42
43 75
    public function getAttributes(): array
44
    {
45 75
        return $this->attributes;
46
    }
47
48 75
    public function getValue(): ?string
49
    {
50 75
        return (string)$this->value;
51
    }
52
53 75
    public function getLabel(): ?string
54
    {
55 75
        return $this->label;
56
    }
57
}
58