SectionView::render()   A
last analyzed

Complexity

Conditions 6
Paths 12

Size

Total Lines 36
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 19
nc 12
nop 1
dl 0
loc 36
ccs 0
cts 20
cp 0
crap 42
rs 9.0111
c 0
b 0
f 0
1
<?php
2
3
namespace Leonidas\Library\Admin\Component\Metabox\View;
4
5
use Leonidas\Contracts\Admin\Component\AdminComponentInterface;
6
use Leonidas\Contracts\Ui\ViewInterface;
7
use WebTheory\Html\Traits\ElementConstructorTrait;
8
9
class SectionView implements ViewInterface
10
{
11
    use ElementConstructorTrait;
12
13
    public function render(array $data = []): string
14
    {
15
        $title = $data['title'];
16
        $padding = $data['padding'];
17
        $isFieldset = $data['is_fieldset'];
18
        $request = $data['request'];
19
20
        /** @var AdminComponentInterface[] $components */
21
        $components = $data['components'];
22
23
        $html = '';
24
25
        $titleElement = $this->tag('h3', [], $title);
26
        $attributes = ['class' => "py-{$padding}"];
27
        $container = $isFieldset ? 'fieldset' : 'div';
28
29
        $html .= $this->open($container, $attributes);
30
31
        if ($isFieldset && false) { // @phpstan-ignore-line
32
            // temporarily disabled because legend elements are absolutely
33
            // positioned within their container, making padding not work
34
            // as desired
35
            $html .= $this->tag('legend', [], $titleElement);
36
        } else {
37
            $html .= $titleElement;
38
        }
39
40
        foreach ($components as $component) {
41
            if ($component->shouldBeRendered($request)) {
42
                $html .= $component->renderComponent($request);
43
            }
44
        }
45
46
        $html .= $this->close($container);
47
48
        return $html;
49
    }
50
}
51