FormGroup::setValue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Form\Container;
6
7
use AbterPhp\Framework\Constant\Html5;
8
use AbterPhp\Framework\Form\Element\IElement;
9
use AbterPhp\Framework\Form\Label\Label;
10
use AbterPhp\Framework\Html\Attribute;
11
use AbterPhp\Framework\Html\Helper\Tag as TagHelper;
12
use AbterPhp\Framework\Html\INode;
13
use AbterPhp\Framework\Html\ITemplater;
14
use AbterPhp\Framework\Html\Tag;
15
16
class FormGroup extends Tag implements ITemplater
17
{
18
    public const CLASS_COUNTABLE = 'countable';
19
20
    public const CLASS_REQUIRED = 'required';
21
22
    protected const DEFAULT_TAG = Html5::TAG_DIV;
23
24
    /**
25
     * %1$s - label
26
     * %2$s - input
27
     * %3$s - help
28
     */
29
    protected const DEFAULT_TEMPLATE = '%1$s%2$s%3$s';
30
31
    protected IElement $input;
32
33
    protected Label $label;
34
35
    protected ?INode $help;
36
37
    protected string $template = self::DEFAULT_TEMPLATE;
38
39
    /**
40
     * FormGroup constructor.
41
     *
42
     * @param IElement                     $input
43
     * @param Label                        $label
44
     * @param INode|null                   $help
45
     * @param string[]                     $intents
46
     * @param array<string,Attribute>|null $attributes
47
     * @param string|null                  $tag
48
     */
49
    public function __construct(
50
        IElement $input,
51
        Label $label,
52
        ?INode $help = null,
53
        array $intents = [],
54
        ?array $attributes = null,
55
        ?string $tag = null
56
    ) {
57
        parent::__construct(null, $intents, $attributes, $tag);
58
59
        $this->label = $label;
60
        $this->input = $input;
61
        $this->help  = $help;
62
    }
63
64
    /**
65
     * @param string $value
66
     *
67
     * @return $this
68
     */
69
    public function setValue(string $value): FormGroup
70
    {
71
        $this->input->setValue($value);
72
73
        return $this;
74
    }
75
76
    /**
77
     * @return IElement|null
78
     */
79
    public function getInput(): ?IElement
80
    {
81
        return $this->input;
82
    }
83
84
    /**
85
     * @return Label|null
86
     */
87
    public function getLabel(): ?Label
88
    {
89
        return $this->label;
90
    }
91
92
    /**
93
     * @return INode|null
94
     */
95
    public function getHelp(): ?INode
96
    {
97
        return $this->help;
98
    }
99
100
    /**
101
     * @return IElement[]
102
     */
103
    public function getElements(): array
104
    {
105
        return [$this->input];
106
    }
107
108
    /**
109
     * @param string $template
110
     *
111
     * @return $this
112
     */
113
    public function setTemplate(string $template): INode
114
    {
115
        $this->template = $template;
116
117
        return $this;
118
    }
119
120
    /**
121
     * @return INode[]
122
     */
123
    public function getExtendedNodes(): array
124
    {
125
        $nodes = [$this->label, $this->input];
126
127
        if ($this->help) {
128
            $nodes[] = $this->help;
129
        }
130
131
        return array_merge($nodes, $this->getNodes());
132
    }
133
134
    /**
135
     * @return INode[]
136
     */
137
    public function getNodes(): array
138
    {
139
        return [];
140
    }
141
142
    /**
143
     * @return string
144
     */
145
    public function __toString(): string
146
    {
147
        $help = $this->help ?: '';
148
149
        $content = sprintf(
150
            $this->template,
151
            (string)$this->label,
152
            (string)$this->input,
153
            (string)$help
154
        );
155
156
        return TagHelper::toString($this->tag, $content, $this->attributes);
157
    }
158
}
159