Skip to content

Commit 64dff26

Browse files
authored
Refactoring for more consistency (#121)
1 parent 85dfe92 commit 64dff26

4 files changed

Lines changed: 96 additions & 31 deletions

File tree

src/Tag/Input/File.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
final class File extends InputTag
1515
{
1616
private ?string $uncheckValue = null;
17-
private array $uncheckInputTagAttributes = [];
17+
private array $uncheckInputAttributes = [];
1818

1919
/**
2020
* @param bool|float|int|string|Stringable|null $value
@@ -26,10 +26,17 @@ public function uncheckValue($value): self
2626
return $new;
2727
}
2828

29-
public function uncheckInputTagAttributes(array $attributes): self
29+
public function uncheckInputAttributes(array $attributes): self
3030
{
3131
$new = clone $this;
32-
$new->uncheckInputTagAttributes = $attributes;
32+
$new->uncheckInputAttributes = array_merge($new->uncheckInputAttributes, $attributes);
33+
return $new;
34+
}
35+
36+
public function replaceUncheckInputAttributes(array $attributes): self
37+
{
38+
$new = clone $this;
39+
$new->uncheckInputAttributes = $attributes;
3340
return $new;
3441
}
3542

@@ -83,7 +90,7 @@ private function renderUncheckInput(): string
8390
$input = Html::hiddenInput(
8491
Html::getNonArrayableName($name),
8592
$this->uncheckValue,
86-
$this->uncheckInputTagAttributes
93+
$this->uncheckInputAttributes
8794
);
8895

8996
// Make sure disabled input is not sending any value.

src/Tag/Input/Range.php

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ final class Range extends InputTag
2121
/**
2222
* @psalm-var non-empty-string
2323
*/
24-
private string $outputTagName = 'span';
25-
private array $outputTagAttributes = [];
26-
private ?string $outputTagId = null;
24+
private string $outputTag = 'span';
25+
private array $outputAttributes = [];
26+
private ?string $outputId = null;
2727

2828
/**
2929
* Maximum value.
@@ -86,21 +86,28 @@ public function showOutput(bool $show = true): self
8686
return $new;
8787
}
8888

89-
public function outputTagName(string $tagName): self
89+
public function outputTag(string $tagName): self
9090
{
9191
if ($tagName === '') {
9292
throw new InvalidArgumentException('The output tag name it cannot be empty value.');
9393
}
9494

9595
$new = clone $this;
96-
$new->outputTagName = $tagName;
96+
$new->outputTag = $tagName;
9797
return $new;
9898
}
9999

100-
public function outputTagAttributes(array $attributes): self
100+
public function outputAttributes(array $attributes): self
101101
{
102102
$new = clone $this;
103-
$new->outputTagAttributes = $attributes;
103+
$new->outputAttributes = array_merge($this->outputAttributes, $attributes);
104+
return $new;
105+
}
106+
107+
public function replaceOutputAttributes(array $attributes): self
108+
{
109+
$new = clone $this;
110+
$new->outputAttributes = $attributes;
104111
return $new;
105112
}
106113

@@ -109,8 +116,8 @@ protected function prepareAttributes(): void
109116
$this->attributes['type'] = 'range';
110117

111118
if ($this->showOutput) {
112-
$this->outputTagId = (string) ($this->outputTagAttributes['id'] ?? Html::generateId('rangeOutput'));
113-
$this->attributes['oninput'] = 'document.getElementById("' . $this->outputTagId . '").innerHTML=this.value';
119+
$this->outputId = (string) ($this->outputAttributes['id'] ?? Html::generateId('rangeOutput'));
120+
$this->attributes['oninput'] = 'document.getElementById("' . $this->outputId . '").innerHTML=this.value';
114121
}
115122
}
116123

@@ -120,10 +127,10 @@ protected function after(): string
120127
return '';
121128
}
122129

123-
return "\n" . CustomTag::name($this->outputTagName)
124-
->attributes($this->outputTagAttributes)
130+
return "\n" . CustomTag::name($this->outputTag)
131+
->attributes($this->outputAttributes)
125132
->content((string) ($this->attributes['value'] ?? '-'))
126-
->id($this->outputTagId)
133+
->id($this->outputId)
127134
->render();
128135
}
129136
}

tests/common/Tag/Input/FileTest.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,13 @@ public function testUncheckValueForm(): void
6868
);
6969
}
7070

71-
public function testUncheckInputTagAttributes(): void
71+
public function testUncheckInputAttributes(): void
7272
{
7373
$result = File::tag()
7474
->name('avatar')
7575
->uncheckValue(7)
76-
->uncheckInputTagAttributes(['id' => 'FileHidden', 'data-key' => '100'])
76+
->uncheckInputAttributes(['id' => 'FileHidden'])
77+
->uncheckInputAttributes(['data-key' => '100'])
7778
->form('post')
7879
->render();
7980

@@ -84,6 +85,23 @@ public function testUncheckInputTagAttributes(): void
8485
);
8586
}
8687

88+
public function testReplaceUncheckInputAttributes(): void
89+
{
90+
$result = File::tag()
91+
->name('avatar')
92+
->uncheckValue(7)
93+
->uncheckInputAttributes(['id' => 'FileHidden'])
94+
->replaceUncheckInputAttributes(['data-key' => '100'])
95+
->form('post')
96+
->render();
97+
98+
$this->assertSame(
99+
'<input type="hidden" name="avatar" value="7" form="post" data-key="100">' .
100+
'<input type="file" name="avatar" form="post">',
101+
$result
102+
);
103+
}
104+
87105
public function dataAccept(): array
88106
{
89107
return [
@@ -147,6 +165,8 @@ public function testImmutability(): void
147165
$tag = File::tag();
148166

149167
$this->assertNotSame($tag, $tag->uncheckValue(null));
168+
$this->assertNotSame($tag, $tag->uncheckInputAttributes([]));
169+
$this->assertNotSame($tag, $tag->replaceUncheckInputAttributes([]));
150170
$this->assertNotSame($tag, $tag->accept(null));
151171
$this->assertNotSame($tag, $tag->multiple());
152172
}

tests/common/Tag/Input/RangeTest.php

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,47 @@ public function testShowOutput(): void
118118

119119
$this->assertMatchesRegularExpression(
120120
'~<input type="range" ' .
121-
'oninput="document.getElementById\(\&quot;(?<id>rangeOutput\d*)\&quot;\)\.innerHTML=this\.value">' .
121+
'oninput="document\.getElementById\(\&quot;(?<id>rangeOutput\d*)\&quot;\)\.innerHTML=this\.value">' .
122122
"\n" . '<span id="(?P=id)">-</span>~',
123123
$tag->render()
124124
);
125125
}
126126

127+
public function testOutputAttributes(): void
128+
{
129+
$tag = Range::tag()
130+
->showOutput()
131+
->outputAttributes(['class' => 'red'])
132+
->outputAttributes(['id' => 'UID']);
133+
134+
$this->assertSame(
135+
'<input type="range" ' .
136+
'oninput="document.getElementById(&quot;UID&quot;).innerHTML=this.value">' .
137+
"\n" . '<span id="UID" class="red">-</span>',
138+
$tag->render()
139+
);
140+
}
141+
142+
public function testReplaceOutputAttributes(): void
143+
{
144+
$tag = Range::tag()
145+
->showOutput()
146+
->outputAttributes(['class' => 'red'])
147+
->replaceOutputAttributes(['id' => 'UID']);
148+
149+
$this->assertSame(
150+
'<input type="range" ' .
151+
'oninput="document.getElementById(&quot;UID&quot;).innerHTML=this.value">' .
152+
"\n" . '<span id="UID">-</span>',
153+
$tag->render()
154+
);
155+
}
156+
127157
public function testOutputWithCustomId(): void
128158
{
129159
$tag = Range::tag()
130160
->showOutput()
131-
->outputTagAttributes(['id' => 'UID']);
161+
->outputAttributes(['id' => 'UID']);
132162

133163
$this->assertMatchesRegularExpression(
134164
'~<input type="range" ' .
@@ -138,29 +168,29 @@ public function testOutputWithCustomId(): void
138168
);
139169
}
140170

141-
public function testOutputWithCustomTagName(): void
171+
public function testOutputWithCustomTag(): void
142172
{
143173
$tag = Range::tag()
144174
->showOutput()
145-
->outputTagName('b');
175+
->outputTag('b');
146176

147177
$this->assertMatchesRegularExpression(
148178
'~<input type="range" ' .
149-
'oninput="document.getElementById\(\&quot;(?<id>rangeOutput\d*)\&quot;\)\.innerHTML=this\.value">' .
179+
'oninput="document\.getElementById\(\&quot;(?<id>rangeOutput\d*)\&quot;\)\.innerHTML=this\.value">' .
150180
"\n" . '<b id="(?P=id)">-</b>~',
151181
$tag->render()
152182
);
153183
}
154184

155-
public function testOutputWithCustomTagAttributes(): void
185+
public function testOutputWithCustomAttributes(): void
156186
{
157187
$tag = Range::tag()
158188
->showOutput()
159-
->outputTagAttributes(['class' => 'red']);
189+
->outputAttributes(['class' => 'red']);
160190

161191
$this->assertMatchesRegularExpression(
162192
'~<input type="range" ' .
163-
'oninput="document.getElementById\(\&quot;(?<id>rangeOutput\d*)\&quot;\)\.innerHTML=this\.value">' .
193+
'oninput="document\.getElementById\(\&quot;(?<id>rangeOutput\d*)\&quot;\)\.innerHTML=this\.value">' .
164194
"\n" . '<span id="(?P=id)" class="red">-</span>~',
165195
$tag->render()
166196
);
@@ -174,19 +204,19 @@ public function testOutputWithValue(): void
174204

175205
$this->assertMatchesRegularExpression(
176206
'~<input type="range" value="10" ' .
177-
'oninput="document.getElementById\(\&quot;(?<id>rangeOutput\d*)\&quot;\)\.innerHTML=this\.value">' .
207+
'oninput="document\.getElementById\(\&quot;(?<id>rangeOutput\d*)\&quot;\)\.innerHTML=this\.value">' .
178208
"\n" . '<span id="(?P=id)">10</span>~',
179209
$tag->render()
180210
);
181211
}
182212

183-
public function testEmptyOutputTagName(): void
213+
public function testEmptyOutputTag(): void
184214
{
185215
$tag = Range::tag();
186216

187217
$this->expectException(InvalidArgumentException::class);
188218
$this->expectExceptionMessage('The output tag name it cannot be empty value.');
189-
$tag->outputTagName('');
219+
$tag->outputTag('');
190220
}
191221

192222
public function testImmutability(): void
@@ -198,7 +228,8 @@ public function testImmutability(): void
198228
$this->assertNotSame($tag, $tag->step(null));
199229
$this->assertNotSame($tag, $tag->list(null));
200230
$this->assertNotSame($tag, $tag->showOutput());
201-
$this->assertNotSame($tag, $tag->outputTagName('b'));
202-
$this->assertNotSame($tag, $tag->outputTagAttributes([]));
231+
$this->assertNotSame($tag, $tag->outputTag('b'));
232+
$this->assertNotSame($tag, $tag->outputAttributes([]));
233+
$this->assertNotSame($tag, $tag->replaceOutputAttributes([]));
203234
}
204235
}

0 commit comments

Comments
 (0)