1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Field\Base; |
6
|
|
|
|
7
|
|
|
use Stringable; |
8
|
|
|
use Yiisoft\Html\Html; |
9
|
|
|
use Yiisoft\Html\NoEncodeStringableInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Adds functionality for processing with field content. |
13
|
|
|
*/ |
14
|
|
|
trait FieldContentTrait |
15
|
|
|
{ |
16
|
|
|
private ?bool $encodeContent = null; |
17
|
|
|
private bool $doubleEncodeContent = true; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @psalm-var list<string|Stringable> |
21
|
|
|
*/ |
22
|
|
|
private array $content = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param bool|null $encode Whether to encode field 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
|
|
|
* @return static |
33
|
|
|
*/ |
34
|
3 |
|
final public function encodeContent(?bool $encode): self |
35
|
|
|
{ |
36
|
3 |
|
$new = clone $this; |
37
|
3 |
|
$new->encodeContent = $encode; |
38
|
3 |
|
return $new; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param bool $doubleEncode Whether already encoded HTML entities in field content should be encoded. |
43
|
|
|
* Defaults to `true`. |
44
|
|
|
* |
45
|
|
|
* @return static |
46
|
|
|
*/ |
47
|
2 |
|
final public function doubleEncodeContent(bool $doubleEncode): self |
48
|
|
|
{ |
49
|
2 |
|
$new = clone $this; |
50
|
2 |
|
$new->doubleEncodeContent = $doubleEncode; |
51
|
2 |
|
return $new; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string|Stringable ...$content Field content. |
56
|
|
|
* |
57
|
|
|
* @return static |
58
|
|
|
*/ |
59
|
21 |
|
final public function content(string|Stringable ...$content): self |
60
|
|
|
{ |
61
|
21 |
|
$new = clone $this; |
62
|
21 |
|
$new->content = array_values($content); |
63
|
21 |
|
return $new; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string|Stringable ...$content Field content. |
68
|
|
|
* |
69
|
|
|
* @return static |
70
|
|
|
*/ |
71
|
4 |
|
final public function addContent(string|Stringable ...$content): self |
72
|
|
|
{ |
73
|
4 |
|
$new = clone $this; |
74
|
4 |
|
$new->content = array_merge($new->content, array_values($content)); |
75
|
4 |
|
return $new; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return string Obtain field content considering encoding options {@see encodeContent()}. |
80
|
|
|
*/ |
81
|
42 |
|
final protected function renderContent(): string |
82
|
|
|
{ |
83
|
42 |
|
$content = ''; |
84
|
|
|
|
85
|
42 |
|
foreach ($this->content as $item) { |
86
|
20 |
|
if ($this->encodeContent || ($this->encodeContent === null && !($item instanceof NoEncodeStringableInterface))) { |
87
|
18 |
|
$item = Html::encode($item, $this->doubleEncodeContent); |
88
|
|
|
} |
89
|
|
|
|
90
|
20 |
|
$content .= $item; |
91
|
|
|
} |
92
|
|
|
|
93
|
42 |
|
return $content; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|