|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace UniMan\Drivers\Redis\DataManager; |
|
4
|
|
|
|
|
5
|
|
|
use RedisProxy\RedisProxy; |
|
6
|
|
|
use UniMan\Core\Utils\Filter; |
|
7
|
|
|
|
|
8
|
|
|
class RedisListDataManager |
|
9
|
|
|
{ |
|
10
|
|
|
private $connection; |
|
11
|
|
|
|
|
12
|
|
|
public function __construct(RedisProxy $connection) |
|
13
|
|
|
{ |
|
14
|
|
|
$this->connection = $connection; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
public function itemsCount(string $key, array $filter): int |
|
18
|
|
|
{ |
|
19
|
|
|
if (!$filter) { |
|
|
|
|
|
|
20
|
|
|
return $this->connection->llen($key); |
|
21
|
|
|
} |
|
22
|
|
|
$totalItems = 0; |
|
23
|
|
|
foreach ($this->connection->lrange($key, 0, -1) as $index => $element) { |
|
24
|
|
|
$item = $this->createElement($index, $element); |
|
25
|
|
|
if (Filter::apply($item, $filter)) { |
|
26
|
|
|
$totalItems++; |
|
27
|
|
|
} |
|
28
|
|
|
} |
|
29
|
|
|
return $totalItems; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function items(string $key, int $page, int $onPage, array $filter = []): array |
|
33
|
|
|
{ |
|
34
|
|
|
$items = []; |
|
35
|
|
|
$offset = ($page - 1) * $onPage; |
|
36
|
|
|
if (!$filter) { |
|
|
|
|
|
|
37
|
|
|
$res = $this->connection->lrange($key, $offset, $offset + $onPage - 1); |
|
38
|
|
|
foreach ($res as $pos => $element) { |
|
39
|
|
|
$index = $offset + $pos; |
|
40
|
|
|
$items[$index] = $this->createElement($index, $element); |
|
41
|
|
|
} |
|
42
|
|
|
return $items; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$skipped = 0; |
|
46
|
|
|
foreach ($this->connection->lrange($key, 0, -1) as $index => $element) { |
|
47
|
|
|
$item = $this->createElement($index, $element); |
|
48
|
|
|
if (!Filter::apply($item, $filter)) { |
|
49
|
|
|
continue; |
|
50
|
|
|
} |
|
51
|
|
|
if ($skipped < $offset) { |
|
52
|
|
|
$skipped++; |
|
53
|
|
|
continue; |
|
54
|
|
|
} |
|
55
|
|
|
$items[$index] = $item; |
|
56
|
|
|
if (count($items) === $onPage) { |
|
57
|
|
|
break; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
return $items; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
private function createElement(int $index, string $element): array |
|
64
|
|
|
{ |
|
65
|
|
|
return [ |
|
66
|
|
|
'index' => $index, |
|
67
|
|
|
'element' => $element, |
|
68
|
|
|
'length' => strlen($element), |
|
69
|
|
|
]; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.