select::options()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 13
rs 9.4285
cc 3
eloc 10
nc 3
nop 1
1
<?php
2
3
namespace Code4\Forms\Fields;
4
5
class select extends AbstractField {
6
7
    protected $_view = 'select';
8
    protected $_type = 'select';
9
10
    protected $options;
11
    protected $optionKeys;
12
13
    public function __construct($itemId, $config) {
14
15
        parent::__construct($itemId, $config);
16
17
        if (array_key_exists('options', $config)) {
18
            $this->options = $config['options'];
19
        }
20
21
        if (array_key_exists('option-keys', $config)) {
22
            $this->optionKeys = $config['option-keys'];
23
        }
24
25
        if (is_string($this->value)) {
26
            $this->value = [$this->value];
27
        }
28
    }
29
30
    /**
31
     * Sprawdzanie typu przesłanych opcji selecta
32
     * @param $options
33
     * @return $this|select
34
     */
35
    public function options($options) {
36
37
        switch(gettype($options)) {
38
            case "object":
39
                return $this->setOptionsFromObject($options);
40
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
41
            case "array":
42
                return $this->setOptionsFromArray($options);
43
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
44
            default:
45
                return $this;
46
        }
47
    }
48
49
    /**
50
     * Ustawiamy opcje selecta z pomocą dostarczonej tablicy
51
     * @param $array
52
     * @return $this
53
     */
54
    public function setOptionsFromArray($array) {
55
        $this->options = $array;
56
        return $this;
57
    }
58
59
    /**
60
     * Jeżeli przesłany został objekt (np. model Eloquent) to używając optionKeys wybieramy tylko potrzebne dane
61
     * @param $object
62
     * @return $this
63
     * @throws \Exception
64
     */
65
    public function setOptionsFromObject($object) {
66
67
        if (!is_array($this->optionKeys) || count($this->optionKeys) != 2) {
68
            throw new \Exception('Błędna opcja optionKeys dla elementu '.$this->id);
69
        }
70
71
        $value = $this->optionKeys[0];
72
        $text = $this->optionKeys[1];
73
74
        $options = [];
75
76
        foreach($object as $el) {
77
            $options[$el->$value] = $el->$text;
78
        }
79
80
        $this->options = $options;
81
82
        return $this;
83
    }
84
85
    /**
86
     * Metoda sprawdza czy w zapisanych wartościach znajduje się szukana
87
     * @param $val
88
     * @return string
89
     */
90
    public function getSelected($val) {
91
        if (is_array($this->value)) {
92
            if (in_array($val, $this->value)) {
93
                return "selected";
94
            }
95
        }
96
        return '';
97
    }
98
99
100
    /**
101
     * Ponieważ atrybut ma taką samą nazwę jak funkcja która ustawia opcje zwracamy je do widoku tą funkcją
102
     * @return mixed
103
     */
104
    public function getOptions() {
105
        return $this->options;
106
    }
107
108
}