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