BooleanField::handles()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
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) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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
}