Passed
Pull Request — master (#192)
by Sergei
16:07 queued 13:36
created

BaseField   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 54
dl 0
loc 128
ccs 56
cts 56
cp 1
rs 10
c 0
b 0
f 0
wmc 21

11 Methods

Rating   Name   Duplication   Size   Complexity  
A useContainer() 0 5 1
A containerTag() 0 9 2
A containerTagAttributes() 0 5 1
A renderEnd() 0 14 3
A beforeRender() 0 2 1
A generateBeginContent() 0 3 1
A generateEndContent() 0 3 1
A run() 0 21 4
A renderOpenContainerAndContent() 0 16 4
A prepareContainerTagAttributes() 0 2 1
A begin() 0 15 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Field\Base;
6
7
use InvalidArgumentException;
8
use Yiisoft\Html\Html;
9
use Yiisoft\Html\Tag\CustomTag;
10
use Yiisoft\Widget\Widget;
11
12
abstract class BaseField extends Widget
13
{
14
    /**
15
     * @psalm-var non-empty-string
16
     */
17
    protected string $containerTag = 'div';
18
    protected array $containerTagAttributes = [];
19
    protected bool $useContainer = true;
20
21
    private bool $isStartedByBegin = false;
22
23 4
    final public function containerTag(string $tag): static
24
    {
25 4
        if ($tag === '') {
26 1
            throw new InvalidArgumentException('Tag name cannot be empty.');
27
        }
28
29 3
        $new = clone $this;
30 3
        $new->containerTag = $tag;
31 3
        return $new;
32
    }
33
34 5
    final public function containerTagAttributes(array $attributes): static
35
    {
36 5
        $new = clone $this;
37 5
        $new->containerTagAttributes = $attributes;
38 5
        return $new;
39
    }
40
41 20
    final public function useContainer(bool $use): static
42
    {
43 20
        $new = clone $this;
44 20
        $new->useContainer = $use;
45 20
        return $new;
46
    }
47
48 4
    final public function begin(): ?string
49
    {
50 4
        parent::begin();
51 4
        $this->isStartedByBegin = true;
52
53 4
        $this->beforeRender();
54
55 4
        $content = $this->generateBeginContent();
56
57 4
        $html = $this->renderOpenContainerAndContent($content);
58 4
        if ($html === '') {
59 1
            return '';
60
        }
61
62 3
        return $html . "\n";
63
    }
64
65 140
    final protected function run(): string
66
    {
67 140
        if ($this->isStartedByBegin) {
68 4
            $this->isStartedByBegin = false;
69 4
            return $this->renderEnd();
70
        }
71
72 137
        $this->beforeRender();
73
74 137
        $content = $this->generateContent();
75 134
        if ($content === null) {
76 1
            return '';
77
        }
78
79 133
        $result = $this->renderOpenContainerAndContent($content);
80
81 133
        if ($this->useContainer) {
82 112
            $result .= "\n" . Html::closeTag($this->containerTag);
83
        }
84
85 133
        return $result;
86
    }
87
88
    protected function beforeRender(): void
89
    {
90
    }
91
92
    abstract protected function generateContent(): ?string;
93
94 2
    protected function generateBeginContent(): string
95
    {
96 2
        return '';
97
    }
98
99 2
    protected function generateEndContent(): string
100
    {
101 2
        return '';
102
    }
103
104
    protected function prepareContainerTagAttributes(array &$attributes): void
105
    {
106
    }
107
108 4
    private function renderEnd(): string
109
    {
110 4
        $content = $this->generateEndContent();
111
112 4
        if (!$this->useContainer) {
113 1
            return $content;
114
        }
115
116 3
        $containerTag = CustomTag::name($this->containerTag);
117
118
        return
119
            "\n"
120 3
            . ($content !== '' ? $content . "\n" : '')
121 3
            . $containerTag->close();
122
    }
123
124 136
    private function renderOpenContainerAndContent(string $content): string
125
    {
126 136
        if (!$this->useContainer) {
127 23
            return $content;
128
        }
129
130 115
        $containerTag = CustomTag::name($this->containerTag);
131
132 115
        $attributes = $this->containerTagAttributes;
133 115
        $this->prepareContainerTagAttributes($attributes);
134 115
        if ($attributes !== []) {
135 7
            $containerTag = $containerTag->attributes($attributes);
136
        }
137
138 115
        return $containerTag->open()
139 115
            . ($content === '' ? '' : ("\n" . $content));
140
    }
141
}
142