Cell::attributes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\DataView\Column\Base;
6
7
use Stringable;
8
use Yiisoft\Html\NoEncodeStringableInterface;
9
10
final class Cell
11
{
12
    private bool $doubleEncode = true;
13
14
    /**
15
     * @param callable|string|Stringable $content
16
     */
17 81
    public function __construct(
18
        private array $attributes = [],
19
        private ?bool $encode = null,
20
        private mixed $content = '',
21
    ) {
22 81
    }
23
24
    /**
25
     * @param bool|null $encode Whether to encode tag content. Supported values:
26
     *  - `null`: stringable objects that implement interface {@see NoEncodeStringableInterface} are not encoded,
27
     *    everything else is encoded;
28
     *  - `true`: any content is encoded;
29
     *  - `false`: nothing is encoded.
30
     * Defaults to `null`.
31
     */
32 52
    public function encode(?bool $encode): self
33
    {
34 52
        $new = clone $this;
35 52
        $new->encode = $encode;
36 52
        return $new;
37
    }
38
39
    /**
40
     * @param bool $doubleEncode Whether already encoded HTML entities in tag content should be encoded.
41
     * Defaults to `true`.
42
     */
43
    public function doubleEncode(bool $doubleEncode): self
44
    {
45
        $new = clone $this;
46
        $new->doubleEncode = $doubleEncode;
47
        return $new;
48
    }
49
50
    /**
51
     * @param callable|string|Stringable $content Tag content.
52
     */
53 81
    public function content(string|Stringable|callable $content): self
54
    {
55 81
        $new = clone $this;
56 81
        $new->content = $content;
57 81
        return $new;
58
    }
59
60
    /**
61
     * Add a set of attributes to existing cell attributes.
62
     * Same named attributes are replaced.
63
     *
64
     * @param array $attributes Name-value set of attributes.
65
     */
66 81
    public function addAttributes(array $attributes): self
67
    {
68 81
        $new = clone $this;
69 81
        $new->attributes = array_merge($new->attributes, $attributes);
70 81
        return $new;
71
    }
72
73
    /**
74
     * Replace attributes with a new set.
75
     *
76
     * @param array $attributes Name-value set of attributes.
77
     */
78
    public function attributes(array $attributes): self
79
    {
80
        $new = clone $this;
81
        $new->attributes = $attributes;
82
        return $new;
83
    }
84
85
    /**
86
     * Set attribute value.
87
     *
88
     * @param string $name Name of the attribute.
89
     * @param mixed $value Value of the attribute.
90
     */
91
    public function attribute(string $name, mixed $value): self
92
    {
93
        $new = clone $this;
94
        $new->attributes[$name] = $value;
95
        return $new;
96
    }
97
98 81
    public function getAttributes(): array
99
    {
100 81
        return $this->attributes;
101
    }
102
103 81
    public function isEncode(): ?bool
104
    {
105 81
        return $this->encode;
106
    }
107
108 81
    public function isDoubleEncode(): bool
109
    {
110 81
        return $this->doubleEncode;
111
    }
112
113 81
    public function getContent(): string|Stringable|callable
114
    {
115 81
        return $this->content;
116
    }
117
}
118