|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace UniMan\Drivers\Redis\Forms; |
|
4
|
|
|
|
|
5
|
|
|
use Nette\Application\UI\Form; |
|
6
|
|
|
use Nette\Utils\ArrayHash; |
|
7
|
|
|
use RedisProxy\RedisProxy; |
|
8
|
|
|
use UniMan\Core\Forms\TableForm\TableFormInterface; |
|
9
|
|
|
|
|
10
|
|
|
class RedisSortedSetForm implements TableFormInterface |
|
11
|
|
|
{ |
|
12
|
|
|
private $connection; |
|
13
|
|
|
|
|
14
|
|
|
private $key; |
|
15
|
|
|
|
|
16
|
|
|
public function __construct(RedisProxy $connection, string $key = null) |
|
17
|
|
|
{ |
|
18
|
|
|
$this->connection = $connection; |
|
19
|
|
|
$this->key = $key; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function addFieldsToForm(Form $form) |
|
23
|
|
|
{ |
|
24
|
|
|
$form->addText('key', 'redis.sorted_set_form.key.label') |
|
25
|
|
|
->setRequired('redis.sorted_set_form.key.required'); |
|
26
|
|
|
|
|
27
|
|
|
$form->addTextArea('members', 'redis.sorted_set_form.members.label') |
|
28
|
|
|
->setOption('description', 'redis.sorted_set_form.members.description') |
|
29
|
|
|
->setRequired('redis.sorted_set_form.members.required'); |
|
30
|
|
|
|
|
31
|
|
|
if ($this->key) { |
|
|
|
|
|
|
32
|
|
|
$members = []; |
|
33
|
|
|
foreach ($this->connection->zrange($this->key, 0, -1, true) as $member => $score) { |
|
34
|
|
|
$members[] = $member . ':' . $score; |
|
35
|
|
|
} |
|
36
|
|
|
$form->setDefaults([ |
|
37
|
|
|
'key' => $this->key, |
|
38
|
|
|
'members' => implode(',', $members), |
|
39
|
|
|
]); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function submit(Form $form, ArrayHash $values) |
|
44
|
|
|
{ |
|
45
|
|
|
if ($this->key && $this->key !== $values->key) { |
|
|
|
|
|
|
46
|
|
|
$this->connection->rename($this->key, $values->key); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$members = []; |
|
50
|
|
|
foreach (array_map('trim', explode(',', $values->members)) as $item) { |
|
51
|
|
|
$member = $item; |
|
52
|
|
|
$score = 0; |
|
53
|
|
|
if (strpos($item, ':')) { |
|
54
|
|
|
list($member, $score) = explode(':', $item, 2); |
|
55
|
|
|
} |
|
56
|
|
|
$members[trim($member)] = floatval(trim($score)); |
|
57
|
|
|
} |
|
58
|
|
|
$this->connection->zadd($values->key, $members); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: