Completed
Push — master ( 9394c7...856cc5 )
by Mihail
06:05
created

CheckboxField   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 5
c 1
b 1
f 0
lcom 1
cbo 1
dl 0
loc 46
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A make() 0 21 4
1
<?php
2
namespace Ffcms\Core\Helper\HTML\Form;
3
4
use Ffcms\Core\Helper\HTML\System\NativeGenerator;
5
6
class CheckboxField extends NativeGenerator implements iField
7
{
8
    private $properties;
9
    private $name;
10
    private $value;
11
    
12
    /**
13
     * CheckboxField constructor. Pass params inside model.
14
     * @param array $properties
15
     * @param string $name
16
     * @param string|null $value
17
     */
18
    public function __construct($properties, $name, $value = null)
19
    {
20
        $this->properties = $properties;
21
        $this->name = $name;
22
        $this->value = $value;
23
    }
24
25
    /**
26
     * Build <input type="checkbox" checked {$properties} /> response
27
     * {@inheritDoc}
28
     * @see \Ffcms\Core\Helper\HTML\Form\iField::make()
29
     */
30
    public function make()
31
    {
32
        // hook DOM model: build "hidden" field with value="0" for this box
33
        $response = self::buildSingleTag('input', [
34
            'type' => 'hidden',
35
            'value' => '0',
36
            'name' => $this->properties['name']
37
        ]);
38
        
39
        // set field type
40
        $this->properties['type'] = 'checkbox';
41
        if ($this->value === 1 || $this->value === true || $this->value === '1') {
42
            $this->properties['checked'] = null; // set checked if active
43
        }
44
        
45
        unset($this->properties['required']);
46
        // this item always have "1" value (0 is hidden and active when this is :not(checked)
47
        $this->properties['value'] = '1';
48
        $response .= self::buildSingleTag('input', $this->properties);
49
        return $response;
50
    }
51
}