Passed
Pull Request — 2.x (#1360)
by Harings
11:27
created

FieldWithOptions   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 42
c 1
b 0
f 0
dl 0
loc 73
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getOptions() 0 10 3
A __construct() 0 37 1
A inModal() 0 2 1
1
<?php
2
3
namespace A17\Twill\View\Components;
4
5
abstract class FieldWithOptions extends TwillFormComponent
6
{
7
    public $options;
8
    public $unpack;
9
    public $columns;
10
    public $searchable;
11
    public $note;
12
    public $placeholder;
13
    public $disabled;
14
    public $addNew;
15
    public $moduleName;
16
    public $storeUrl;
17
    public $required;
18
    public $fieldsInModal;
19
    public $default;
20
    /** Below are unused but needed to keep compatible  */
21
    public $confirmMessageText;
22
    public $confirmTitleText;
23
    public $requireConfirmation;
24
25
    public function __construct(
26
        $name,
27
        $label,
28
        $options = [],
29
        $unpack = false,
30
        $columns = 0,
31
        $searchable = false,
32
        $note = null,
33
        $placeholder = null,
34
        $disabled = false,
35
        $addNew = false,
36
        $moduleName = null,
37
        $storeUrl = null,
38
        $default = false,
39
        $fieldsInModal = null
40
    ) {
41
        parent::__construct(
42
            $name,
43
            $label
44
        );
45
46
        $this->options = $options;
47
        $this->unpack = $unpack;
48
        $this->columns = $columns;
49
        $this->searchable = $searchable;
50
        $this->note = $note;
51
        $this->placeholder = $placeholder;
52
        $this->disabled = $disabled;
53
        $this->addNew = $addNew;
54
        $this->default = $default;
55
        $this->moduleName = $moduleName;
56
        $this->storeUrl = $storeUrl;
57
        $this->fieldsInModal = $fieldsInModal;
58
59
        $this->confirmMessageText = null;
60
        $this->confirmTitleText = null;
61
        $this->requireConfirmation = null;
62
    }
63
64
    public function inModal(): bool {
65
        return $this->fieldsInModal ?? false;
66
    }
67
68
    public function getOptions(): array
69
    {
70
        return is_object($this->options) && method_exists($this->options, 'map') ? $this->options->map(
71
            function ($label, $value) {
72
                return [
73
                    'value' => $value,
74
                    'label' => $label,
75
                ];
76
            }
77
        )->values()->toArray() : $this->options;
78
    }
79
}
80