Passed
Branch main (e68017)
by James Ekow Abaka
10:11
created

Checkable::render()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 11
rs 10
c 1
b 0
f 0
1
<?php
2
namespace ntentan\honam\engines\php\helpers\form;
3
4
/**
5
 * A regular checkbox with a label.
6
 */
7
class Checkable extends Field
8
{
9
    /**
10
     * The value that this field should contain if this checkbox is checked.
11
     */
12
    protected $checkedValue;
13
14
    /**
15
     * Constructor for the checkbox.
16
     *
17
     * @param string $label The label of the checkbox.
18
     * @param string $name The name of the checkbox used for the name='' attribute of the HTML output
19
     * @param string $value A value to assign to this checkbox.
20
     * @param string $description A description of the field.
21
     */
22
    public function __construct(string $label="", string $name="", string $value="")
23
    {
24
        Element::__construct($label);
25
        parent::__construct($name);
26
        $this->setCheckedValue($value);
27
    }
28
29
    /**
30
     * Sets the value that should be assigned as the checked value for
31
     * this check box.
32
     * @param string $checkedValue
33
     * @return CheckableField
0 ignored issues
show
Bug introduced by
The type ntentan\honam\engines\ph...ers\form\CheckableField was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
34
     */
35
    public function setCheckedValue($checkedValue)
36
    {
37
        $this->checkedValue = $checkedValue;
38
        return $this;
39
    }
40
41
    /**
42
     * Gets and returns the checkedValue for the check box.
43
     * @return string
44
     */
45
    public function getCheckedValue()
46
    {
47
        return $this->checkedValue;
48
    }
49
50
    public function render()
51
    {
52
        if($this->getCheckedValue() == (string)$this->getValue())
53
        {
54
            $this->setAttribute('checked', 'checked');
55
        }
56
        
57
        $this->setAttribute("name", $this->getName());
58
        //$this->setAttribute("class", "{$this->getAttribute('type')} {$this->getCSSClasses()}");        
59
        $this->setValue($this->getCheckedValue());
60
        return $this->templateRenderer->render('input_checkable_element.tpl.php', array('element' => $this));
61
    }
62
}
63