Select::html()   B
last analyzed

Complexity

Conditions 7
Paths 64

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 11
nc 64
nop 0
dl 0
loc 16
rs 8.8333
c 0
b 0
f 0
1
<?php
2
namespace Mezon\Gui\Field;
3
4
use Mezon\Gui\Field;
5
6
/**
7
 * Class Select
8
 *
9
 * @package Field
10
 * @subpackage Select
11
 * @author Dodonov A.A.
12
 * @version v.1.0 (2019/09/04)
13
 * @copyright Copyright (c) 2019, http://aeon.su
14
 */
15
16
/**
17
 * Text area control
18
 */
19
class Select extends Field
20
{
21
22
    /**
23
     * Control items
24
     *
25
     * @var array
26
     */
27
    protected $items = [];
28
29
    /**
30
     * Constructor
31
     *
32
     * @param array $fieldDescription
33
     *            Field description
34
     * @param string $value
35
     *            Field value
36
     */
37
    public function __construct(array $fieldDescription, string $value = '')
38
    {
39
        $fieldDescription['type'] = isset($fieldDescription['type']) ? $fieldDescription['type'] : 'integer';
40
41
        parent::__construct($fieldDescription, $value);
42
43
        $itemsSource = $fieldDescription['items'];
44
45
        if (is_string($itemsSource) && function_exists($itemsSource)) {
46
            // callback function forms a list of items
47
            $this->items = $itemsSource();
48
        } else {
49
            $this->items = $itemsSource;
50
        }
51
    }
52
53
    /**
54
     * Generating textarea field
55
     *
56
     * @return string HTML representation of the textarea field
57
     */
58
    public function html(): string
59
    {
60
        $content = '<select class="' . $this->class . '"';
61
        $content .= $this->required ? ' required="required"' : '';
62
        $content .= ' type="text" name="' . $this->getNamePrefix() . $this->name .
63
            ($this->batch ? '[{_creation_form_items_counter}]' : '') . '" ';
64
        $content .= $this->disabled ? ' disabled ' : '';
65
        $content .= $this->toggler === '' ? '' : 'toggler="' . $this->toggler . '" ';
66
        $content .= $this->toggler === '' ? '' : 'toggle-value="' . $this->toggleValue . '" ';
67
        $content .= 'value="' . $this->value . '">';
68
69
        foreach ($this->items as $id => $title) {
70
            $content .= '<option value="' . $id . '">' . $title . '</option>';
71
        }
72
73
        return $content . '</select>';
74
    }
75
}
76