Completed
Push — feature/update_flight ( 01d55d...a5f5fd )
by Laurent
01:55
created

BaseInput::hasError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
        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 83 which is incompatible with the return type declared by the interface flightlog\form\FormElementInterface::getId of type string.
Loading history...
84
    }
85
86
    /**
87
     * @inheritDoc
88
     */
89
    public function getValue()
90
    {
91
        return $this->value;
92
    }
93
94
    /**
95
     * @inheritDoc
96
     */
97
    public function setValue($value)
98
    {
99
        $this->value = $value;
100
    }
101
102
    /**
103
     * @param string     $name
104
     * @param int|string $value
105
     *
106
     * @return $this
107
     */
108
    protected function setAttribute($name, $value)
109
    {
110
        $this->options['attr'][$name] = $value;
111
112
        return $this;
113
    }
114
115
    /**
116
     * @return $this
117
     */
118
    public function required(){
119
        $this->options['attr']['required'] = 'required';
120
121
        return $this;
122
    }
123
124
    /**
125
     * @return $this
126
     */
127
    public function disable(){
128
        $this->options['attr']['disabled'] = 'disabled';
129
        return $this;
130
    }
131
132
    /**
133
     * @inheritdoc
134
     */
135
    public function isDisabled(){
136
        return isset($this->options['attr']['disabled']);
137
    }
138
139
    /**
140
     * @param string $option
141
     *
142
     * @return string|int|boolean|null
143
     */
144
    public function getOption($option){
145
        if(!isset($this->options[$option])){
146
            return null;
147
        }
148
149
        return $this->options[$option];
150
    }
151
152
    /**
153
     * @inheritDoc
154
     */
155
    public function setErrors($errors = [])
156
    {
157
        $this->errors = $errors;
158
    }
159
160
    /**
161
     * @inheritDoc
162
     */
163
    public function hasError()
164
    {
165
        return !empty($this->errors);
166
    }
167
168
}