|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_mapper\Mappers\Storage; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\kw_mapper\Interfaces\IFileFormat; |
|
7
|
|
|
use kalanis\kw_mapper\MapperException; |
|
8
|
|
|
use kalanis\kw_mapper\Mappers\AMapper; |
|
9
|
|
|
use kalanis\kw_mapper\Storage\Shared; |
|
10
|
|
|
use kalanis\kw_mapper\Storage\Storage; |
|
11
|
|
|
use kalanis\kw_storage\StorageException; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class AStorage |
|
16
|
|
|
* @package kalanis\kw_mapper\Mappers\Storage |
|
17
|
|
|
* The path is separated |
|
18
|
|
|
* - storage has first half which is usually static |
|
19
|
|
|
* - content has second half which can be changed by circumstances |
|
20
|
|
|
*/ |
|
21
|
|
|
abstract class AStorage extends AMapper |
|
22
|
|
|
{ |
|
23
|
|
|
use Storage\TStorage; |
|
24
|
|
|
use Shared\TFormat; |
|
25
|
|
|
|
|
26
|
24 |
|
public function getAlias(): string |
|
27
|
|
|
{ |
|
28
|
24 |
|
return $this->getSource(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
13 |
|
protected function getReadSource(): string |
|
32
|
|
|
{ |
|
33
|
13 |
|
return $this->getSource(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
8 |
|
protected function getWriteSource(): string |
|
37
|
|
|
{ |
|
38
|
8 |
|
return $this->getSource(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param IFileFormat|null $format |
|
43
|
|
|
* @throws MapperException |
|
44
|
|
|
* @return array<string|int, array<string|int, string|int|float|bool|array<string|int, string|int|float|bool>>> |
|
45
|
|
|
*/ |
|
46
|
13 |
|
protected function loadFromStorage(?IFileFormat $format = null): array |
|
47
|
|
|
{ |
|
48
|
|
|
try { |
|
49
|
13 |
|
$format = $format ?: Shared\FormatFiles\Factory::getInstance()->getFormatClass($this->getFormat()); |
|
50
|
13 |
|
return $format->unpack($this->getStorage()->read($this->getReadSource())); |
|
51
|
1 |
|
} catch (StorageException $ex) { |
|
52
|
1 |
|
throw new MapperException('Unable to read from source', 0, $ex); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param array<string|int, array<string|int, string|int|float|bool|array<string|int, string|int|float|bool>>> $content |
|
58
|
|
|
* @param IFileFormat|null $format |
|
59
|
|
|
* @throws MapperException |
|
60
|
|
|
* @return bool |
|
61
|
|
|
*/ |
|
62
|
8 |
|
protected function saveToStorage(array $content, ?IFileFormat $format = null): bool |
|
63
|
|
|
{ |
|
64
|
|
|
try { |
|
65
|
8 |
|
$format = $format ?: Shared\FormatFiles\Factory::getInstance()->getFormatClass($this->getFormat()); |
|
66
|
8 |
|
return $this->getStorage()->write($this->getWriteSource(), $format->pack($content)); |
|
67
|
1 |
|
} catch (StorageException $ex) { |
|
68
|
1 |
|
throw new MapperException('Unable to write into source', 0, $ex); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|