RadioColumnRenderer::renderBody()   A
last analyzed

Complexity

Conditions 5
Paths 12

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 5.0035

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 29
ccs 18
cts 19
cp 0.9474
rs 9.3554
cc 5
nc 12
nop 3
crap 5.0035
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\DataView\Column;
6
7
use InvalidArgumentException;
8
use Stringable;
9
use Yiisoft\Html\Html;
10
use Yiisoft\Yii\DataView\Column\Base\Cell;
11
use Yiisoft\Yii\DataView\Column\Base\DataContext;
12
use Yiisoft\Yii\DataView\Column\Base\GlobalContext;
13
14
final class RadioColumnRenderer implements ColumnRendererInterface
15
{
16
    public function renderColumn(ColumnInterface $column, Cell $cell, GlobalContext $context): Cell
17
    {
18
        $this->checkColumn($column);
19
        return $cell->addAttributes($column->getColumnAttributes());
20
    }
21
22 7
    public function renderHeader(ColumnInterface $column, Cell $cell, GlobalContext $context): ?Cell
23
    {
24 7
        $this->checkColumn($column);
25
26 7
        $header = $column->getHeader();
27 7
        if ($header === null) {
28 4
            return null;
29
        }
30
31 3
        return $cell
32 3
            ->addAttributes($column->getHeaderAttributes())
33 3
            ->content($header);
34
    }
35
36 7
    public function renderFilter(ColumnInterface $column, Cell $cell, GlobalContext $context): ?Cell
37
    {
38 7
        return null;
39
    }
40
41 7
    public function renderBody(ColumnInterface $column, Cell $cell, DataContext $context): Cell
42
    {
43 7
        $this->checkColumn($column);
44
45 7
        $inputAttributes = $column->getInputAttributes();
46 7
        $name = null;
47 7
        $value = null;
48
49 7
        if (!array_key_exists('name', $inputAttributes)) {
50 6
            $name = 'radio-selection';
51
        }
52
53 7
        if (!array_key_exists('value', $inputAttributes)) {
54 7
            $key = $context->getKey();
55 7
            $value = is_array($key)
56
                ? json_encode($key, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)
57 7
                : (string)$key;
58
        }
59
60 7
        $input = Html::radio($name, $value, $inputAttributes);
61
62 7
        $contentClosure = $column->getContent();
63
        /** @var string|Stringable $content */
64 7
        $content = $contentClosure === null ? $input : $contentClosure($input, $context);
65
66 7
        return $cell
67 7
            ->addAttributes($column->getBodyAttributes())
68 7
            ->content($content)
69 7
            ->encode(false);
70
    }
71
72
    public function renderFooter(ColumnInterface $column, Cell $cell, GlobalContext $context): Cell
73
    {
74
        $this->checkColumn($column);
75
76
        if ($column->getFooter() !== null) {
77
            $cell = $cell->content($column->getFooter());
78
        }
79
80
        return $cell;
81
    }
82
83
    /**
84
     * @psalm-assert RadioColumn $column
85
     */
86 7
    private function checkColumn(ColumnInterface $column): void
87
    {
88 7
        if (!$column instanceof RadioColumn) {
89
            throw new InvalidArgumentException(
90
                sprintf(
91
                    'Expected "%s", but "%s" given.',
92
                    RadioColumn::class,
93
                    $column::class
94
                )
95
            );
96
        }
97
    }
98
}
99