1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Tomaj\NetteApi\Params; |
6
|
|
|
|
7
|
|
|
use Nette\Application\UI\Form; |
8
|
|
|
use Nette\Forms\Controls\BaseControl; |
9
|
|
|
use Nette\Utils\Html; |
10
|
|
|
use Tomaj\NetteApi\ValidationResult\ValidationResult; |
11
|
|
|
use Tomaj\NetteApi\ValidationResult\ValidationResultInterface; |
12
|
|
|
|
13
|
|
|
abstract class InputParam implements ParamInterface |
14
|
|
|
{ |
15
|
|
|
const TYPE_POST = 'POST'; |
16
|
|
|
const TYPE_GET = 'GET'; |
17
|
|
|
const TYPE_PUT = 'PUT'; |
18
|
|
|
const TYPE_FILE = 'FILE'; |
19
|
|
|
const TYPE_COOKIE = 'COOKIE'; |
20
|
|
|
const TYPE_POST_RAW = 'POST_RAW'; |
21
|
|
|
const TYPE_POST_JSON = 'POST_JSON'; |
22
|
|
|
|
23
|
|
|
const OPTIONAL = false; |
24
|
|
|
const REQUIRED = true; |
25
|
|
|
|
26
|
|
|
/** @var string */ |
27
|
|
|
protected $type; |
28
|
|
|
|
29
|
|
|
/** @var string */ |
30
|
|
|
protected $key; |
31
|
|
|
|
32
|
|
|
/** @var bool */ |
33
|
|
|
protected $required = self::OPTIONAL; |
34
|
|
|
|
35
|
|
|
/** @var array|null */ |
36
|
|
|
protected $availableValues = null; |
37
|
|
|
|
38
|
|
|
/** @var bool */ |
39
|
|
|
protected $multi = false; |
40
|
|
|
|
41
|
|
|
/** @var string */ |
42
|
|
|
protected $description = ''; |
43
|
|
|
|
44
|
|
|
/** @var mixed */ |
45
|
|
|
protected $default; |
46
|
|
|
|
47
|
|
|
/** @var mixed */ |
48
|
|
|
protected $example; |
49
|
|
|
|
50
|
72 |
|
public function __construct(string $key) |
51
|
|
|
{ |
52
|
72 |
|
$this->key = $key; |
53
|
72 |
|
} |
54
|
|
|
|
55
|
45 |
|
public function setRequired(): self |
56
|
|
|
{ |
57
|
45 |
|
$this->required = self::REQUIRED; |
58
|
45 |
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
24 |
|
public function setAvailableValues(array $availableValues): self |
62
|
|
|
{ |
63
|
24 |
|
$this->availableValues = $availableValues; |
64
|
24 |
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
12 |
|
public function setMulti(): self |
68
|
|
|
{ |
69
|
12 |
|
$this->multi = true; |
70
|
12 |
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
21 |
|
public function getType(): string |
74
|
|
|
{ |
75
|
21 |
|
return $this->type; |
76
|
|
|
} |
77
|
|
|
|
78
|
36 |
|
public function getKey(): string |
79
|
|
|
{ |
80
|
36 |
|
return $this->key; |
81
|
|
|
} |
82
|
|
|
|
83
|
21 |
|
public function isRequired(): bool |
84
|
|
|
{ |
85
|
21 |
|
return $this->required; |
86
|
|
|
} |
87
|
|
|
|
88
|
12 |
|
public function getAvailableValues(): ?array |
89
|
|
|
{ |
90
|
12 |
|
return $this->availableValues; |
91
|
|
|
} |
92
|
|
|
|
93
|
45 |
|
public function isMulti(): bool |
94
|
|
|
{ |
95
|
45 |
|
return $this->multi; |
96
|
|
|
} |
97
|
|
|
|
98
|
3 |
|
public function setDescription(string $description): self |
99
|
|
|
{ |
100
|
3 |
|
$this->description = $description; |
101
|
3 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
3 |
|
public function getDescription(): string |
105
|
|
|
{ |
106
|
3 |
|
return $this->description; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param mixed $default |
111
|
|
|
* @return self |
112
|
|
|
*/ |
113
|
3 |
|
public function setDefault($default): self |
114
|
|
|
{ |
115
|
3 |
|
$this->default = $default; |
116
|
3 |
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return mixed |
121
|
|
|
*/ |
122
|
12 |
|
public function getDefault() |
123
|
|
|
{ |
124
|
12 |
|
return $this->default; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param mixed $example |
129
|
|
|
* @return self |
130
|
|
|
*/ |
131
|
3 |
|
public function setExample($example): self |
132
|
|
|
{ |
133
|
3 |
|
$this->example = $example; |
134
|
3 |
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return mixed |
139
|
|
|
*/ |
140
|
12 |
|
public function getExample() |
141
|
|
|
{ |
142
|
12 |
|
return $this->example; |
143
|
|
|
} |
144
|
|
|
|
145
|
12 |
|
public function updateConsoleForm(Form $form): void |
146
|
|
|
{ |
147
|
12 |
|
$count = $this->isMulti() ? 5 : 1; // TODO moznost nastavit kolko inputov sa ma vygenerovat v konzole, default moze byt 5 |
148
|
12 |
|
for ($i = 0; $i < $count; $i++) { |
149
|
12 |
|
$key = $this->getKey(); |
150
|
12 |
|
if ($this->isMulti()) { |
151
|
3 |
|
$key = $key . '___' . $i; |
152
|
|
|
} |
153
|
12 |
|
$input = $this->addFormInput($form, $key); |
154
|
12 |
|
if ($this->description) { |
155
|
3 |
|
$input->setOption('description', Html::el('div', ['class' => 'param-description'])->setHtml($this->description)); |
156
|
|
|
} |
157
|
12 |
|
if ($this->getExample() || $this->getDefault()) { |
158
|
3 |
|
$default = $this->getExample() ?: $this->getDefault(); |
159
|
3 |
|
$default = is_array($default) ? ($default[$i] ?? null) : $default; |
160
|
3 |
|
$input->setDefaultValue($default); |
161
|
|
|
} |
162
|
|
|
} |
163
|
12 |
|
} |
164
|
|
|
|
165
|
6 |
|
protected function addFormInput(Form $form, string $key): BaseControl |
166
|
|
|
{ |
167
|
6 |
|
if ($this->getAvailableValues()) { |
168
|
3 |
|
return $form->addSelect($key, $this->getParamLabel(), array_combine($this->getAvailableValues(), $this->getAvailableValues())) |
169
|
3 |
|
->setPrompt('Select ' . $this->getLabel()); |
170
|
|
|
} |
171
|
6 |
|
return $form->addText($key, $this->getParamLabel()); |
172
|
|
|
} |
173
|
|
|
|
174
|
12 |
|
protected function getLabel(): string |
175
|
|
|
{ |
176
|
12 |
|
return ucfirst(str_replace('_', ' ', $this->getKey())); |
177
|
|
|
} |
178
|
|
|
|
179
|
12 |
|
protected function getParamLabel(): string |
180
|
|
|
{ |
181
|
12 |
|
$title = $this->getLabel(); |
182
|
12 |
|
if ($this->isRequired()) { |
183
|
3 |
|
$title .= ' *'; |
184
|
|
|
} |
185
|
12 |
|
$title .= ' (' . $this->getType() . ')'; |
186
|
12 |
|
return $title; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Check if actual value from environment is valid |
191
|
|
|
*/ |
192
|
21 |
|
public function validate(): ValidationResultInterface |
193
|
|
|
{ |
194
|
21 |
|
$value = $this->getValue(); |
195
|
21 |
|
if ($this->required === self::OPTIONAL && ($value === null || $value === '')) { |
196
|
9 |
|
return new ValidationResult(ValidationResult::STATUS_OK); |
197
|
|
|
} |
198
|
|
|
|
199
|
18 |
|
if ($this->required && ($value === null || $value === '')) { |
200
|
9 |
|
return new ValidationResult(ValidationResult::STATUS_ERROR, ['Field is required']); |
201
|
|
|
} |
202
|
|
|
|
203
|
12 |
|
if ($this->availableValues !== null) { |
204
|
9 |
|
$result = empty(array_diff(($this->isMulti() ? $value : [$value]), $this->availableValues)); |
205
|
9 |
|
if ($result === false) { |
206
|
9 |
|
return new ValidationResult(ValidationResult::STATUS_ERROR, ['Field contains not available value(s)']); |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
210
|
9 |
|
return new ValidationResult(ValidationResult::STATUS_OK); |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|