|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_mapper\Mappers\Shared; |
|
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\Shared |
|
13
|
|
|
* Abstract for manipulation with file content as table |
|
14
|
|
|
*/ |
|
15
|
|
|
trait TFinder |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var Records\ARecord[] */ |
|
18
|
|
|
protected array $records = []; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param Records\ARecord $record |
|
22
|
|
|
* @param bool $usePks |
|
23
|
|
|
* @param bool $wantFromStorage |
|
24
|
|
|
* @throws MapperException |
|
25
|
|
|
* @return Records\ARecord[] |
|
26
|
|
|
*/ |
|
27
|
29 |
|
protected function findMatched(Records\ARecord $record, bool $usePks = false, bool $wantFromStorage = false): array |
|
28
|
|
|
{ |
|
29
|
29 |
|
$this->loadOnDemand($record); |
|
30
|
|
|
|
|
31
|
29 |
|
$toProcess = array_combine(array_keys($this->records), array_values($this->records)); // copy array - records will be removed when don't match |
|
32
|
29 |
|
if (false === $toProcess) { |
|
33
|
|
|
// @codeCoverageIgnoreStart |
|
34
|
|
|
// php7- |
|
35
|
|
|
throw new MapperException('Combine on field went wrong. Call php support.'); |
|
36
|
|
|
} |
|
37
|
|
|
// @codeCoverageIgnoreEnd |
|
38
|
29 |
|
$toCompare = $this->getArrayToCompare($record, $usePks, $wantFromStorage); |
|
39
|
|
|
|
|
40
|
29 |
|
if ($usePks) { // nothing to get when any necessary primary key is unknown |
|
41
|
4 |
|
foreach ($record->getMapper()->getPrimaryKeys() as $primaryKey) { |
|
42
|
4 |
|
if (!isset($toCompare[$primaryKey])) { |
|
43
|
1 |
|
return []; |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
// through relations |
|
49
|
29 |
|
foreach ($toCompare as $relationKey => $compareValue) { |
|
50
|
12 |
|
foreach ($this->records as $positionKey => $knownRecord) { |
|
51
|
12 |
|
if ( !isset($toProcess[$positionKey]) ) { // not twice |
|
52
|
1 |
|
continue; |
|
53
|
|
|
} |
|
54
|
12 |
|
if ( !$knownRecord->offsetExists($relationKey) ) { // unknown relation key in record is not allowed into compare |
|
55
|
1 |
|
unset($toProcess[$positionKey]); |
|
56
|
1 |
|
continue; |
|
57
|
|
|
} |
|
58
|
11 |
|
if ( strval($knownRecord->offsetGet($relationKey)) != strval($compareValue) ) { |
|
59
|
11 |
|
unset($toProcess[$positionKey]); |
|
60
|
11 |
|
continue; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
29 |
|
return $toProcess; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* More records on one mapper - reload with correct one |
|
70
|
|
|
* @param Records\ARecord $record |
|
71
|
|
|
* @throws MapperException |
|
72
|
|
|
*/ |
|
73
|
29 |
|
protected function loadOnDemand(Records\ARecord $record): void |
|
74
|
|
|
{ |
|
75
|
29 |
|
if (empty($this->records)) { |
|
76
|
11 |
|
$this->records = $this->loadSource($record); |
|
77
|
|
|
} else { |
|
78
|
20 |
|
$test = reset($this->records); |
|
79
|
20 |
|
if (get_class($test) != get_class($record)) { // reload other data - changed record |
|
80
|
|
|
// @codeCoverageIgnoreStart |
|
81
|
|
|
$this->records = $this->loadSource($record); |
|
82
|
|
|
} |
|
83
|
|
|
// @codeCoverageIgnoreEnd |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param Records\ARecord $record |
|
89
|
|
|
* @param bool $usePks |
|
90
|
|
|
* @param bool $wantFromStorage |
|
91
|
|
|
* @throws MapperException |
|
92
|
|
|
* @return array<string|int, mixed> |
|
93
|
|
|
*/ |
|
94
|
37 |
|
protected function getArrayToCompare(Records\ARecord $record, bool $usePks, bool $wantFromStorage): array |
|
95
|
|
|
{ |
|
96
|
37 |
|
$stored = []; |
|
97
|
37 |
|
$written = []; |
|
98
|
37 |
|
foreach ($record as $key => $item) { |
|
99
|
37 |
|
$entry = $record->getEntry($key); |
|
100
|
37 |
|
if ($usePks && !in_array($key, $record->getMapper()->getPrimaryKeys())) { |
|
101
|
8 |
|
continue; |
|
102
|
|
|
} |
|
103
|
37 |
|
if (false === $entry->getData() && !$entry->isFromStorage()) { |
|
104
|
29 |
|
continue; |
|
105
|
|
|
} |
|
106
|
20 |
|
if ($entry->isFromStorage()) { |
|
107
|
6 |
|
$stored[$key] = $entry->getData(); |
|
108
|
|
|
} else { |
|
109
|
20 |
|
$written[$key] = $entry->getData(); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
37 |
|
return $wantFromStorage ? (empty($stored) ? $written : $stored) : array_merge($stored, $written); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
9 |
|
protected function clearSource(): void |
|
116
|
|
|
{ |
|
117
|
9 |
|
$this->records = []; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param Records\ARecord $record |
|
122
|
|
|
* @throws MapperException |
|
123
|
|
|
* @return Records\ARecord[] |
|
124
|
|
|
*/ |
|
125
|
|
|
abstract protected function loadSource(Records\ARecord $record): array; |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @return string[] |
|
129
|
|
|
*/ |
|
130
|
|
|
abstract public function getPrimaryKeys(): array; |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @return array<string|int, string|int> |
|
134
|
|
|
*/ |
|
135
|
|
|
abstract public function getRelations(): array; |
|
136
|
|
|
} |
|
137
|
|
|
|