|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_mapper\Mappers\Shared; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\kw_mapper\Interfaces\IFileFormat; |
|
7
|
|
|
use kalanis\kw_mapper\MapperException; |
|
8
|
|
|
use kalanis\kw_mapper\Records\ARecord; |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Trait TFile |
|
13
|
|
|
* @package kalanis\kw_mapper\Mappers\Storage |
|
14
|
|
|
* Abstract layer for working with single files as content source |
|
15
|
|
|
*/ |
|
16
|
|
|
trait TFile |
|
17
|
|
|
{ |
|
18
|
|
|
use TContent; |
|
19
|
|
|
use TSource; |
|
20
|
|
|
use TPrimaryKey; |
|
21
|
|
|
|
|
22
|
16 |
|
public function setPathKey(string $pathKey): self |
|
23
|
|
|
{ |
|
24
|
16 |
|
$this->addPrimaryKey($pathKey); |
|
25
|
16 |
|
return $this; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param ARecord $record |
|
30
|
|
|
* @throws MapperException |
|
31
|
|
|
* @return bool |
|
32
|
|
|
*/ |
|
33
|
5 |
|
protected function insertRecord(ARecord $record): bool |
|
34
|
|
|
{ |
|
35
|
5 |
|
return $this->updateRecord($record); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param ARecord $record |
|
40
|
|
|
* @throws MapperException |
|
41
|
|
|
* @return bool |
|
42
|
|
|
*/ |
|
43
|
5 |
|
protected function updateRecord(ARecord $record): bool |
|
44
|
|
|
{ |
|
45
|
5 |
|
$this->setSource(strval($record->offsetGet($this->getPathFromPk($record)))); |
|
46
|
5 |
|
return $this->saveToStorage([[$record->offsetGet($this->getContentKey())]]); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param ARecord $record |
|
51
|
|
|
* @throws MapperException |
|
52
|
|
|
* @return int |
|
53
|
|
|
*/ |
|
54
|
1 |
|
public function countRecord(ARecord $record): int |
|
55
|
|
|
{ |
|
56
|
1 |
|
$this->setSource(strval($record->offsetGet($this->getPathFromPk($record)))); |
|
57
|
1 |
|
return intval(!empty($this->loadFromStorage())); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param ARecord $record |
|
62
|
|
|
* @throws MapperException |
|
63
|
|
|
* @return bool |
|
64
|
|
|
*/ |
|
65
|
9 |
|
protected function loadRecord(ARecord $record): bool |
|
66
|
|
|
{ |
|
67
|
9 |
|
$this->setSource(strval($record->offsetGet($this->getPathFromPk($record)))); |
|
68
|
8 |
|
$stored = $this->loadFromStorage(); |
|
69
|
7 |
|
$row = reset($stored); |
|
70
|
7 |
|
if (false === $row || !is_array($row)) { |
|
71
|
1 |
|
throw new MapperException('Cannot load data array from storage'); |
|
72
|
|
|
} |
|
73
|
6 |
|
$entry = reset($row); |
|
74
|
6 |
|
if (false === $entry) { |
|
75
|
1 |
|
throw new MapperException('Cannot load data entry from storage'); |
|
76
|
|
|
} |
|
77
|
5 |
|
$record->getEntry($this->getContentKey())->setData($entry, true); |
|
78
|
5 |
|
return true; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param ARecord $record |
|
83
|
|
|
* @throws MapperException |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
14 |
|
protected function getPathFromPk(ARecord $record): string |
|
87
|
|
|
{ |
|
88
|
14 |
|
$pk = reset($this->primaryKeys); |
|
89
|
14 |
|
if (!$pk || empty($record->offsetGet($pk))) { |
|
90
|
1 |
|
throw new MapperException('Cannot manipulate content without primary key - path!'); |
|
91
|
|
|
} |
|
92
|
13 |
|
return $pk; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param IFileFormat|null $format |
|
97
|
|
|
* @throws MapperException |
|
98
|
|
|
* @return array<string|int, array<string|int, string|int|float|bool|array<string|int, string|int|float|bool>>> |
|
99
|
|
|
*/ |
|
100
|
|
|
abstract protected function loadFromStorage(?IFileFormat $format = null): array; |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param array<string|int, array<string|int, string|int|float|bool|array<string|int, string|int|float|bool>>> $content |
|
104
|
|
|
* @param IFileFormat|null $format |
|
105
|
|
|
* @throws MapperException |
|
106
|
|
|
* @return bool |
|
107
|
|
|
*/ |
|
108
|
|
|
abstract protected function saveToStorage(array $content, ?IFileFormat $format = null): bool; |
|
109
|
|
|
} |
|
110
|
|
|
|