Completed
Branch master (856cc5)
by Mihail
07:21 queued 04:29
created

CheckboxField::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
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
}