1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Enjoys\Forms; |
6
|
|
|
|
7
|
|
|
use Enjoys\Forms\Interfaces\AttributeInterface; |
8
|
|
|
use Webmozart\Assert\Assert; |
9
|
|
|
use Webmozart\Assert\InvalidArgumentException; |
10
|
|
|
|
11
|
|
|
abstract class Attribute implements AttributeInterface |
12
|
|
|
{ |
13
|
|
|
protected string $name = ''; |
14
|
|
|
|
15
|
|
|
private array $values = []; |
16
|
|
|
|
17
|
|
|
protected bool $withoutValue = true; |
18
|
|
|
protected bool $fillNameAsValue = false; |
19
|
|
|
protected bool $multiple = false; |
20
|
|
|
protected string $separator = ''; |
21
|
|
|
|
22
|
309 |
|
public function withName(string $name): AttributeInterface |
23
|
|
|
{ |
24
|
309 |
|
$new = clone $this; |
25
|
309 |
|
$new->name = $name; |
26
|
309 |
|
return $new; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
|
30
|
337 |
|
public function setWithoutValue(bool $withoutValue): AttributeInterface |
31
|
|
|
{ |
32
|
337 |
|
$this->withoutValue = $withoutValue; |
33
|
337 |
|
return $this; |
34
|
|
|
} |
35
|
|
|
|
36
|
1 |
|
public function setFillNameAsValue(bool $fillNameAsValue): AttributeInterface |
37
|
|
|
{ |
38
|
1 |
|
$this->fillNameAsValue = $fillNameAsValue; |
39
|
1 |
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
32 |
|
public function setMultiple(bool $multiple, string $separator = ' '): AttributeInterface |
43
|
|
|
{ |
44
|
32 |
|
$this->multiple = $multiple; |
45
|
32 |
|
$this->separator = $separator; |
46
|
32 |
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
3 |
|
public function setSeparator(string $separator): AttributeInterface |
51
|
|
|
{ |
52
|
3 |
|
$this->separator = $separator; |
53
|
3 |
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
332 |
|
public function getName(): string |
57
|
|
|
{ |
58
|
332 |
|
return $this->name; |
59
|
|
|
} |
60
|
|
|
|
61
|
73 |
|
public function __toString(): string |
62
|
|
|
{ |
63
|
73 |
|
if ($this->getName() === '') { |
64
|
|
|
return ''; |
65
|
|
|
} |
66
|
73 |
|
if ($this->withoutValue && empty($this->values)) { |
67
|
21 |
|
if ($this->fillNameAsValue) { |
68
|
1 |
|
return sprintf('%1$s="%1$s"', $this->getName()); |
69
|
|
|
} |
70
|
21 |
|
return $this->getName(); |
71
|
|
|
} |
72
|
|
|
|
73
|
65 |
|
if (!$this->withoutValue && empty($this->values)) { |
74
|
10 |
|
return ''; |
75
|
|
|
} |
76
|
|
|
|
77
|
60 |
|
return sprintf('%s="%s"', $this->getName(), $this->getValueString()); |
78
|
|
|
} |
79
|
|
|
|
80
|
178 |
|
public function getValues(): array |
81
|
|
|
{ |
82
|
178 |
|
return $this->values; |
83
|
|
|
} |
84
|
|
|
|
85
|
170 |
|
public function getValueString(): string |
86
|
|
|
{ |
87
|
170 |
|
return implode($this->separator, $this->getValues()); |
88
|
|
|
} |
89
|
|
|
|
90
|
4 |
|
public function set(array $values): void |
91
|
|
|
{ |
92
|
4 |
|
$this->clear(); |
93
|
4 |
|
foreach ($values as $item) { |
94
|
4 |
|
$this->add($item); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
326 |
|
public function clear(): void |
99
|
|
|
{ |
100
|
326 |
|
$this->values = []; |
101
|
|
|
} |
102
|
|
|
|
103
|
335 |
|
public function has(mixed $value): bool |
104
|
|
|
{ |
105
|
335 |
|
return in_array($value, $this->values, true); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
|
109
|
342 |
|
public function add(mixed $value): AttributeInterface |
110
|
|
|
{ |
111
|
342 |
|
$value = $this->normalize($value); |
112
|
|
|
|
113
|
341 |
|
if ($value === null) { |
114
|
150 |
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
335 |
|
if (!$this->multiple) { |
118
|
324 |
|
$this->clear(); |
119
|
|
|
} else { |
120
|
28 |
|
$value = explode($this->separator, $value); |
121
|
|
|
} |
122
|
335 |
|
foreach ((array)$value as $item) { |
123
|
335 |
|
if (!$this->has($item)) { |
124
|
335 |
|
$this->values[] = $item; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
335 |
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
4 |
|
public function remove(string $value): bool |
132
|
|
|
{ |
133
|
4 |
|
$key = array_search($value, $this->values, true); |
134
|
|
|
|
135
|
4 |
|
if ($key === false) { |
136
|
1 |
|
return false; |
137
|
|
|
} |
138
|
|
|
|
139
|
4 |
|
unset($this->values[$key]); |
140
|
|
|
|
141
|
4 |
|
return true; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param mixed $value |
146
|
|
|
* @return string|null |
147
|
|
|
* @throws InvalidArgumentException |
148
|
|
|
*/ |
149
|
342 |
|
private function normalize(mixed $value): ?string |
150
|
|
|
{ |
151
|
342 |
|
if ($value instanceof \Closure) { |
152
|
5 |
|
$value = $value(); |
153
|
|
|
} |
154
|
|
|
|
155
|
342 |
|
Assert::nullOrScalar($value); |
156
|
|
|
|
157
|
341 |
|
return ($value === null) ? null : $this->safety((string)$value); |
158
|
|
|
} |
159
|
|
|
|
160
|
335 |
|
private function safety(string $value): string |
161
|
|
|
{ |
162
|
335 |
|
return \htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|