Completed
Push — master ( 3c5c0f...ec5ff6 )
by Laurent
01:40
created

BaseInput   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 163
rs 10
c 0
b 0
f 0
wmc 17
lcom 2
cbo 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
A getType() 0 4 1
A getOptions() 0 4 1
A getName() 0 4 1
A getId() 0 4 2
A getValue() 0 4 1
A setValue() 0 4 1
A setAttribute() 0 6 1
A required() 0 6 1
A disable() 0 5 1
A isDisabled() 0 4 1
A getOption() 0 8 2
A setErrors() 0 4 1
A hasError() 0 4 1
1
<?php
2
/**
3
 *
4
 */
5
6
namespace flightlog\form;
7
8
/**
9
 * @author Laurent De Coninck <[email protected]>
10
 */
11
abstract class BaseInput implements FormElementInterface
12
{
13
    /**
14
     * @var string
15
     */
16
    private $name;
17
18
    /**
19
     * @var array
20
     */
21
    private $options;
22
23
    /**
24
     * @var string|int
25
     */
26
    private $value;
27
28
    /**
29
     * @var string
30
     */
31
    private $type;
32
33
    /**
34
     * @var string[]|array
35
     */
36
    private $errors;
37
38
    /**
39
     * @param string $name
40
     * @param string $type
41
     * @param array  $options
42
     */
43
    public function __construct($name, $type, array $options = [])
44
    {
45
        if (empty($name)) {
46
            throw new \InvalidArgumentException('Name cannot be empty');
47
        }
48
49
        $this->name = $name;
50
        $this->options = $options;
51
        $this->type = $type;
52
        $this->errors = [];
53
    }
54
55
    /**
56
     * @inheritDoc
57
     */
58
    public function getType()
59
    {
60
        return $this->type;
61
    }
62
63
    /**
64
     * @inheritDoc
65
     */
66
    public function getOptions()
67
    {
68
        return $this->options;
69
    }
70
71
    /**
72
     * @inheritDoc
73
     */
74
    public function getName()
75
    {
76
        return $this->name;
77
    }
78
79
    /**
80
     * @inheritdoc
81
     */
82
    public function getId()
83
    {
84
        return $this->getOption('id') ?: $this->getName();
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->getOption('id') ?: $this->getName(); of type string|integer|boolean adds the type boolean to the return on line 84 which is incompatible with the return type declared by the interface flightlog\form\FormElementInterface::getId of type string.
Loading history...
85
    }
86
87
    /**
88
     * @inheritDoc
89
     */
90
    public function getValue()
91
    {
92
        return $this->value;
93
    }
94
95
    /**
96
     * @inheritDoc
97
     */
98
    public function setValue($value)
99
    {
100
        $this->value = $value;
101
    }
102
103
    /**
104
     * @param string     $name
105
     * @param int|string $value
106
     *
107
     * @return $this
108
     */
109
    protected function setAttribute($name, $value)
110
    {
111
        $this->options['attr'][$name] = $value;
112
113
        return $this;
114
    }
115
116
    /**
117
     * @return $this
118
     */
119
    public function required()
120
    {
121
        $this->options['attr']['required'] = 'required';
122
123
        return $this;
124
    }
125
126
    /**
127
     * @return $this
128
     */
129
    public function disable()
130
    {
131
        $this->options['attr']['disabled'] = 'disabled';
132
        return $this;
133
    }
134
135
    /**
136
     * @inheritdoc
137
     */
138
    public function isDisabled()
139
    {
140
        return isset($this->options['attr']['disabled']);
141
    }
142
143
    /**
144
     * @param string $option
145
     *
146
     * @return string|int|boolean|null
147
     */
148
    public function getOption($option)
149
    {
150
        if (!isset($this->options[$option])) {
151
            return null;
152
        }
153
154
        return $this->options[$option];
155
    }
156
157
    /**
158
     * @inheritDoc
159
     */
160
    public function setErrors($errors = [])
161
    {
162
        $this->errors = $errors;
163
    }
164
165
    /**
166
     * @inheritDoc
167
     */
168
    public function hasError()
169
    {
170
        return !empty($this->errors);
171
    }
172
173
}