Passed
Pull Request — master (#1)
by Peter
07:07
created

FormGroup   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 36
dl 0
loc 152
rs 10
c 1
b 0
f 0
wmc 12

10 Methods

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