Completed
Push — master ( 49ef2c...6cae08 )
by Rasmus
06:22
created

CheckboxField::renderInput()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 23
ccs 14
cts 14
cp 1
rs 8.7972
cc 4
eloc 16
nc 8
nop 3
crap 4
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;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $model->getInput(...= $this->checked_value; (boolean) is incompatible with the return type declared by the interface mindplay\kissform\Facets\FieldInterface::getValue of type string|null.

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:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

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