|
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
|
|
|
private array $attributes = []; |
|
14
|
|
|
|
|
15
|
|
|
private array $attr = []; |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
308 |
|
public function getAttributeCollection(string $namespace = 'general'): AttributeCollection |
|
19
|
|
|
{ |
|
20
|
308 |
|
if (!array_key_exists($namespace, $this->attr)) { |
|
21
|
308 |
|
$this->attr[$namespace] = new AttributeCollection(); |
|
22
|
|
|
} |
|
23
|
308 |
|
return $this->attr[$namespace]; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
134 |
|
public function getAttr(string $name, string $namespace = 'general'): AttributeInterface|null |
|
27
|
|
|
{ |
|
28
|
134 |
|
return $this->getAttributeCollection($namespace)->get($name); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @return $this |
|
33
|
|
|
*/ |
|
34
|
1 |
|
public function setAttrsWithClear(array $attributes, string $namespace = 'general') |
|
35
|
|
|
{ |
|
36
|
1 |
|
$this->getAttributeCollection($namespace)->clear(); |
|
37
|
1 |
|
return $this->setAttrs($attributes, $namespace); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param AttributeInterface[] $attributes |
|
42
|
|
|
* @param string $namespace |
|
43
|
|
|
* @return $this |
|
44
|
|
|
*/ |
|
45
|
299 |
|
public function setAttrs(array $attributes, string $namespace = 'general') |
|
46
|
|
|
{ |
|
47
|
299 |
|
foreach ($attributes as $attribute) { |
|
48
|
299 |
|
$this->setAttr($attribute, $namespace); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
299 |
|
return $this; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return $this |
|
57
|
|
|
*/ |
|
58
|
301 |
|
public function setAttr(AttributeInterface $attribute, string $namespace = 'general') |
|
59
|
|
|
{ |
|
60
|
301 |
|
$attributeCollection = $this->getAttributeCollection($namespace); |
|
61
|
301 |
|
$attributeCollection->remove($attribute); |
|
62
|
301 |
|
$this->addAttr($attribute, $namespace); |
|
63
|
301 |
|
return $this; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return $this |
|
68
|
|
|
*/ |
|
69
|
4 |
|
public function addAttrs(array $attributes, string $namespace = 'general') |
|
70
|
|
|
{ |
|
71
|
4 |
|
foreach ($attributes as $attribute) { |
|
72
|
4 |
|
$this->addAttr($attribute, $namespace); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
4 |
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return $this |
|
81
|
|
|
*/ |
|
82
|
305 |
|
public function addAttr(AttributeInterface $attribute, string $namespace = 'general') |
|
83
|
|
|
{ |
|
84
|
305 |
|
$attributeCollection = $this->getAttributeCollection($namespace); |
|
85
|
305 |
|
$attributeCollection->add($attribute); |
|
86
|
305 |
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return AttributeCollection |
|
91
|
|
|
* @see getAttributeCollection |
|
92
|
|
|
*/ |
|
93
|
3 |
|
public function getAttrs(string $namespace = 'general'): AttributeCollection |
|
94
|
|
|
{ |
|
95
|
3 |
|
return $this->getAttributeCollection($namespace); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $namespace |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
53 |
|
public function getAttributesString(string $namespace = 'general'): string |
|
104
|
|
|
{ |
|
105
|
53 |
|
$attributesStringed = (string)$this->getAttributeCollection($namespace); |
|
106
|
|
|
|
|
107
|
53 |
|
if (empty($attributesStringed)) { |
|
108
|
15 |
|
return ''; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
51 |
|
return ' ' . $attributesStringed; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @return $this |
|
116
|
|
|
*/ |
|
117
|
175 |
|
public function removeAttr(string|AttributeInterface $attribute, string $namespace = 'general') |
|
118
|
|
|
{ |
|
119
|
175 |
|
$this->getAttributeCollection($namespace)->remove($attribute); |
|
120
|
175 |
|
return $this; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
7 |
|
public function addClass(mixed $class, string $namespace = 'general'): self |
|
124
|
|
|
{ |
|
125
|
7 |
|
$attrCollection = $this->getAttributeCollection($namespace); |
|
126
|
7 |
|
$attr = $attrCollection->get('class'); |
|
127
|
7 |
|
if ($attr === null) { |
|
128
|
6 |
|
$attr = AttributeFactory::create('class'); |
|
129
|
6 |
|
$attrCollection->add($attr); |
|
130
|
|
|
} |
|
131
|
7 |
|
$attr->add($class); |
|
132
|
7 |
|
return $this; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @return $this |
|
137
|
|
|
*/ |
|
138
|
5 |
|
public function addClasses(array $classes, string $namespace = 'general') |
|
139
|
|
|
{ |
|
140
|
5 |
|
foreach ($classes as $class) { |
|
141
|
5 |
|
$this->addClass($class, $namespace); |
|
142
|
|
|
} |
|
143
|
5 |
|
return $this; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
3 |
|
public function getClassesList(string $namespace = 'general'): array |
|
147
|
|
|
{ |
|
148
|
3 |
|
return $this->getAttr('class', $namespace)?->getValues() ?? []; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @return $this |
|
153
|
|
|
*/ |
|
154
|
2 |
|
public function removeClass(string $classValue, string $namespace = 'general') |
|
155
|
|
|
{ |
|
156
|
2 |
|
$this->getAttr('class', $namespace)?->remove($classValue); |
|
157
|
2 |
|
return $this; |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|