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
|
4 |
|
protected function updateRecord(Records\ARecord $record): bool |
79
|
|
|
{ |
80
|
4 |
|
$matches = $this->findMatched($record, !empty($this->getPrimaryKeys()), true); |
81
|
4 |
|
if (empty($matches)) { // nothing found |
82
|
1 |
|
return false; |
83
|
|
|
} |
84
|
|
|
|
85
|
3 |
|
reset($matches); |
86
|
3 |
|
$dataLine = & $this->records[key($matches)]; |
87
|
3 |
|
foreach ($this->getRelations() as $objectKey => $recordKey) { |
88
|
3 |
|
if (in_array($objectKey, $this->getPrimaryKeys())) { |
89
|
2 |
|
continue; // no to change pks |
90
|
|
|
} |
91
|
3 |
|
$dataLine->offsetSet($objectKey, $record->offsetGet($objectKey)); |
92
|
|
|
} |
93
|
3 |
|
return $this->saveSource($this->records); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param Records\ARecord|Records\PageRecord $record |
98
|
|
|
* @throws MapperException |
99
|
|
|
* @return int |
100
|
|
|
*/ |
101
|
1 |
|
public function countRecord(Records\ARecord $record): int |
102
|
|
|
{ |
103
|
1 |
|
return count($this->findMatched($record)); |
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 |
|
reset($matches); |
119
|
5 |
|
$dataLine = & $this->records[key($matches)]; |
120
|
5 |
|
foreach ($this->getRelations() as $objectKey => $recordKey) { |
121
|
5 |
|
$entry = $record->getEntry($objectKey); |
122
|
5 |
|
$entry->setData($dataLine->offsetGet($objectKey), true); |
123
|
|
|
} |
124
|
5 |
|
return true; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param Records\ARecord|Records\PageRecord $record |
129
|
|
|
* @throws MapperException |
130
|
|
|
* @return bool |
131
|
|
|
* Scan array and remove items that have set equal values as that in passed record |
132
|
|
|
*/ |
133
|
4 |
|
protected function deleteRecord(Records\ARecord $record): bool |
134
|
|
|
{ |
135
|
4 |
|
$toDelete = $this->findMatched($record); |
136
|
4 |
|
if (empty($toDelete)) { |
137
|
1 |
|
return false; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
// remove matched |
141
|
3 |
|
foreach ($toDelete as $key => $record) { |
142
|
3 |
|
unset($this->records[$key]); |
143
|
|
|
} |
144
|
3 |
|
return $this->saveSource($this->records); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param Records\ARecord $record |
149
|
|
|
* @throws MapperException |
150
|
|
|
* @return Records\ARecord[] |
151
|
|
|
*/ |
152
|
15 |
|
public function loadMultiple(Records\ARecord $record): array |
153
|
|
|
{ |
154
|
15 |
|
return array_values($this->findMatched($record)); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|