Completed
Branch master (555f37)
by Alfred
01:38
created

Datalist   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 14
c 1
b 0
f 0
dl 0
loc 26
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A RenderHtml() 0 11 3
A __construct() 0 6 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Iriven France
5
 * Date: 08/05/2018
6
 * Time: 20:37
7
 */
8
9
namespace Iriven\Plugins\Form\Elements;
10
use \Iriven\Plugins\Form\Core\FormElement;
11
12
class Datalist extends FormElement
13
{
14
    private $options;
15
    private $listname;
16
    public function __construct($label, array $options = [], $attributes)
17
    {
18
        parent::__construct($label, $attributes);
19
        $this->listname = 'datalist-' . $this->Attributes()->get('id');
20
        $this->Attributes()->set('list', $this->listname);
21
        $this->options = array_values($options);
22
    }
23
24
    /**
25
     * @return string
26
     */
27
    public function RenderHtml(): string
28
    {
29
        $html = parent::RenderHtml();
30
        $html .= '<datalist id="'. $this->listname.'">';
31
        foreach( $this->options as $option)
32
        {
33
            if(!$option = trim(strip_tags($option))) continue;
34
            $html .= '<option value="'.$option.'">';
35
        }
36
        $html .= '</datalist>';
37
        return $html;
38
    }
39
40
}
41