Completed
Push — master ( cb8392...3a1609 )
by Dimas
09:53
created

array2element::select()   B

Complexity

Conditions 7
Paths 24

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 18
c 1
b 0
f 0
nc 24
nop 3
dl 0
loc 28
rs 8.8333
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
      if (is_callable($val)) {
18
        if (isset($options[$val])) { //arguments
19
          $select->setAttribute($key, call_user_func($val, $options[$val]));
20
          continue; //break here
21
        }
22
      }
23
      // otherwise call default actions
24
      $select->setAttribute($key, $val);
25
    }
26
    foreach ($data as $opt) {
27
      $option = $dom->createElement('option', $opt);
28
      $option->setAttribute('value', $opt);
29
      if ($options['selected'] == $opt) {
30
        $option->setAttribute('selected', 'selected');
31
      }
32
      $select->appendChild($option);
33
    }
34
    $dom->appendChild($select);
35
36
    return $dom->saveHTML();
37
  }
38
}
39