1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_connect\doctrine_dbal; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Doctrine\DBAL\Connection; |
7
|
|
|
use Doctrine\DBAL\Query\QueryBuilder; |
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
|
|
|
use kalanis\kw_connect\core\Rows\SimpleArrayRow; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class Connector |
19
|
|
|
* @package kalanis\kw_connect\doctrine_dbal |
20
|
|
|
* Data source is Doctrine DBAL |
21
|
|
|
*/ |
22
|
|
|
class Connector extends AConnector implements IIterableConnector |
23
|
|
|
{ |
24
|
|
|
protected Connection $connection; |
25
|
|
|
protected QueryBuilder $queryBuilder; |
26
|
|
|
protected string $primaryKey = ''; |
27
|
|
|
/** @var array<int, array<string>> */ |
28
|
|
|
protected array $ordering = []; |
29
|
|
|
protected ?int $limit = null; |
30
|
|
|
protected ?int $offset = null; |
31
|
|
|
protected bool $dataFetched = false; |
32
|
|
|
|
33
|
|
|
public function __construct(Connection $connection, string $primaryKey) |
34
|
|
|
{ |
35
|
|
|
$this->connection = $connection; |
36
|
|
|
$this->queryBuilder = $connection->createQueryBuilder(); |
37
|
|
|
$this->primaryKey = $primaryKey; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function setFiltering(string $colName, string $filterType, $value): void |
41
|
|
|
{ |
42
|
|
|
$type = $this->getFilterFactory()->getFilter($filterType); |
43
|
|
|
if ($type instanceof IFilterSubs) { |
44
|
|
|
$type->addFilterFactory($this->getFilterFactory()); |
45
|
|
|
} |
46
|
|
|
$type->setDataSource($this->queryBuilder); |
47
|
|
|
$type->setFiltering($colName, $value); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function setOrdering(string $colName, string $direction): void |
51
|
|
|
{ |
52
|
|
|
$this->ordering[] = [$colName, $direction]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function setPagination(?int $offset, ?int $limit): void |
56
|
|
|
{ |
57
|
|
|
$this->offset = $offset; |
58
|
|
|
$this->limit = $limit; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getTotalCount(): int |
62
|
|
|
{ |
63
|
|
|
$this->queryBuilder->select('count(' . $this->primaryKey. ')'); |
64
|
|
|
return intval($this->queryBuilder->fetchOne()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function fetchData(): void |
68
|
|
|
{ |
69
|
|
|
foreach ($this->ordering as list($colName, $direction)) { |
70
|
|
|
$dir = IOrder::ORDER_ASC == $direction ? 'ASC' : 'DESC' ; |
71
|
|
|
$this->queryBuilder->orderBy(strval($colName), strval($dir)); |
72
|
|
|
} |
73
|
|
|
if (!is_null($this->offset)) { |
74
|
|
|
$this->queryBuilder->setFirstResult($this->offset); |
75
|
|
|
} |
76
|
|
|
if (!is_null($this->limit)) { |
77
|
|
|
$this->queryBuilder->setMaxResults($this->limit); |
78
|
|
|
} |
79
|
|
|
$this->parseData(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
protected function parseData(): void |
83
|
|
|
{ |
84
|
|
|
foreach ( |
85
|
|
|
$this->connection->iterateNumeric( |
86
|
|
|
$this->queryBuilder->getSQL(), |
87
|
|
|
$this->queryBuilder->getParameters(), |
|
|
|
|
88
|
|
|
$this->queryBuilder->getParameterTypes() |
89
|
|
|
) as $value |
90
|
|
|
) { |
91
|
|
|
$this->translatedData[$this->getPrimaryKey($value)] = $this->getTranslated($value); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param array<int|string, bool|float|int|string|null> $data |
97
|
|
|
* @return IRow |
98
|
|
|
*/ |
99
|
|
|
protected function getTranslated($data): IRow |
100
|
|
|
{ |
101
|
|
|
return new SimpleArrayRow($data); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param array<mixed> $data |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
protected function getPrimaryKey($data): string |
109
|
|
|
{ |
110
|
|
|
return strval($data[$this->primaryKey]); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function getFilterFactory(): IFilterFactory |
114
|
|
|
{ |
115
|
|
|
return Filters\Factory::getInstance(); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|