1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_connect\dibi; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Dibi\Fluent; |
7
|
|
|
use Dibi\Row as DRow; |
8
|
|
|
use kalanis\kw_connect\core\AConnector; |
9
|
|
|
use kalanis\kw_connect\core\Interfaces\IFilterFactory; |
10
|
|
|
use kalanis\kw_connect\core\Interfaces\IFilterSubs; |
11
|
|
|
use kalanis\kw_connect\core\Interfaces\IIterableConnector; |
12
|
|
|
use kalanis\kw_connect\core\Interfaces\IOrder; |
13
|
|
|
use kalanis\kw_connect\core\Interfaces\IRow; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Connector |
18
|
|
|
* @package kalanis\kw_connect\dibi |
19
|
|
|
* Data source is Dibi\Fluent |
20
|
|
|
*/ |
21
|
|
|
class Connector extends AConnector implements IIterableConnector |
22
|
|
|
{ |
23
|
|
|
protected Fluent $dibiFluent; |
24
|
|
|
protected string $primaryKey; |
25
|
|
|
/** @var array<int, array<string>> */ |
26
|
|
|
protected array $sorters = []; |
27
|
|
|
protected ?int $limit = null; |
28
|
|
|
protected ?int $offset = null; |
29
|
|
|
/** @var DRow[] */ |
30
|
|
|
protected array $rawData = []; |
31
|
|
|
protected bool $dataFetched = false; |
32
|
|
|
|
33
|
|
|
public function __construct(Fluent $dataSource, string $primaryKey) |
34
|
|
|
{ |
35
|
|
|
$this->dibiFluent = $dataSource; |
36
|
|
|
$this->primaryKey = $primaryKey; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function setFiltering(string $colName, string $filterType, $value): void |
40
|
|
|
{ |
41
|
|
|
$type = $this->getFilterFactory()->getFilter($filterType); |
42
|
|
|
if ($type instanceof IFilterSubs) { |
43
|
|
|
$type->addFilterFactory($this->getFilterFactory()); |
44
|
|
|
} |
45
|
|
|
$type->setDataSource($this->dibiFluent); |
46
|
|
|
$type->setFiltering($colName, $value); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function setOrdering(string $colName, string $direction): void |
50
|
|
|
{ |
51
|
|
|
$this->sorters[] = [$colName, $direction]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function setPagination(?int $offset, ?int $limit): void |
55
|
|
|
{ |
56
|
|
|
$this->offset = $offset; |
57
|
|
|
$this->limit = $limit; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getTotalCount(): int |
61
|
|
|
{ |
62
|
|
|
// count needs only filtered content |
63
|
|
|
$dataSource = clone $this->dibiFluent; |
64
|
|
|
return $dataSource->count(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function fetchData(): void |
68
|
|
|
{ |
69
|
|
|
foreach (array_reverse($this->sorters) as list($colName, $direction)) { |
70
|
|
|
$dir = IOrder::ORDER_ASC == $direction ? 'ASC' : 'DESC' ; |
71
|
|
|
$this->dibiFluent->orderBy($colName, $dir); |
72
|
|
|
} |
73
|
|
|
$this->rawData = $this->dibiFluent->fetchAll($this->offset, $this->limit); |
74
|
|
|
$this->parseData(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
protected function parseData(): void |
78
|
|
|
{ |
79
|
|
|
foreach ($this->rawData as $mapper) { |
80
|
|
|
$this->translatedData[$this->getPrimaryKey($mapper)] = $this->getTranslated($mapper); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function getTranslated(DRow $data): IRow |
85
|
|
|
{ |
86
|
|
|
return new Row($data); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
protected function getPrimaryKey(DRow $record): string |
90
|
|
|
{ |
91
|
|
|
return strval($record->offsetGet($this->primaryKey)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function getFilterFactory(): IFilterFactory |
95
|
|
|
{ |
96
|
|
|
return Filters\Factory::getInstance(); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|