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

array2element   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
dl 0
loc 23
rs 10
c 1
b 0
f 0
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A select() 0 21 5
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