Completed
Push — master ( ab5ce3...f55930 )
by Helmut
02:51
created

Dropdown::getProperties()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 5
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
    public $options = [];
11
    
12
    public $value = '';
13 7
14
    public function setOptions($options)
15 7
    {
16 7
        $this->options = $options;
17
        return $this;
18
    }
19 5
20
    public function getValue() 
21 5
    {
22
        return $this->value;
23
    }
24 7
25
    public function getButtonName()
26 7
    {
27
        //
28
    }  
29 3
30
    public function renderWith() 
31 3
    {
32 3
        $properties = [
33 3
            'value' => $this->value, 
34
            'options' => [],
35 3
        ];
36
37
        foreach ($this->options as $value => $label) {
38 7
            $properties['options'][] = [
39
                'value' => $value, 
40 7
                'label' => $label, 
41 7
                'selected' => $this->value == $value
42
            ];
43 1
        }
44
45 1
        return $properties;
46 1
    }
47
48
    public function setValueFromDefault()
49
    {
50
        $this->value = $this->default;
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->default of type array is incompatible with the declared type string of property $value.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
51
    }
52
53 1
    public function setValueFromModel($model)
54
    {
55 1
        if (property_exists($model, $this->name)) $this->value = $model->{$this->name};
56 1
    }   
57
58 1
    public function setValueFromRequest($request)
59
    {
60 1
        $this->value = $request->get($this->name);
61
    }
62
63
    public function fillModelWithValue($model)
64
    {
65
        if (property_exists($model, $this->name)) $model->{$this->name} = $this->value;
66
    }   
67
68
    public function validateRequired()
69
    {
70
        return Validate::required($this->value);
71
    }
72
73
}
74