1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_mapper\Mappers\File; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_mapper\Adapters\DataExchange; |
7
|
|
|
use kalanis\kw_mapper\Interfaces\IEntryType; |
8
|
|
|
use kalanis\kw_mapper\MapperException; |
9
|
|
|
use kalanis\kw_mapper\Mappers\TFinder; |
10
|
|
|
use kalanis\kw_mapper\Mappers\TStore; |
11
|
|
|
use kalanis\kw_mapper\Mappers\TTranslate; |
12
|
|
|
use kalanis\kw_mapper\Records; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class ATable |
17
|
|
|
* @package kalanis\kw_mapper\Mappers\File |
18
|
|
|
* Abstract for manipulation with file content as table |
19
|
|
|
*/ |
20
|
|
|
abstract class ATable extends AStorage |
21
|
|
|
{ |
22
|
|
|
use TFinder; |
23
|
|
|
use TStore; |
24
|
|
|
use TTranslate; |
25
|
|
|
|
26
|
|
|
/** @var bool */ |
27
|
|
|
protected $orderFromFirst = true; |
28
|
|
|
|
29
|
3 |
|
public function orderFromFirst(bool $orderFromFirst = true): self |
30
|
|
|
{ |
31
|
3 |
|
$this->orderFromFirst = $orderFromFirst; |
32
|
3 |
|
return $this; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param Records\ARecord|Records\PageRecord $record |
37
|
|
|
* @throws MapperException |
38
|
|
|
* @return bool |
39
|
|
|
*/ |
40
|
5 |
|
protected function insertRecord(Records\ARecord $record): bool |
41
|
|
|
{ |
42
|
5 |
|
$matches = $this->findMatched($record, !empty($this->getPrimaryKeys())); |
43
|
5 |
|
if (!empty($matches)) { // found!!! |
44
|
1 |
|
return false; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// pks |
48
|
5 |
|
$records = array_map([$this, 'toArray'], $this->records); |
49
|
5 |
|
foreach ($this->getPrimaryKeys() as $primaryKey) { |
50
|
4 |
|
$entry = $record->getEntry($primaryKey); |
51
|
4 |
|
if (in_array($entry->getType(), [IEntryType::TYPE_INTEGER, IEntryType::TYPE_FLOAT])) { |
52
|
1 |
|
if (empty($entry->getData())) { |
53
|
1 |
|
$data = empty($records) ? 1 : intval(max(array_column($records, $primaryKey))) + 1 ; |
54
|
1 |
|
$entry->setData($data); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
5 |
|
$this->records = $this->orderFromFirst ? array_merge($this->records, [$record]) : array_merge([$record], $this->records); |
60
|
5 |
|
return $this->saveSource($this->records); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param Records\ARecord $object |
65
|
|
|
* @return array<string|int, string|int|float|object|array<string|int|float|object>> |
66
|
|
|
*/ |
67
|
5 |
|
public function toArray($object) |
68
|
|
|
{ |
69
|
5 |
|
$ex = new DataExchange($object); |
70
|
5 |
|
return $ex->export(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param Records\ARecord|Records\PageRecord $record |
75
|
|
|
* @throws MapperException |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
3 |
|
protected function updateRecord(Records\ARecord $record): bool |
79
|
|
|
{ |
80
|
3 |
|
$matches = $this->findMatched($record, !empty($this->getPrimaryKeys()), true); |
81
|
3 |
|
if (empty($matches)) { // nothing found |
82
|
|
|
return false; |
83
|
|
|
} |
84
|
|
|
|
85
|
3 |
|
$dataLine = & $this->records[reset($matches)]; |
86
|
3 |
|
foreach ($this->getRelations() as $objectKey => $recordKey) { |
87
|
3 |
|
if (in_array($objectKey, $this->getPrimaryKeys())) { |
88
|
2 |
|
continue; // no to change pks |
89
|
|
|
} |
90
|
3 |
|
$dataLine->offsetSet($objectKey, $record->offsetGet($objectKey)); |
91
|
|
|
} |
92
|
3 |
|
return $this->saveSource($this->records); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param Records\ARecord|Records\PageRecord $record |
97
|
|
|
* @throws MapperException |
98
|
|
|
* @return int |
99
|
|
|
*/ |
100
|
1 |
|
public function countRecord(Records\ARecord $record): int |
101
|
|
|
{ |
102
|
1 |
|
$matches = $this->findMatched($record); |
103
|
1 |
|
return count($matches); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param Records\ARecord|Records\PageRecord $record |
108
|
|
|
* @throws MapperException |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
6 |
|
protected function loadRecord(Records\ARecord $record): bool |
112
|
|
|
{ |
113
|
6 |
|
$matches = $this->findMatched($record); |
114
|
6 |
|
if (empty($matches)) { // nothing found |
115
|
2 |
|
return false; |
116
|
|
|
} |
117
|
|
|
|
118
|
5 |
|
$dataLine = & $this->records[reset($matches)]; |
119
|
5 |
|
foreach ($this->getRelations() as $objectKey => $recordKey) { |
120
|
5 |
|
$entry = $record->getEntry($objectKey); |
121
|
5 |
|
$entry->setData($dataLine->offsetGet($objectKey), true); |
122
|
|
|
} |
123
|
5 |
|
return true; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param Records\ARecord|Records\PageRecord $record |
128
|
|
|
* @throws MapperException |
129
|
|
|
* @return bool |
130
|
|
|
* Scan array and remove items that have set equal values as that in passed record |
131
|
|
|
*/ |
132
|
4 |
|
protected function deleteRecord(Records\ARecord $record): bool |
133
|
|
|
{ |
134
|
4 |
|
$toDelete = $this->findMatched($record); |
135
|
4 |
|
if (empty($toDelete)) { |
136
|
1 |
|
return false; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
// remove matched |
140
|
3 |
|
foreach ($toDelete as $key) { |
141
|
3 |
|
unset($this->records[$key]); |
142
|
|
|
} |
143
|
3 |
|
return $this->saveSource($this->records); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param Records\ARecord $record |
148
|
|
|
* @throws MapperException |
149
|
|
|
* @return Records\ARecord[] |
150
|
|
|
*/ |
151
|
15 |
|
public function loadMultiple(Records\ARecord $record): array |
152
|
|
|
{ |
153
|
15 |
|
$toLoad = $this->findMatched($record); |
154
|
|
|
|
155
|
15 |
|
$result = []; |
156
|
15 |
|
foreach ($toLoad as $key) { |
157
|
15 |
|
$result[] = $this->records[$key]; |
158
|
|
|
} |
159
|
15 |
|
return $result; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|