|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace UniMan\Drivers\Redis\Forms; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Nette\Application\UI\Form; |
|
7
|
|
|
use Nette\Utils\ArrayHash; |
|
8
|
|
|
use UniMan\Core\Forms\DatabaseForm\DatabaseFormInterface; |
|
9
|
|
|
use UniMan\Drivers\Redis\RedisDatabaseAliasStorage; |
|
10
|
|
|
|
|
11
|
|
|
class RedisEditDatabaseForm implements DatabaseFormInterface |
|
12
|
|
|
{ |
|
13
|
|
|
private $database; |
|
14
|
|
|
|
|
15
|
|
|
private $databaseAliasStorage; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct($database, RedisDatabaseAliasStorage $databaseAliasStorage) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->database = $database; |
|
20
|
|
|
$this->databaseAliasStorage = $databaseAliasStorage; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function addFieldsToForm(Form $form) |
|
24
|
|
|
{ |
|
25
|
|
|
try { |
|
26
|
|
|
$this->databaseAliasStorage->check(); |
|
27
|
|
|
} catch (Exception $e) { |
|
28
|
|
|
$form->addError($e->getMessage()); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
$form->addText('database', 'redis.database_form.database.label') |
|
32
|
|
|
->setRequired('redis.database_form.database.required') |
|
|
|
|
|
|
33
|
|
|
->setDisabled(); |
|
34
|
|
|
|
|
35
|
|
|
$form->addText('alias', 'redis.database_form.alias.label') |
|
36
|
|
|
->setOption('description', 'redis.database_form.alias.description'); |
|
37
|
|
|
|
|
38
|
|
|
$form->setDefaults([ |
|
39
|
|
|
'database' => $this->database, |
|
40
|
|
|
'alias' => $this->databaseAliasStorage->load($this->database), |
|
41
|
|
|
]); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function submit(Form $form, ArrayHash $values) |
|
45
|
|
|
{ |
|
46
|
|
|
try { |
|
47
|
|
|
$this->databaseAliasStorage->check(); |
|
48
|
|
|
} catch (Exception $e) { |
|
49
|
|
|
$form->addError($e->getMessage()); |
|
50
|
|
|
return; |
|
51
|
|
|
} |
|
52
|
|
|
$aliases = $this->databaseAliasStorage->loadAll(); |
|
53
|
|
|
$aliases[$this->database] = $values['alias']; |
|
54
|
|
|
if (!$values['alias']) { |
|
55
|
|
|
unset($aliases[$this->database]); |
|
56
|
|
|
} |
|
57
|
|
|
return $this->databaseAliasStorage->save($aliases); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: