|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace mindplay\kissform\Fields; |
|
4
|
|
|
|
|
5
|
|
|
use mindplay\kissform\Field; |
|
6
|
|
|
use mindplay\kissform\InputModel; |
|
7
|
|
|
use mindplay\kissform\InputRenderer; |
|
8
|
|
|
use mindplay\kissform\Validators\CheckAccept; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* This class represents a labeled checkbox structure, e.g. `div.checkbox` with an `input` and |
|
12
|
|
|
* matching `label` tag inside. |
|
13
|
|
|
* |
|
14
|
|
|
* Note that the markup deviates from Bootstrap standard markup, which isn't useful for styling. |
|
15
|
|
|
* |
|
16
|
|
|
* @link https://github.com/twbs/bootstrap/issues/19931 |
|
17
|
|
|
* @link https://github.com/flatlogic/awesome-bootstrap-checkbox |
|
18
|
|
|
*/ |
|
19
|
|
|
class CheckboxField extends Field |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
public $checked_value = '1'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string overrides the default checkbox label (provided by Field::$label) |
|
28
|
|
|
*/ |
|
29
|
|
|
public $label; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string|null wrapper class-name (or NULL to disable the wrapper div; defaults to "checkbox") |
|
33
|
|
|
*/ |
|
34
|
|
|
public $wrapper_class = 'checkbox'; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* {@inheritdoc} |
|
38
|
|
|
*/ |
|
39
|
1 |
|
public function renderInput(InputRenderer $renderer, InputModel $model, array $attr) |
|
40
|
|
|
{ |
|
41
|
1 |
|
$label = $renderer->getLabel($this); |
|
42
|
|
|
|
|
43
|
1 |
|
$id = $renderer->getId($this); |
|
44
|
|
|
|
|
45
|
1 |
|
$input = $renderer->tag( |
|
46
|
1 |
|
'input', |
|
47
|
|
|
$attr + [ |
|
48
|
1 |
|
'name' => $renderer->getName($this), |
|
49
|
1 |
|
'id' => $id, |
|
50
|
1 |
|
'value' => $this->checked_value, |
|
51
|
1 |
|
'checked' => $model->getInput($this) === $this->checked_value, |
|
52
|
1 |
|
'type' => 'checkbox', |
|
53
|
|
|
] |
|
54
|
|
|
); |
|
55
|
|
|
|
|
56
|
|
|
return |
|
57
|
1 |
|
($this->wrapper_class ? '<div class="' . $this->wrapper_class . '">' : '') |
|
58
|
1 |
|
. $input |
|
59
|
1 |
|
. ($label ? $renderer->tag('label', ['for' => $id], $renderer->softEscape($label)) : '') |
|
60
|
1 |
|
. ($this->wrapper_class ? '</div>' : ''); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param InputModel $model |
|
65
|
|
|
* |
|
66
|
|
|
* @return bool |
|
67
|
|
|
* |
|
68
|
|
|
* @SuppressWarnings(PHPMD.BooleanGetMethodName) |
|
69
|
|
|
*/ |
|
70
|
1 |
|
public function getValue(InputModel $model) |
|
71
|
|
|
{ |
|
72
|
1 |
|
return $model->getInput($this) === $this->checked_value; |
|
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param InputModel $model |
|
77
|
|
|
* @param bool $value |
|
78
|
|
|
* |
|
79
|
|
|
* @return void |
|
80
|
|
|
*/ |
|
81
|
1 |
|
public function setValue(InputModel $model, $value) |
|
82
|
|
|
{ |
|
83
|
1 |
|
$model->setInput($this, $value ? $this->checked_value : null); |
|
84
|
1 |
|
} |
|
85
|
|
|
|
|
86
|
1 |
|
public function createValidators() |
|
87
|
|
|
{ |
|
88
|
1 |
|
return [new CheckAccept($this->checked_value)]; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.