Passed
Pull Request — master (#192)
by Sergei
02:47
created

AbstractField::hint()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Field\Base;
6
7
use InvalidArgumentException;
8
use Yiisoft\Html\Tag\CustomTag;
9
use Yiisoft\Widget\Widget;
10
11
abstract class AbstractField extends Widget
12
{
13
    /**
14
     * @psalm-var non-empty-string
15
     */
16
    protected string $containerTag = 'div';
17
    protected array $containerTagAttributes = [];
18
    protected bool $useContainer = true;
19
20
    protected string $template = "{label}\n{input}\n{hint}\n{error}";
21
    protected ?bool $hideLabel = null;
22
23
    protected array $labelConfig = [];
24
    protected array $hintConfig = [];
25
    protected array $errorConfig = [];
26
27 4
    final public function containerTag(string $tag): static
28
    {
29 4
        if ($tag === '') {
30 1
            throw new InvalidArgumentException('Tag name cannot be empty.');
31
        }
32
33 3
        $new = clone $this;
34 3
        $new->containerTag = $tag;
35 3
        return $new;
36
    }
37
38 3
    final public function containerTagAttributes(array $attributes): static
39
    {
40 3
        $new = clone $this;
41 3
        $new->containerTagAttributes = $attributes;
42 3
        return $new;
43
    }
44
45 8
    final public function useContainer(bool $use): static
46
    {
47 8
        $new = clone $this;
48 8
        $new->useContainer = $use;
49 8
        return $new;
50
    }
51
52
    /**
53
     * Set layout template for render a field.
54
     */
55 2
    final public function template(string $template): static
56
    {
57 2
        $new = clone $this;
58 2
        $new->template = $template;
59 2
        return $new;
60
    }
61
62 2
    final public function hideLabel(?bool $hide = true): static
63
    {
64 2
        $new = clone $this;
65 2
        $new->hideLabel = $hide;
66 2
        return $new;
67
    }
68
69 3
    final public function labelConfig(array $config): static
70
    {
71 3
        $new = clone $this;
72 3
        $new->labelConfig = $config;
73 3
        return $new;
74
    }
75
76 2
    final public function label(?string $content): static
77
    {
78 2
        $new = clone $this;
79 2
        $new->labelConfig['content()'] = [$content];
80 2
        return $new;
81
    }
82
83 2
    final public function hintConfig(array $config): static
84
    {
85 2
        $new = clone $this;
86 2
        $new->hintConfig = $config;
87 2
        return $new;
88
    }
89
90 2
    final public function hint(?string $content): static
91
    {
92 2
        $new = clone $this;
93 2
        $new->hintConfig['content()'] = [$content];
94 2
        return $new;
95
    }
96
97 2
    final public function errorConfig(array $config): static
98
    {
99 2
        $new = clone $this;
100 2
        $new->errorConfig = $config;
101 2
        return $new;
102
    }
103
104 1
    final public function error(?string $message): static
105
    {
106 1
        $new = clone $this;
107 1
        $new->errorConfig['message()'] = [$message];
108 1
        return $new;
109
    }
110
111 93
    final protected function run(): string
112
    {
113 93
        if (!$this->useContainer) {
114 9
            return $this->generateContent();
115
        }
116
117 84
        $containerTag = CustomTag::name($this->containerTag);
118 84
        if ($this->containerTagAttributes !== []) {
119 3
            $containerTag = $containerTag->attributes($this->containerTagAttributes);
120
        }
121
122 84
        return $containerTag->open()
123
            . PHP_EOL
124 84
            . $this->generateContent()
125
            . PHP_EOL
126 83
            . $containerTag->close();
127
    }
128
129 93
    final protected function generateContent(): string
130
    {
131 92
        $parts = [
132 93
            '{input}' => $this->generateInput(),
133 92
            '{label}' => ($this->hideLabel ?? $this->shouldHideLabel()) ? '' : $this->generateLabel(),
134 92
            '{hint}' => $this->generateHint(),
135 92
            '{error}' => $this->generateError(),
136
        ];
137
138 92
        return preg_replace('/^\h*\v+/m', '', trim(strtr($this->template, $parts)));
139
    }
140
141 72
    protected function shouldHideLabel(): bool
142
    {
143 72
        return false;
144
    }
145
146
    abstract protected function generateInput(): string;
147
148
    abstract protected function generateLabel(): string;
149
150
    abstract protected function generateHint(): string;
151
152
    abstract protected function generateError(): string;
153
}
154