|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_connect\search; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\kw_connect\core\AConnector; |
|
7
|
|
|
use kalanis\kw_connect\core\Interfaces\IFilterFactory; |
|
8
|
|
|
use kalanis\kw_connect\core\Interfaces\IFilterSubs; |
|
9
|
|
|
use kalanis\kw_connect\core\Interfaces\IIterableConnector; |
|
10
|
|
|
use kalanis\kw_connect\core\Interfaces\IOrder; |
|
11
|
|
|
use kalanis\kw_connect\core\Interfaces\IRow; |
|
12
|
|
|
use kalanis\kw_mapper\Interfaces\IQueryBuilder; |
|
13
|
|
|
use kalanis\kw_mapper\MapperException; |
|
14
|
|
|
use kalanis\kw_mapper\Records\ARecord; |
|
15
|
|
|
use kalanis\kw_mapper\Search\Search as MapperSearch; |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class Connector |
|
20
|
|
|
* @package kalanis\kw_table\Connector\Sources |
|
21
|
|
|
* Data source is kw_mapper/Search |
|
22
|
|
|
*/ |
|
23
|
|
|
class Connector extends AConnector implements IIterableConnector |
|
24
|
|
|
{ |
|
25
|
|
|
protected MapperSearch $dataSource; |
|
26
|
|
|
/** @var ARecord[] */ |
|
27
|
|
|
protected array $rawData = []; |
|
28
|
|
|
protected bool $dataFetched = false; |
|
29
|
|
|
|
|
30
|
1 |
|
public function __construct(MapperSearch $search) |
|
31
|
|
|
{ |
|
32
|
1 |
|
$this->dataSource = $search; |
|
33
|
1 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @throws MapperException |
|
37
|
|
|
*/ |
|
38
|
1 |
|
protected function parseData(): void |
|
39
|
|
|
{ |
|
40
|
1 |
|
foreach ($this->rawData as $mapper) { |
|
41
|
1 |
|
$this->translatedData[$this->getPrimaryKey($mapper)] = $this->getTranslated($mapper); |
|
42
|
|
|
} |
|
43
|
1 |
|
} |
|
44
|
|
|
|
|
45
|
1 |
|
protected function getTranslated(ARecord $data): IRow |
|
46
|
|
|
{ |
|
47
|
1 |
|
return new Row($data); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param ARecord $record |
|
52
|
|
|
* @throws MapperException |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
1 |
|
protected function getPrimaryKey(ARecord $record): string |
|
56
|
|
|
{ |
|
57
|
1 |
|
$pks = $record->getMapper()->getPrimaryKeys(); |
|
58
|
1 |
|
$values = []; |
|
59
|
1 |
|
foreach ($pks as $pk) { |
|
60
|
1 |
|
$values[] = strval($record->offsetGet($pk)); |
|
61
|
|
|
} |
|
62
|
1 |
|
return implode('_', $values); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
1 |
|
public function setFiltering(string $colName, string $filterType, $value): void |
|
66
|
|
|
{ |
|
67
|
1 |
|
$type = $this->getFilterFactory()->getFilter($filterType); |
|
68
|
1 |
|
if ($type instanceof IFilterSubs) { |
|
69
|
1 |
|
$type->addFilterFactory($this->getFilterFactory()); |
|
70
|
|
|
} |
|
71
|
1 |
|
$type->setDataSource($this->dataSource); |
|
72
|
1 |
|
$type->setFiltering($colName, $value); |
|
73
|
1 |
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param string $colName |
|
77
|
|
|
* @param string $direction |
|
78
|
|
|
* @throws MapperException |
|
79
|
|
|
*/ |
|
80
|
1 |
|
public function setOrdering(string $colName, string $direction): void |
|
81
|
|
|
{ |
|
82
|
1 |
|
$this->dataSource->orderBy( |
|
83
|
1 |
|
$colName, |
|
84
|
1 |
|
IOrder::ORDER_ASC == $direction ? IQueryBuilder::ORDER_ASC : IQueryBuilder::ORDER_DESC |
|
85
|
|
|
); |
|
86
|
1 |
|
} |
|
87
|
|
|
|
|
88
|
1 |
|
public function setPagination(?int $offset, ?int $limit): void |
|
89
|
|
|
{ |
|
90
|
1 |
|
$this->dataSource->offset($offset); |
|
91
|
1 |
|
$this->dataSource->limit($limit); |
|
92
|
1 |
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @throws MapperException |
|
96
|
|
|
* @return int |
|
97
|
|
|
*/ |
|
98
|
1 |
|
public function getTotalCount(): int |
|
99
|
|
|
{ |
|
100
|
1 |
|
return $this->dataSource->getCount(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @throws MapperException |
|
105
|
|
|
*/ |
|
106
|
1 |
|
public function fetchData(): void |
|
107
|
|
|
{ |
|
108
|
1 |
|
$this->rawData = $this->dataSource->getResults(); |
|
109
|
1 |
|
$this->parseData(); |
|
110
|
1 |
|
} |
|
111
|
|
|
|
|
112
|
1 |
|
public function getFilterFactory(): IFilterFactory |
|
113
|
|
|
{ |
|
114
|
1 |
|
return Filters\Factory::getInstance(); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|