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
|
299 |
|
public function getAttributeCollection(string $namespace = 'general'): AttributeCollection |
19
|
|
|
{ |
20
|
299 |
|
if (!array_key_exists($namespace, $this->attr)) { |
21
|
299 |
|
$this->attr[$namespace] = new AttributeCollection(); |
22
|
|
|
} |
23
|
299 |
|
return $this->attr[$namespace]; |
24
|
|
|
} |
25
|
|
|
|
26
|
133 |
|
public function getAttribute(string $name, string $namespace = 'general'): AttributeInterface|null |
27
|
|
|
{ |
28
|
133 |
|
return $this->getAttributeCollection($namespace)->get($name); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return $this |
33
|
|
|
*/ |
34
|
1 |
|
public function setAttributesWithClear(array $attributes, string $namespace = 'general') |
35
|
|
|
{ |
36
|
1 |
|
$this->getAttributeCollection($namespace)->clear(); |
37
|
1 |
|
return $this->setAttributes($attributes, $namespace); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param AttributeInterface[] $attributes |
42
|
|
|
* @param string $namespace |
43
|
|
|
* @return $this |
44
|
|
|
*/ |
45
|
290 |
|
public function setAttributes(array $attributes, string $namespace = 'general') |
46
|
|
|
{ |
47
|
290 |
|
foreach ($attributes as $attribute) { |
48
|
290 |
|
$this->setAttribute($attribute, $namespace); |
49
|
|
|
} |
50
|
|
|
|
51
|
290 |
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return $this |
57
|
|
|
*/ |
58
|
292 |
|
public function setAttribute(AttributeInterface $attribute, string $namespace = 'general') |
59
|
|
|
{ |
60
|
292 |
|
$attributeCollection = $this->getAttributeCollection($namespace); |
61
|
292 |
|
$attributeCollection->remove($attribute); |
62
|
292 |
|
$this->addAttribute($attribute, $namespace); |
63
|
292 |
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return $this |
68
|
|
|
*/ |
69
|
4 |
|
public function addAttributes(array $attributes, string $namespace = 'general') |
70
|
|
|
{ |
71
|
4 |
|
foreach ($attributes as $attribute) { |
72
|
4 |
|
$this->addAttribute($attribute, $namespace); |
73
|
|
|
} |
74
|
|
|
|
75
|
4 |
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return $this |
81
|
|
|
*/ |
82
|
296 |
|
public function addAttribute(AttributeInterface $attribute, string $namespace = 'general') |
83
|
|
|
{ |
84
|
296 |
|
$attributeCollection = $this->getAttributeCollection($namespace); |
85
|
296 |
|
$attributeCollection->add($attribute); |
86
|
296 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return AttributeCollection |
91
|
|
|
* @see getAttributeCollection |
92
|
|
|
*/ |
93
|
3 |
|
public function getAttributes(string $namespace = 'general'): AttributeCollection |
94
|
|
|
{ |
95
|
3 |
|
return $this->getAttributeCollection($namespace); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* |
100
|
|
|
* @param string $namespace |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
51 |
|
public function getAttributesString(string $namespace = 'general'): string |
104
|
|
|
{ |
105
|
51 |
|
$attributesStringed = (string)$this->getAttributeCollection($namespace); |
106
|
|
|
|
107
|
51 |
|
if (empty($attributesStringed)) { |
108
|
16 |
|
return ''; |
109
|
|
|
} |
110
|
|
|
|
111
|
48 |
|
return ' ' . $attributesStringed; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return $this |
116
|
|
|
*/ |
117
|
174 |
|
public function removeAttribute(string|AttributeInterface $attribute, string $namespace = 'general') |
118
|
|
|
{ |
119
|
174 |
|
$this->getAttributeCollection($namespace)->remove($attribute); |
120
|
174 |
|
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->getAttribute('class', $namespace)?->getValues() ?? []; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @return $this |
153
|
|
|
*/ |
154
|
2 |
|
public function removeClass(string $classValue, string $namespace = 'general') |
155
|
|
|
{ |
156
|
2 |
|
$this->getAttribute('class', $namespace)?->remove($classValue); |
157
|
2 |
|
return $this; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|