|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Field; |
|
6
|
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use Stringable; |
|
9
|
|
|
use Yiisoft\Form\Field\Base\AbstractInputField; |
|
10
|
|
|
use Yiisoft\Html\Html; |
|
11
|
|
|
|
|
12
|
|
|
use function is_string; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* An imprecise control for setting the element’s value to a string representing a number. |
|
16
|
|
|
* |
|
17
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#range-state-(type=range) |
|
18
|
|
|
*/ |
|
19
|
|
|
final class Range extends AbstractInputField |
|
20
|
|
|
{ |
|
21
|
|
|
private bool $showOutput = false; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @psalm-var non-empty-string |
|
25
|
|
|
*/ |
|
26
|
|
|
private string $outputTagName = 'span'; |
|
27
|
|
|
private array $outputTagAttributes = []; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Maximum value. |
|
31
|
|
|
* |
|
32
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#attr-input-max |
|
33
|
|
|
*/ |
|
34
|
2 |
|
public function max(float|int|string|Stringable|null $value): self |
|
35
|
|
|
{ |
|
36
|
2 |
|
$new = clone $this; |
|
37
|
2 |
|
$new->inputTagAttributes['max'] = $value; |
|
38
|
2 |
|
return $new; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Minimum value. |
|
43
|
|
|
* |
|
44
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#attr-input-min |
|
45
|
|
|
*/ |
|
46
|
2 |
|
public function min(float|int|string|Stringable|null $value): self |
|
47
|
|
|
{ |
|
48
|
2 |
|
$new = clone $this; |
|
49
|
2 |
|
$new->inputTagAttributes['min'] = $value; |
|
50
|
2 |
|
return $new; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Granularity to be matched by the form control's value. |
|
55
|
|
|
* |
|
56
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#attr-input-step |
|
57
|
|
|
*/ |
|
58
|
|
|
public function step(float|int|string|Stringable|null $value): self |
|
59
|
|
|
{ |
|
60
|
|
|
$new = clone $this; |
|
61
|
|
|
$new->inputTagAttributes['step'] = $value; |
|
62
|
|
|
return $new; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* ID of element that lists predefined options suggested to the user. |
|
67
|
|
|
* |
|
68
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#the-list-attribute |
|
69
|
|
|
*/ |
|
70
|
|
|
public function list(?string $id): self |
|
71
|
|
|
{ |
|
72
|
|
|
$new = clone $this; |
|
73
|
|
|
$new->inputTagAttributes['list'] = $id; |
|
74
|
|
|
return $new; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
public function showOutput(bool $show = true): self |
|
78
|
|
|
{ |
|
79
|
1 |
|
$new = clone $this; |
|
80
|
1 |
|
$new->showOutput = $show; |
|
81
|
1 |
|
return $new; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function outputTagName(string $tagName): self |
|
85
|
|
|
{ |
|
86
|
|
|
if ($tagName === '') { |
|
87
|
|
|
throw new InvalidArgumentException('The output tag name it cannot be empty value.'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$new = clone $this; |
|
91
|
|
|
$new->outputTagName = $tagName; |
|
92
|
|
|
return $new; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
1 |
|
public function outputTagAttributes(array $attributes): self |
|
96
|
|
|
{ |
|
97
|
1 |
|
$new = clone $this; |
|
98
|
1 |
|
$new->outputTagAttributes = $attributes; |
|
99
|
1 |
|
return $new; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
2 |
|
protected function generateInput(): string |
|
103
|
|
|
{ |
|
104
|
2 |
|
$value = $this->getAttributeValue(); |
|
105
|
|
|
|
|
106
|
2 |
|
if (!is_string($value) && !is_numeric($value) && $value !== null) { |
|
107
|
|
|
throw new InvalidArgumentException('Range widget requires a string, numeric or null value.'); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
2 |
|
$tag = Html::range($this->getInputName(), $value, $this->getInputTagAttributes()); |
|
111
|
2 |
|
if ($this->showOutput) { |
|
112
|
1 |
|
$tag = $tag |
|
113
|
1 |
|
->showOutput() |
|
114
|
1 |
|
->outputTagName($this->outputTagName) |
|
115
|
1 |
|
->outputTagAttributes($this->outputTagAttributes); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
2 |
|
return $tag->render(); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|