Completed
Push — master ( 59cd4c...8588a0 )
by Dimas
10:00
created

array2element::select()   A

Complexity

Conditions 5
Paths 12

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 14
nc 12
nop 3
dl 0
loc 21
rs 9.4888
c 1
b 0
f 0
1
<?php
2
3
namespace HTML;
4
5
use SmartDOMDocument;
6
7
class array2element
8
{
9
  public function select(array $data, array $attributes = [], array $options = [])
10
  {
11
    $dom = new SmartDOMDocument();
12
    if (!isset($options['selected'])) {
13
      $options['selected'] = null;
14
    }
15
    $select = $dom->createElement('select');
16
    foreach ($attributes as $key => $val) {
17
      $select->setAttribute($key, $val);
18
    }
19
    foreach ($data as $opt) {
20
      $option = $dom->createElement('option', $opt);
21
      $option->setAttribute('value', $opt);
22
      if ($options['selected'] == $opt) {
23
        $option->setAttribute('selected', 'selected');
24
      }
25
      $select->appendChild($option);
26
    }
27
    $dom->appendChild($select);
28
29
    return $dom->saveHTML();
30
  }
31
}
32