1
|
|
|
<?php |
2
|
|
|
namespace rtens\domin\delivery\web\fields; |
3
|
|
|
|
4
|
|
|
use rtens\domin\delivery\web\HeadElements; |
5
|
|
|
use rtens\domin\Parameter; |
6
|
|
|
use rtens\domin\delivery\web\Element; |
7
|
|
|
use rtens\domin\delivery\web\WebField; |
8
|
|
|
use watoki\reflect\type\BooleanType; |
9
|
|
|
|
10
|
|
|
class BooleanField implements WebField { |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @param Parameter $parameter |
14
|
|
|
* @return bool |
15
|
|
|
*/ |
16
|
|
|
public function handles(Parameter $parameter) { |
17
|
|
|
return $parameter->getType() == new BooleanType(); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param Parameter $parameter |
22
|
|
|
* @param string $serialized |
23
|
|
|
* @return mixed |
24
|
|
|
*/ |
25
|
|
|
public function inflate(Parameter $parameter, $serialized) { |
26
|
|
|
return !!$serialized; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param Parameter $parameter |
31
|
|
|
* @param mixed $value |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
public function render(Parameter $parameter, $value) { |
35
|
|
|
return implode('', [ |
36
|
|
|
new Element('input', [ |
37
|
|
|
'type' => 'hidden', |
38
|
|
|
'name' => $parameter->getName(), |
39
|
|
|
'value' => 0 |
40
|
|
|
]), |
41
|
|
|
new Element('input', array_merge([ |
42
|
|
|
'class' => 'boolean-switch', |
43
|
|
|
'type' => 'checkbox', |
44
|
|
|
'name' => $parameter->getName(), |
45
|
|
|
'value' => 1, |
46
|
|
|
], $value ? [ |
47
|
|
|
'checked' => 'checked' |
48
|
|
|
] : [])) |
49
|
|
|
]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param Parameter $parameter |
54
|
|
|
* @return array|Element[] |
55
|
|
|
*/ |
56
|
|
View Code Duplication |
public function headElements(Parameter $parameter) { |
|
|
|
|
57
|
|
|
return [ |
58
|
|
|
HeadElements::bootstrap(), |
59
|
|
|
HeadElements::jquery(), |
60
|
|
|
HeadElements::style('//cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.2/css/bootstrap3/bootstrap-switch.min.css'), |
61
|
|
|
HeadElements::script('//cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.2/js/bootstrap-switch.min.js'), |
62
|
|
|
new Element('script', [], [" |
63
|
|
|
$(function () { |
64
|
|
|
$('.boolean-switch').bootstrapSwitch({ |
65
|
|
|
size: 'small', |
66
|
|
|
onColor: 'success', |
67
|
|
|
onText: 'Yes', |
68
|
|
|
offText: 'No' |
69
|
|
|
}); |
70
|
|
|
});"]) |
71
|
|
|
]; |
72
|
|
|
} |
73
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.