generated from yii-tools/template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHasPrefixCollection.php
86 lines (73 loc) · 2.38 KB
/
HasPrefixCollection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
declare(strict_types=1);
namespace UIAwesome\Html\Concern;
use UIAwesome\Html\{Helper\CssClass, Helper\Sanitize, Interop\RenderInterface};
/**
* Is used by widgets that implement the prefix collection class.
*/
trait HasPrefixCollection
{
protected string $prefix = '';
protected array $prefixAttributes = [];
protected false|string $prefixTag = false;
/**
* Set the `HTML` prefix content.
*
* @param RenderInterface|string ...$values The `HTML` prefix content.
*
* @return static A new instance of the current class with the specified prefix content.
*/
public function prefix(string|RenderInterface ...$values): static
{
$new = clone $this;
$new->prefix = Sanitize::html(...$values);
return $new;
}
/**
* Set the `HTML` attributes for the prefix tag.
*
* @param array $values Attribute values indexed by attribute names.
*
* @return static A new instance of the current class with the specified prefix attributes.
*/
public function prefixAttributes(array $values): static
{
$new = clone $this;
$new->prefixAttributes = $values;
return $new;
}
/**
* Sets the `CSS` class that will be assigned to the prefix.
*
* @param string $value The CSS class name.
* @param bool $override If `true` the value will be overridden.
*
* @return static A new instance of the current class with the specified prefix class.
*/
public function prefixClass(string $value, bool $override = false): static
{
$new = clone $this;
CssClass::add($new->prefixAttributes, $value, $override);
return $new;
}
/**
* Set the prefix tag name.
*
* @param false|string $value The tag name for the prefix element.
* If `false` the prefix tag will be disabled.
*
* @throws \InvalidArgumentException If the prefix tag is an empty string.
*
* @return static A new instance of the current class with the specified prefix tag.
* If `false` the prefix tag will be disabled.
*/
public function prefixTag(false|string $value = 'div'): static
{
if ($value === '') {
throw new \InvalidArgumentException('The prefix tag must be a non-empty string.');
}
$new = clone $this;
$new->prefixTag = $value;
return $new;
}
}