1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Enjoys\Forms\Traits; |
6
|
|
|
|
7
|
|
|
use Enjoys\Forms\AttributeCollection; |
8
|
|
|
use Enjoys\Forms\AttributeFactory; |
9
|
|
|
use Enjoys\Forms\Interfaces\AttributeInterface; |
10
|
|
|
|
11
|
|
|
trait Attributes |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var array<string, AttributeCollection> |
15
|
|
|
*/ |
16
|
|
|
private array $attributes = []; |
17
|
|
|
|
18
|
|
|
|
19
|
332 |
|
public function getAttributeCollection(string $namespace = 'general'): AttributeCollection |
20
|
|
|
{ |
21
|
332 |
|
if (!array_key_exists($namespace, $this->attributes)) { |
22
|
332 |
|
$this->attributes[$namespace] = new AttributeCollection(); |
23
|
|
|
} |
24
|
332 |
|
return $this->attributes[$namespace]; |
25
|
|
|
} |
26
|
|
|
|
27
|
174 |
|
public function getAttribute(string $name, string $namespace = 'general'): AttributeInterface|null |
28
|
|
|
{ |
29
|
174 |
|
return $this->getAttributeCollection($namespace)->get($name); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param AttributeInterface[] $attributes |
34
|
|
|
*/ |
35
|
1 |
|
public function setAttributesWithClear(array $attributes, string $namespace = 'general'): static |
36
|
|
|
{ |
37
|
1 |
|
$this->getAttributeCollection($namespace)->clear(); |
38
|
1 |
|
return $this->setAttributes($attributes, $namespace); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param AttributeInterface[] $attributes |
43
|
|
|
*/ |
44
|
323 |
|
public function setAttributes(array $attributes, string $namespace = 'general'): static |
45
|
|
|
{ |
46
|
323 |
|
foreach ($attributes as $attribute) { |
47
|
323 |
|
$this->setAttribute($attribute, $namespace); |
48
|
|
|
} |
49
|
|
|
|
50
|
323 |
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
|
54
|
325 |
|
public function setAttribute(AttributeInterface $attribute, string $namespace = 'general'): static |
55
|
|
|
{ |
56
|
325 |
|
$attributeCollection = $this->getAttributeCollection($namespace); |
57
|
325 |
|
$attributeCollection->remove($attribute); |
58
|
325 |
|
$this->addAttribute($attribute, $namespace); |
59
|
325 |
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param AttributeInterface[] $attributes |
64
|
|
|
*/ |
65
|
4 |
|
public function addAttributes(array $attributes, string $namespace = 'general'): static |
66
|
|
|
{ |
67
|
4 |
|
foreach ($attributes as $attribute) { |
68
|
4 |
|
$this->addAttribute($attribute, $namespace); |
69
|
|
|
} |
70
|
|
|
|
71
|
4 |
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
329 |
|
public function addAttribute(AttributeInterface $attribute, string $namespace = 'general'): static |
75
|
|
|
{ |
76
|
329 |
|
$attributeCollection = $this->getAttributeCollection($namespace); |
77
|
329 |
|
$attributeCollection->add($attribute); |
78
|
329 |
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @see getAttributeCollection |
83
|
|
|
*/ |
84
|
3 |
|
public function getAttributes(string $namespace = 'general'): AttributeCollection |
85
|
|
|
{ |
86
|
3 |
|
return $this->getAttributeCollection($namespace); |
87
|
|
|
} |
88
|
|
|
|
89
|
51 |
|
public function getAttributesString(string $namespace = 'general'): string |
90
|
|
|
{ |
91
|
51 |
|
$attributesStringed = (string)$this->getAttributeCollection($namespace); |
92
|
|
|
|
93
|
51 |
|
if (empty($attributesStringed)) { |
94
|
16 |
|
return ''; |
95
|
|
|
} |
96
|
|
|
|
97
|
48 |
|
return ' ' . $attributesStringed; |
98
|
|
|
} |
99
|
|
|
|
100
|
189 |
|
public function removeAttribute(string|AttributeInterface $attribute, string $namespace = 'general'): static |
101
|
|
|
{ |
102
|
189 |
|
$this->getAttributeCollection($namespace)->remove($attribute); |
103
|
189 |
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
7 |
|
public function addClass(string $class, string $namespace = 'general'): static |
107
|
|
|
{ |
108
|
7 |
|
$attrCollection = $this->getAttributeCollection($namespace); |
109
|
7 |
|
$attr = $attrCollection->get('class'); |
110
|
7 |
|
if ($attr === null) { |
111
|
6 |
|
$attr = AttributeFactory::create('class'); |
112
|
6 |
|
$attrCollection->add($attr); |
113
|
|
|
} |
114
|
7 |
|
$attr->add($class); |
115
|
7 |
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param string[] $classes |
120
|
|
|
*/ |
121
|
5 |
|
public function addClasses(array $classes, string $namespace = 'general'): static |
122
|
|
|
{ |
123
|
5 |
|
foreach ($classes as $class) { |
124
|
5 |
|
$this->addClass($class, $namespace); |
125
|
|
|
} |
126
|
5 |
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
3 |
|
public function getClassesList(string $namespace = 'general'): array |
130
|
|
|
{ |
131
|
3 |
|
return $this->getAttribute('class', $namespace)?->getValues() ?? []; |
132
|
|
|
} |
133
|
|
|
|
134
|
2 |
|
public function removeClass(string $classValue, string $namespace = 'general'): static |
135
|
|
|
{ |
136
|
2 |
|
$this->getAttribute('class', $namespace)?->remove($classValue); |
137
|
2 |
|
return $this; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|