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(); |
|
|
|
|
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
|
|
|
} |