|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_connect\records; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\kw_connect\arrays\Filters; |
|
7
|
|
|
use kalanis\kw_connect\core\AConnector; |
|
8
|
|
|
use kalanis\kw_connect\core\ConnectException; |
|
9
|
|
|
use kalanis\kw_connect\core\Interfaces\IFilterFactory; |
|
10
|
|
|
use kalanis\kw_connect\core\Interfaces\IIterableConnector; |
|
11
|
|
|
use kalanis\kw_connect\core\Interfaces\IOrder; |
|
12
|
|
|
use kalanis\kw_connect\core\Interfaces\IRow; |
|
13
|
|
|
use kalanis\kw_mapper\Records\ARecord; |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class Connector |
|
18
|
|
|
* @package kalanis\kw_connect\records |
|
19
|
|
|
* Data source is kw_mapper/Record |
|
20
|
|
|
*/ |
|
21
|
|
|
class Connector extends AConnector implements IIterableConnector |
|
22
|
|
|
{ |
|
23
|
|
|
/** @var ARecord[] */ |
|
24
|
|
|
protected array $dataSource; |
|
25
|
|
|
/** @var array<IRow> */ |
|
26
|
|
|
protected array $filteredData = []; |
|
27
|
|
|
protected string $orderDirection = IOrder::ORDER_ASC; |
|
28
|
|
|
protected string $orderColumn = ''; |
|
29
|
|
|
protected ?string $filterByColumn = null; |
|
30
|
|
|
/** @var string|int|null */ |
|
31
|
|
|
protected $filterByNamePart = null; |
|
32
|
|
|
protected ?int $offset = null; |
|
33
|
|
|
protected ?int $limit = null; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param array<ARecord> $records |
|
37
|
|
|
*/ |
|
38
|
4 |
|
public function __construct(array $records) |
|
39
|
|
|
{ |
|
40
|
4 |
|
$this->dataSource = $records; |
|
41
|
4 |
|
} |
|
42
|
|
|
|
|
43
|
3 |
|
protected function parseData(): void |
|
44
|
|
|
{ |
|
45
|
3 |
|
$filtered = array_filter(array_map([$this, 'getTranslated'], $this->dataSource), [$this, 'filterItems']); |
|
46
|
3 |
|
uasort($filtered, [$this, 'sortItems']); |
|
47
|
3 |
|
$this->filteredData = $filtered; |
|
48
|
3 |
|
$this->translatedData = array_slice($filtered, intval($this->offset), $this->limit); |
|
49
|
3 |
|
} |
|
50
|
|
|
|
|
51
|
3 |
|
public function getTranslated(ARecord $data): IRow |
|
52
|
|
|
{ |
|
53
|
3 |
|
return new Row($data); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
public function setFiltering(string $colName, string $filterType, $value): void |
|
57
|
|
|
{ |
|
58
|
1 |
|
$this->filterByColumn = $colName; |
|
59
|
1 |
|
$this->filterByNamePart = is_array($value) ? strval(reset($value)) : strval($value); |
|
60
|
1 |
|
} |
|
61
|
|
|
|
|
62
|
3 |
|
public function setOrdering(string $colName, string $direction): void |
|
63
|
|
|
{ |
|
64
|
3 |
|
$this->orderColumn = $colName; |
|
65
|
3 |
|
$this->orderDirection = $direction; |
|
66
|
3 |
|
} |
|
67
|
|
|
|
|
68
|
1 |
|
public function setPagination(?int $offset, ?int $limit): void |
|
69
|
|
|
{ |
|
70
|
1 |
|
$this->offset = $offset; |
|
71
|
1 |
|
$this->limit = $limit; |
|
72
|
1 |
|
} |
|
73
|
|
|
|
|
74
|
4 |
|
public function getTotalCount(): int |
|
75
|
|
|
{ |
|
76
|
4 |
|
if (empty($this->dataSource)) { |
|
77
|
1 |
|
return 0; |
|
78
|
|
|
} |
|
79
|
3 |
|
if (empty($this->filteredData)) { |
|
80
|
1 |
|
$this->fetchData(); |
|
81
|
|
|
} |
|
82
|
3 |
|
return count($this->filteredData); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
3 |
|
public function fetchData(): void |
|
86
|
|
|
{ |
|
87
|
3 |
|
$this->parseData(); |
|
88
|
3 |
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param IRow $node |
|
92
|
|
|
* @throws ConnectException |
|
93
|
|
|
* @return bool |
|
94
|
|
|
*/ |
|
95
|
3 |
|
public function filterItems(IRow $node): bool |
|
96
|
|
|
{ |
|
97
|
3 |
|
return is_null($this->filterByNamePart) |
|
98
|
1 |
|
|| is_null($this->filterByColumn) |
|
99
|
|
|
|| ( |
|
100
|
1 |
|
$this->columnExists($node, $this->filterByColumn) |
|
101
|
3 |
|
&& $this->compareValues($node, $this->filterByColumn, $this->filterByNamePart) |
|
102
|
|
|
); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
1 |
|
protected function columnExists(IRow $node, string $whichColumn): bool |
|
106
|
|
|
{ |
|
107
|
1 |
|
return $node->__isset($whichColumn); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param IRow $node |
|
112
|
|
|
* @param string $whichColumn |
|
113
|
|
|
* @param string|int $columnValue |
|
114
|
|
|
* @throws ConnectException |
|
115
|
|
|
* @return bool |
|
116
|
|
|
*/ |
|
117
|
1 |
|
protected function compareValues(IRow $node, string $whichColumn, $columnValue): bool |
|
118
|
|
|
{ |
|
119
|
1 |
|
return false !== stripos(strval($node->getValue($whichColumn)), strval($columnValue)); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param IRow $a |
|
124
|
|
|
* @param IRow $b |
|
125
|
|
|
* @throws ConnectException |
|
126
|
|
|
* @return int |
|
127
|
|
|
*/ |
|
128
|
3 |
|
public function sortItems(IRow $a, IRow $b) |
|
129
|
|
|
{ |
|
130
|
|
|
return |
|
131
|
3 |
|
IOrder::ORDER_ASC == $this->orderDirection |
|
132
|
2 |
|
? $a->getValue($this->orderColumn) <=> $b->getValue($this->orderColumn) |
|
133
|
3 |
|
: $b->getValue($this->orderColumn) <=> $a->getValue($this->orderColumn) |
|
134
|
|
|
; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
1 |
|
public function getFilterFactory(): IFilterFactory |
|
138
|
|
|
{ |
|
139
|
1 |
|
return Filters\Factory::getInstance(); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|