|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bdf\Form\Leaf\View; |
|
4
|
|
|
|
|
5
|
|
|
use Bdf\Form\Leaf\BooleanElement; |
|
6
|
|
|
use Bdf\Form\View\ElementViewTrait; |
|
7
|
|
|
use Bdf\Form\View\FieldViewInterface; |
|
8
|
|
|
use Bdf\Form\View\FieldViewRendererInterface; |
|
9
|
|
|
use Bdf\Form\View\FieldViewTrait; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Element view for boolean / checkbox |
|
13
|
|
|
* |
|
14
|
|
|
* @see BooleanElement::view() |
|
15
|
|
|
*/ |
|
16
|
|
|
final class BooleanElementView implements FieldViewInterface |
|
17
|
|
|
{ |
|
18
|
|
|
use ElementViewTrait; |
|
19
|
|
|
use FieldViewTrait; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
private $httpValue; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var bool |
|
28
|
|
|
*/ |
|
29
|
|
|
private $checked; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* BooleanElementView constructor. |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $type |
|
35
|
|
|
* @param string $name |
|
36
|
|
|
* @param mixed $value |
|
37
|
|
|
* @param string $httpValue |
|
38
|
|
|
* @param bool $checked |
|
39
|
|
|
* @param string|null $error |
|
40
|
|
|
*/ |
|
41
|
12 |
|
public function __construct(string $type, string $name, $value, string $httpValue, bool $checked, ?string $error) |
|
42
|
|
|
{ |
|
43
|
12 |
|
$this->type = $type; |
|
44
|
12 |
|
$this->name = $name; |
|
45
|
12 |
|
$this->value = $value; |
|
46
|
12 |
|
$this->httpValue = $httpValue; |
|
47
|
12 |
|
$this->checked = $checked; |
|
48
|
12 |
|
$this->error = $error; |
|
49
|
12 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Get the HTTP value which is used as "true" for the form element |
|
53
|
|
|
* Unlike `FieldViewInterface::value()`, this method will always return the value even if the element is not submitted (i.e. false) |
|
54
|
|
|
* |
|
55
|
|
|
* @return string |
|
56
|
|
|
*/ |
|
57
|
9 |
|
public function httpValue(): string |
|
58
|
|
|
{ |
|
59
|
9 |
|
return $this->httpValue; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Does the current element is checked or submitted (i.e. it's value is true) |
|
64
|
|
|
* Use this method instead of value() for render a checkbox |
|
65
|
|
|
* |
|
66
|
|
|
* @return bool |
|
67
|
|
|
*/ |
|
68
|
9 |
|
public function checked(): bool |
|
69
|
|
|
{ |
|
70
|
9 |
|
return $this->checked; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* {@inheritdoc} |
|
75
|
|
|
*/ |
|
76
|
4 |
|
protected function defaultRenderer(): FieldViewRendererInterface |
|
77
|
|
|
{ |
|
78
|
4 |
|
return CheckboxHtmlRenderer::instance(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Ignore property "attributes" |
|
83
|
|
|
* |
|
84
|
|
|
* @return array |
|
85
|
|
|
*/ |
|
86
|
1 |
|
public function __sleep(): array |
|
87
|
|
|
{ |
|
88
|
1 |
|
return ['type', 'name', 'value', 'httpValue', 'checked', 'error']; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|