Completed
Push — master ( abaac9...0583a5 )
by Mihail
04:25
created

SelectField::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
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
use Ffcms\Core\Exception\SyntaxException;
6
use Ffcms\Core\Helper\Type\Obj;
7
8
class SelectField extends NativeGenerator implements iField
9
{
10
    private $properties;
11
    private $name;
12
    private $value;
13
    
14
    /**
15
     * SelectField constructor. Pass arguments inside model
16
     * @param array $properties
17
     * @param string $name
18
     * @param string|null $value
19
     */
20
    public function __construct($properties, $name, $value = null)
21
    {
22
        $this->properties = $properties;
23
        $this->name = $name;
24
        $this->value = $value;
25
    }
26
27
    /**
28
     * Build <select {$properties} >[<options>]</select> response
29
     * {@inheritDoc}
30
     * @see \Ffcms\Core\Helper\HTML\Form\iField::make()
31
     */
32
    public function make()
33
    {
34
        // get options from properties
35
        $options = $this->properties['options'];
36
        unset($this->properties['options']);
37 View Code Duplication
        if (!Obj::isIterable($options)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
38
            throw new SyntaxException('Select field ' . self::nohtml($this->name) . ' have no iterable options');
39
        }
40
        // value is not used there
41
        unset($this->properties['value']);
42
        // options is defined as key->value array?
43
        $optionsKey = $this->properties['optionsKey'] === true;
44
        unset($this->properties['optionsKey']);
45
        $buildOpt = null;
46
        foreach ($options as $optIdx => $opt) {
47
            $optionProperty = [];
48 View Code Duplication
            if ($optionsKey === true) { // options with value => text
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
49
                $optionProperty['value'] = $optIdx;
50
                if ($optIdx == $this->value) {
51
                    $optionProperty['selected'] = null; // def boolean attribute html5
52
                }
53
            } else { // only value option
54
                if ($opt == $this->value) {
55
                    $optionProperty['selected'] = null; // def boolean attribute html5
56
                }
57
            }
58
            $buildOpt .= self::buildContainerTag('option', $optionProperty, $opt);
59
        }
60
        
61
        // return compiled DOM
62
        return self::buildContainerTag('select', $this->properties, $buildOpt, true);
63
    }
64
}