Select   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 60
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B create() 0 50 11
1
<?php
2
3
namespace Faulancer\Form\Type\Base;
4
5
use Faulancer\Form\Type\AbstractType;
6
7
/**
8
 * Class Select
9
 *
10
 * @package Faulancer\Form\Type\Base
11
 * @author Florian Knapp <[email protected]>
12
 */
13
class Select extends AbstractType
14
{
15
16
    protected $inputType = 'select';
17
18
    /**
19
     * @return self
20
     */
21
    public function create()
22
    {
23
        parent::create();
24
25
        $output = '<' . $this->inputType;
26
27
        foreach ($this->definition['attributes'] as $attr => $value) {
28
29
            if ($attr === 'type') {
30
                continue;
31
            }
32
33
            $output .= ' ' . $attr . '="' . $value . '"';
34
35
        }
36
37
        $output .= '>';
38
39
        $definition = $this->definition;
40
        $isSelected = function($val) use (&$definition) {
41
42
            if ($this->isPost() && empty($val)) {
43
                unset($definition['selected']);
44
            }
45
46
            if (!empty($this->getValue()) && $val === $this->getValue()) {
47
                return true;
48
            } elseif (!empty($definition['selected']) && $val === $definition['selected']) {
49
                return true;
50
            }
51
52
            return false;
53
54
        };
55
56
        foreach ($definition['options'] as $val => $text) {
57
58
            $selected = $isSelected($val) === true ? ' selected="selected"' : '';
59
            $option   = '<option value="' . $val .'"%s>' . $text . '</option>';
60
61
            $output .= sprintf($option, $selected);
62
63
        }
64
65
        $output .= '</select>';
66
67
        $this->element = $output;
68
69
        return $this;
70
    }
71
72
}