|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_mapper\Mappers; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\kw_mapper\MapperException; |
|
7
|
|
|
use kalanis\kw_mapper\Records; |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Trait TFinder |
|
12
|
|
|
* @package kalanis\kw_mapper\Mappers |
|
13
|
|
|
* Abstract for manipulation with file content as table |
|
14
|
|
|
*/ |
|
15
|
|
|
trait TFinder |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var Records\ARecord[] */ |
|
18
|
|
|
protected $records = []; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param Records\ARecord $record |
|
22
|
|
|
* @param bool $usePks |
|
23
|
|
|
* @param bool $wantFromStorage |
|
24
|
|
|
* @throws MapperException |
|
25
|
|
|
* @return string[]|int[] |
|
26
|
|
|
*/ |
|
27
|
23 |
|
protected function findMatched(Records\ARecord $record, bool $usePks = false, bool $wantFromStorage = false): array |
|
28
|
|
|
{ |
|
29
|
23 |
|
$this->loadOnDemand($record); |
|
30
|
|
|
|
|
31
|
23 |
|
$toProcess = array_keys($this->records); |
|
32
|
23 |
|
$toProcess = array_combine($toProcess, $toProcess); |
|
33
|
|
|
|
|
34
|
|
|
// through relations |
|
35
|
23 |
|
foreach ($this->getRelations() as $objectKey => $recordKey) { |
|
36
|
23 |
|
if (!$record->offsetExists($objectKey)) { // nothing with unknown relation key in record |
|
37
|
|
|
// @codeCoverageIgnoreStart |
|
38
|
|
|
if ($usePks && in_array($objectKey, $this->getPrimaryKeys())) { // is empty PK |
|
39
|
|
|
return []; // probably error? |
|
40
|
|
|
} |
|
41
|
|
|
continue; |
|
42
|
|
|
// @codeCoverageIgnoreEnd |
|
43
|
|
|
} |
|
44
|
23 |
|
if (empty($record->offsetGet($objectKey))) { // nothing with empty data |
|
45
|
23 |
|
if ($usePks && in_array($objectKey, $this->getPrimaryKeys())) { // is empty PK |
|
46
|
1 |
|
return []; |
|
47
|
|
|
} |
|
48
|
23 |
|
continue; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
8 |
|
foreach ($this->records as $knownKey => $knownRecord) { |
|
52
|
8 |
|
if ( !isset($toProcess[$knownKey]) ) { // not twice |
|
53
|
5 |
|
continue; |
|
54
|
|
|
} |
|
55
|
8 |
|
if ($usePks && !in_array($objectKey, $this->getPrimaryKeys())) { // is not PK |
|
56
|
3 |
|
continue; |
|
57
|
|
|
} |
|
58
|
8 |
|
if ($wantFromStorage && !$knownRecord->getEntry($objectKey)->isFromStorage()) { // look through only values known in storage |
|
59
|
3 |
|
continue; |
|
60
|
|
|
} |
|
61
|
8 |
|
if ( !$knownRecord->offsetExists($objectKey) ) { // unknown relation key in record is not allowed into compare |
|
62
|
|
|
// @codeCoverageIgnoreStart |
|
63
|
|
|
unset($toProcess[$knownKey]); |
|
64
|
|
|
continue; |
|
65
|
|
|
} |
|
66
|
|
|
// @codeCoverageIgnoreEnd |
|
67
|
8 |
|
if ( empty($knownRecord->offsetGet($objectKey)) ) { // empty input is not need to compare |
|
68
|
1 |
|
unset($toProcess[$knownKey]); |
|
69
|
1 |
|
continue; |
|
70
|
|
|
} |
|
71
|
8 |
|
if ( strval($knownRecord->offsetGet($objectKey)) != strval($record->offsetGet($objectKey)) ) { |
|
72
|
7 |
|
unset($toProcess[$knownKey]); |
|
73
|
7 |
|
continue; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
23 |
|
return $toProcess; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* More records on one mapper - reload with correct one |
|
83
|
|
|
* @param Records\ARecord $record |
|
84
|
|
|
* @throws MapperException |
|
85
|
|
|
*/ |
|
86
|
23 |
|
protected function loadOnDemand(Records\ARecord $record): void |
|
87
|
|
|
{ |
|
88
|
23 |
|
if (empty($this->records)) { |
|
89
|
5 |
|
$this->records = $this->loadSource($record); |
|
90
|
|
|
} else { |
|
91
|
23 |
|
$test = reset($this->records); |
|
92
|
23 |
|
if (get_class($test) != get_class($record)) { // reload other data - changed record |
|
93
|
|
|
// @codeCoverageIgnoreStart |
|
94
|
|
|
$this->records = $this->loadSource($record); |
|
95
|
|
|
} |
|
96
|
|
|
// @codeCoverageIgnoreEnd |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param Records\ARecord $record |
|
102
|
|
|
* @throws MapperException |
|
103
|
|
|
* @return Records\ARecord[] |
|
104
|
|
|
*/ |
|
105
|
|
|
abstract protected function loadSource(Records\ARecord $record): array; |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return string[] |
|
109
|
|
|
*/ |
|
110
|
|
|
abstract public function getPrimaryKeys(): array; |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @return array<string|int, string|int> |
|
114
|
|
|
*/ |
|
115
|
|
|
abstract public function getRelations(): array; |
|
116
|
|
|
} |
|
117
|
|
|
|