1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_mapper\Mappers\Shared; |
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\Records; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Trait TWriteFileTable |
14
|
|
|
* @package kalanis\kw_mapper\Mappers\Shared |
15
|
|
|
* Abstract for manipulation with file content as table - write content |
16
|
|
|
*/ |
17
|
|
|
trait TWriteFileTable |
18
|
|
|
{ |
19
|
|
|
use TFinder; |
20
|
|
|
use TStore; |
21
|
|
|
|
22
|
|
|
/** @var bool */ |
23
|
|
|
protected $orderFromFirst = true; |
24
|
|
|
|
25
|
3 |
|
public function orderFromFirst(bool $orderFromFirst = true): self |
26
|
|
|
{ |
27
|
3 |
|
$this->orderFromFirst = $orderFromFirst; |
28
|
3 |
|
return $this; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param Records\ARecord|Records\PageRecord $record |
33
|
|
|
* @throws MapperException |
34
|
|
|
* @return bool |
35
|
|
|
*/ |
36
|
5 |
|
protected function insertRecord(Records\ARecord $record): bool |
37
|
|
|
{ |
38
|
5 |
|
$this->clearSource(); |
39
|
5 |
|
$matches = $this->findMatched($record, !empty($this->getPrimaryKeys())); |
40
|
5 |
|
if (!empty($matches)) { // already found!!! |
41
|
1 |
|
return false; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
// pks |
45
|
5 |
|
$records = array_map([$this, 'toArray'], $this->records); |
46
|
5 |
|
foreach ($this->getPrimaryKeys() as $primaryKey) { |
47
|
4 |
|
$entry = $record->getEntry($primaryKey); |
48
|
4 |
|
if (in_array($entry->getType(), [IEntryType::TYPE_INTEGER, IEntryType::TYPE_FLOAT])) { |
49
|
1 |
|
if (empty($entry->getData())) { |
50
|
1 |
|
$data = empty($records) ? 1 : intval(max(array_column($records, $primaryKey))) + 1 ; |
51
|
1 |
|
$entry->setData($data); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
5 |
|
$this->records = $this->orderFromFirst ? array_merge($this->records, [$record]) : array_merge([$record], $this->records); |
57
|
5 |
|
return $this->saveSource($this->records); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param Records\ARecord $object |
62
|
|
|
* @return array<string|int, string|int|float|object|array<string|int|float|object>> |
63
|
|
|
*/ |
64
|
5 |
|
public function toArray($object) |
65
|
|
|
{ |
66
|
5 |
|
$ex = new DataExchange($object); |
67
|
5 |
|
return $ex->export(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param Records\ARecord|Records\PageRecord $record |
72
|
|
|
* @throws MapperException |
73
|
|
|
* @return bool |
74
|
|
|
*/ |
75
|
4 |
|
protected function updateRecord(Records\ARecord $record): bool |
76
|
|
|
{ |
77
|
4 |
|
$this->clearSource(); |
78
|
4 |
|
$matches = $this->findMatched($record, !empty($this->getPrimaryKeys()), true); |
79
|
4 |
|
if (empty($matches)) { // nothing found |
80
|
1 |
|
return false; |
81
|
|
|
} |
82
|
|
|
|
83
|
3 |
|
reset($matches); |
84
|
3 |
|
$dataLine = & $this->records[key($matches)]; |
85
|
3 |
|
foreach ($this->getRelations() as $objectKey => $recordKey) { |
86
|
3 |
|
if (in_array($objectKey, $this->getPrimaryKeys())) { |
87
|
2 |
|
continue; // no to change pks |
88
|
|
|
} |
89
|
3 |
|
$dataLine->offsetSet($objectKey, $record->offsetGet($objectKey)); |
90
|
|
|
} |
91
|
3 |
|
return $this->saveSource($this->records); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param Records\ARecord|Records\PageRecord $record |
96
|
|
|
* @throws MapperException |
97
|
|
|
* @return bool |
98
|
|
|
* Scan array and remove items that have set equal values as these in passed record |
99
|
|
|
*/ |
100
|
4 |
|
protected function deleteRecord(Records\ARecord $record): bool |
101
|
|
|
{ |
102
|
4 |
|
$this->clearSource(); |
103
|
4 |
|
$toDelete = $this->findMatched($record); |
104
|
4 |
|
if (empty($toDelete)) { |
105
|
1 |
|
return false; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// remove matched |
109
|
3 |
|
foreach ($toDelete as $key => $record) { |
110
|
3 |
|
unset($this->records[$key]); |
111
|
|
|
} |
112
|
3 |
|
return $this->saveSource($this->records); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|