Skip to content

Commit 4e53312

Browse files
authored
Add TagContentTrait to remove code duplication
1 parent 7510d15 commit 4e53312

3 files changed

Lines changed: 103 additions & 168 deletions

File tree

src/Tag/Base/ContentTag.php

Lines changed: 1 addition & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -4,88 +4,7 @@
44

55
namespace Yiisoft\Html\Tag\Base;
66

7-
use Stringable;
8-
use Yiisoft\Html\Html;
9-
use Yiisoft\Html\NoEncodeStringableInterface;
10-
117
abstract class ContentTag extends NormalTag
128
{
13-
private ?bool $encode = null;
14-
private bool $doubleEncode = true;
15-
16-
/**
17-
* @var string[]|Stringable[]
18-
*/
19-
private array $content = [];
20-
21-
/**
22-
* @param bool|null $encode Whether to encode tag content. Supported values:
23-
* - `null`: stringable objects that implement interface {@see NoEncodeStringableInterface} are not encoded,
24-
* everything else is encoded;
25-
* - `true`: any content is encoded;
26-
* - `false`: nothing is encoded.
27-
* Defaults to `null`.
28-
*
29-
* @return static
30-
*/
31-
final public function encode(?bool $encode): self
32-
{
33-
$new = clone $this;
34-
$new->encode = $encode;
35-
return $new;
36-
}
37-
38-
/**
39-
* @param bool $doubleEncode Whether already encoded HTML entities in tag content should be encoded.
40-
* Defaults to `true`.
41-
*
42-
* @return static
43-
*/
44-
final public function doubleEncode(bool $doubleEncode): self
45-
{
46-
$new = clone $this;
47-
$new->doubleEncode = $doubleEncode;
48-
return $new;
49-
}
50-
51-
/**
52-
* @param string|Stringable ...$content Tag content.
53-
*
54-
* @return static
55-
*/
56-
final public function content(...$content): self
57-
{
58-
$new = clone $this;
59-
$new->content = $content;
60-
return $new;
61-
}
62-
63-
/**
64-
* @param string|Stringable ...$content Tag content.
65-
*
66-
* @return static
67-
*/
68-
public function addContent(...$content): self
69-
{
70-
$new = clone $this;
71-
$new->content = [...$new->content, ...$content];
72-
return $new;
73-
}
74-
75-
/**
76-
* @return string Obtain tag content considering encoding options.
77-
*/
78-
final protected function generateContent(): string
79-
{
80-
$content = '';
81-
foreach ($this->content as $item) {
82-
if ($this->encode || ($this->encode === null && !($item instanceof NoEncodeStringableInterface))) {
83-
$item = Html::encode($item, $this->doubleEncode);
84-
}
85-
86-
$content .= $item;
87-
}
88-
89-
return $content;
90-
}
9+
use TagContentTrait;
9110
}

src/Tag/Base/TagContentTrait.php

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

src/Tag/CustomTag.php

Lines changed: 7 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,15 @@
44

55
namespace Yiisoft\Html\Tag;
66

7-
use Stringable;
8-
use Yiisoft\Html\Html;
9-
use Yiisoft\Html\NoEncodeStringableInterface;
107
use Yiisoft\Html\Tag\Base\Tag;
8+
use Yiisoft\Html\Tag\Base\TagContentTrait;
119

1210
/**
1311
* Custom HTML tag.
1412
*/
1513
final class CustomTag extends Tag
1614
{
17-
private const TYPE_AUTO = 0;
18-
private const TYPE_NORMAL = 1;
19-
private const TYPE_VOID = 2;
20-
21-
private int $type = self::TYPE_AUTO;
15+
use TagContentTrait;
2216

2317
/**
2418
* List of void elements. These only have a start tag; end tags must not be specified.
@@ -44,14 +38,12 @@ final class CustomTag extends Tag
4438
'wbr' => 1,
4539
];
4640

47-
private string $name;
48-
private ?bool $encode = null;
49-
private bool $doubleEncode = true;
41+
private const TYPE_AUTO = 0;
42+
private const TYPE_NORMAL = 1;
43+
private const TYPE_VOID = 2;
5044

51-
/**
52-
* @var string[]|Stringable[]
53-
*/
54-
private array $content = [];
45+
private int $type = self::TYPE_AUTO;
46+
private string $name;
5547

5648
private function __construct(string $name)
5749
{
@@ -98,60 +90,6 @@ public function void(): self
9890
return $new;
9991
}
10092

101-
/**
102-
* @param bool|null $encode Whether to encode tag content. Supported values:
103-
* - `null`: stringable objects that implement interface {@see NoEncodeStringableInterface} are not encoded,
104-
* everything else is encoded;
105-
* - `true`: any content is encoded;
106-
* - `false`: nothing is encoded.
107-
* Defaults to `null`.
108-
*
109-
* @return static
110-
*/
111-
public function encode(?bool $encode): self
112-
{
113-
$new = clone $this;
114-
$new->encode = $encode;
115-
return $new;
116-
}
117-
118-
/**
119-
* @param bool $doubleEncode Whether already encoded HTML entities in tag content should be encoded.
120-
* Defaults to `true`.
121-
*
122-
* @return static
123-
*/
124-
public function doubleEncode(bool $doubleEncode): self
125-
{
126-
$new = clone $this;
127-
$new->doubleEncode = $doubleEncode;
128-
return $new;
129-
}
130-
131-
/**
132-
* @param string|Stringable ...$content Tag content.
133-
*
134-
* @return static
135-
*/
136-
public function content(...$content): self
137-
{
138-
$new = clone $this;
139-
$new->content = $content;
140-
return $new;
141-
}
142-
143-
/**
144-
* @param string|Stringable ...$content Tag content.
145-
*
146-
* @return static
147-
*/
148-
public function addContent(...$content): self
149-
{
150-
$new = clone $this;
151-
$new->content = [...$new->content, ...$content];
152-
return $new;
153-
}
154-
15593
protected function getName(): string
15694
{
15795
return $this->name;
@@ -179,21 +117,4 @@ public function close(): string
179117
{
180118
return '</' . $this->getName() . '>';
181119
}
182-
183-
/**
184-
* @return string Obtain tag content considering encoding options.
185-
*/
186-
private function generateContent(): string
187-
{
188-
$content = '';
189-
foreach ($this->content as $item) {
190-
if ($this->encode || ($this->encode === null && !($item instanceof NoEncodeStringableInterface))) {
191-
$item = Html::encode($item, $this->doubleEncode);
192-
}
193-
194-
$content .= $item;
195-
}
196-
197-
return $content;
198-
}
199120
}

0 commit comments

Comments
 (0)