Dropdown::renderWith()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 17
c 0
b 0
f 0
rs 9.4285
ccs 10
cts 10
cp 1
cc 2
eloc 10
nc 2
nop 0
crap 2
1
<?php 
2
3
namespace Helmut\Forms\Fields\Dropdown;
4
5
use Helmut\Forms\Field;
6
use Helmut\Forms\Utility\Validate;
7
8
class Dropdown extends Field {
9
10
    protected $options = [];
11
    protected $value = '';
12
13 7
    public function setOptions($options)
14
    {
15 7
        $this->options = $options;
16 7
        return $this;
17
    }
18
19 5
    public function getValue() 
20
    {
21 5
        return $this->value;
22
    }
23
24 7
    public function getButtonName()
25
    {
26 7
        //
27
    }  
28
29 3
    public function renderWith() 
30
    {
31 3
        $properties = [
32 3
            'value' => $this->value, 
33 3
            'options' => [],
34
        ];
35 3
36
        foreach ($this->options as $value => $label) {
37
            $properties['options'][] = [
38 7
                'value' => $value, 
39
                'label' => $label, 
40 7
                'selected' => $this->value == $value
41 7
            ];
42
        }
43 1
44
        return $properties;
45 1
    }
46 1
47
    public function setValueFromDefault()
48
    {
49
        $this->value = $this->default;
50
    }
51
52
    public function setValueFromModel($model)
53 1
    {
54
        if (isset($model->{$this->name})) $this->value = $model->{$this->name};
55 1
    }   
56 1
57
    public function setValueFromRequest($request)
58 1
    {
59
        $this->value = $request->get($this->name);
60 1
    }
61
62
    public function fillModelWithValue($model)
63
    {
64
        $model->{$this->name} = $this->value;
65
    }   
66
    
67
    public function validateRequired()
68
    {
69
        return Validate::required($this->value);
70
    }
71
72
}
73